首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Applescript Google翻译

Applescript Google翻译
EN

Stack Overflow用户
提问于 2021-10-19 10:24:01
回答 2查看 89关注 0票数 1

当选定的文本只有一个单词时,代码工作得很好,但对于多个单词,代码就不能正常工作。我认为单词之间的空格导致了这个问题,但我不知道如何解决它。

代码语言:javascript
复制
on run {input, parameters}
 set phrase to input as string
 set phrase to quoted form of phrase

 set ui_lang to "en"
 set from_lang to "en"
 set to_lang to "tr"

 return "https://translate.google.com/?hl=" & ui_lang & "&sl=" & from_lang & "&tl=" & to_lang & "&text=" & phrase

终点管路

EN

回答 2

Stack Overflow用户

发布于 2021-10-19 12:14:31

如果它是构成URL的一部分,那么使用quoted form将不起作用,因为这将为shell准备文本。

对于网址,包括空格在内的特殊字符必须为url encoded。空格被%20替换,其他特殊字符有自己的'%‘代码。

解决此特定情况的一种快速而肮脏的方法是将空格替换为'%20‘,将以下内容作为您的前三行:

代码语言:javascript
复制
set phrase to input as string -- no changes
set AppleScript's text item delimiters to {"%20"}
set phrase to words of phrase as text

基本上,它将字符串拆分成单词,并使用%20而不是空格重新连接它们。它也应该与单个单词输入一起工作。您应该在后续行中将TID改回任意值(或默认的{""})。

例如:

"and empathetic programmers"变成了"and%20empathetic%20programmers""https://translate.google/?hl=en&sl=en&tl=tr&text=and%20empathetic%20programmers"变成了"https://translate.google/?hl=en&sl=en&tl=tr&text=and%20empathetic%20programmers"

这个网站上有许多讨论这个主题的问题,包括一些特定于applescript的问题。您还可以在这里找到更多,包括一些处理程序:Text Encoding | DecodingEncoding and Decoding Text

有很多关于这个主题的文章,所以搜索起来很容易。还有许多在线编码器/解码器,您可以使用它们进行测试比较。

票数 0
EN

Stack Overflow用户

发布于 2021-10-19 15:24:33

代码语言:javascript
复制
on run {input, parameters}
    
    set phrase to input as string
    
    set ui_lang to "en"
    set from_lang to "en"
    set to_lang to "tr"
    
    open location "https://translate.google.com/?hl=" & ui_lang & "&sl=" & from_lang & "&tl=" & to_lang & "&text=" & phrase
    
end run
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69629136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档