AppleScript Google搜索带引号

时间:2018-07-31 22:33:55

标签: macos search automator

我想使用Automator对所选文本进行Google搜索,但所选文本应在引号内!

我不知道如何使脚本在所选文本(输入)上加上引号,例如“所选文本”。

on run {input, parameters}
   open location "https://www.google.com/search?q=" & input
end run

编辑

这是解决方案:

on run {input, parameters}
   open location "https://www.google.com/search?q=" & quote & input & quote
end run

3 个答案:

答案 0 :(得分:0)

假设您希望双引号成为搜索查询的一部分(即您正在Google进行文字字符串搜索),则需要在搜索字符串周围添加文字双引号。好吧,除了URL中不允许使用文字双引号,因此应将它们编码为%22,如下所示:

open location "https://www.google.com/search?q=%22" & input & "%22"

请注意,尽管input字符串似乎可以自动处理诸如空格之类的基本内容,但您可能还需要对其进行URL编码。如果您需要更好的处理方法,请参见"I need to URL-encode a string in AppleScript"(与aaplmath相关的问题)。

答案 1 :(得分:0)

如果您希望Google为您提供与所要查找的词组完全相同的词组,则搜索查询必须在引号内,为此,Automator Service的外观如下:

on run {input, parameters}
   open location "https://www.google.com/search?q=" & quote & input & quote
end run

答案 2 :(得分:0)

我强烈建议您使用AppleScriptObjC的技能(此处是在Foundation框架的帮助下)可靠地转义搜索字符串(macOS 10.10 +)

use framework "Foundation"
use scripting additions

on run {input, parameters}
    set nsInput to current application's NSString's stringWithString:("https://www.google.com/search?q=" & input)
    set characterSet to current application's NSCharacterSet's URLQueryAllowedCharacterSet()
    set escapedInput to (nsInput's stringByAddingPercentEncodingWithAllowedCharacters:characterSet) as text
    open location escapedInput
end run

它处理引号以及空格或其他特殊字符