【问题标题】:AppleScript Google search with quotes带引号的 AppleScript Google 搜索
【发布时间】:2018-08-03 02:09:30
【问题描述】:

使用 Automator,我想对选定的文本进行谷歌搜索,但选定的文本应该在引号内!

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

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

【问题讨论】:

标签: macos search automator


【解决方案1】:

假设您希望双引号成为搜索查询的一部分(即您在 Google 进行文字字符串搜索),您需要在搜索字符串周围添加文字双引号。好吧,除了 URL 中不允许使用文字双引号,因此它们应该被 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 链接)。

【讨论】:

    【解决方案2】:

    我强烈建议使用 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
    

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

    【讨论】:

    • 谢谢,但在尝试您的建议时,Automator 说 无法获得 «script» 的框架“Foundation”。不允许访问。
    • 好吧,我在 Automator (macOS 10.13) 中测试了代码,它工作正常。
    • 我在 10.9.5。也许这就是问题所在?
    • 确实,这就是问题所在,Yosemite (10.10) 及更高版本支持 Script Editor 和 Automator 中的 AppleScriptObjC。
    • 通过实验,找到了解决办法:open location "https://www.google.com/search?q=" & "\"" & input & "\""
    【解决方案3】:

    如果您希望 Google 为您提供包含您要查找的确切短语的结果,则搜索查询必须在引号内,为此 Automator 服务必须如下所示:

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

    【讨论】:

      猜你喜欢
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多