AppleScript:在传递给 Firefox 的 URL 中编码任意查询字符串值

AppleScript: encoding arbitrary query-string values in URLs passed to Firefox

我在 PC 或 Mac 上使用 firefox 作为我唯一的浏览器已经很长时间了。 简而言之,我的问题是:我想在 mac 上使用 automator 和 Applescript 使用 translate.google.com 进行相当即时的翻译。 什么对 Safari 或 Chrome(脚本的 4 或 5 行以下)

最有效
On run {input, parameters}
Tell application "Safari"
activate
try
Open location "https://translate.google.com/#auto/en/" & (the clipboard)
end try
end tell
end run

同样的东西(脚本)在 Firefox 上根本不起作用,我尝试了不同的方法 绕开不可能的问题

On run {input, parameters}
Set theProcess to "Firefox"
Set info to {}
Set y to 0
Set x to 0
Set n to 0

Tell application "Applications / Firefox.app"
activate
Open location "http://translate.google.com/#auto/en/"
end tell
Tell application "System events"
Repeat with theProcess in (process "Firefox")
try
Set info to info & (value of (first attribute whose name is "AXWindows") of theProcess)
end try
end repeats
Set n to count of info
info
end tell
Tell application "System Events" to tell process "Firefox"
Set x to "1"
Set frontmost to true
try
Click menu item "Paste" of menu "Edit" of menu bar 1
end try
Repeat while x is "1" -
If x is "1" then
Keystroke "V" using command down
Set x to "0"

end if
end repeat
end tell
end run

通过复制和粘贴,操作会在页面完全加载之前发生,即使减慢复制和粘贴过程也是如此。 经过多次观察,剪贴板中包含的文本与URL的关联存在格式化问题,我对此进行了改进,但还不完美。

tell application "Applications/Firefox.app" to activate
tell application "System Events" to tell process "Firefox"
    set frontmost to true
    set sentence to text of (the clipboard)
    set thesentences to paragraphs of sentence
    set thenewsentences to thesentences as string
    set the clipboard to thenewsentences
    keystroke "t" using command down
    keystroke "https://translate.google.com/#auto/fr/" & (the clipboard) & return
end tell

反正如果不修改任何东西就可以和Safari一起使用,问题出在Firefox入口,所以如果你能看看这个问题,那对我们大家来说都是非常有用的。 感谢您的关注。 谢谢你的回答。

Safari 和 Chrome 为您执行 URL 中保留字符的 necessary encoding,但 Firefox 不会。

因此,您需要显式地对查询字符串值(要嵌入 URL 的文本)进行编码。

最简单(虽然不明显)的方法是使用 perl,通过 shell 命令,感激地改编自 here:

# Example input that contains chars. that have special meaning in a URL ('&' and '?')
set the clipboard to "Non, Je ne regrette rien & rien ne va plus?"

# Get text from the clipboard and URL-encode it.
set encodedText to do shell script ¬
  "perl -MURI::Escape -lne 'print uri_escape($_)' <<<" & quoted form of (the clipboard)

# Now it's safe to append the encoded text to the URL template.
tell application "Firefox"
    activate
    open location "https://translate.google.com/#auto/en/" & encodedText
end tell

上述方法适用于上述所有三种浏览器:Firefox、Safari 和 Google Chrome.

注:

从(至少)Firefox v50 开始,Firefox 默认在当前 window 的新 选项卡 中打开 URL。

您可以让 Firefox 在新的 window 中打开 URL,方法是取消选中 General 上的 Open new windows in a new tab instead Firefox 首选项选项卡。

但是请注意,这是一个持久性设置,会影响从 Firefox 外部打开的所有 URLs。

有关在不依赖于更改设置的新 window 中打开的临时解决方案,请参阅我的 this answer

您好,某些版本的 Automator 服务可能会遇到问题,我修改了脚本,使其几乎可以在任何地方使用。 一个常见的系统错误是允许应用程序控制您的计算机,这是由系统首选项选项卡安全和隐私处理的,系统会询问您是否允许 Firefox、TexEdit 和其他使用此服务的键盘快捷键。 同样在 Automator 中创建服务(一般(并出现在所有应用程序中)没有条目(最多 Yosemite 因为我看到 El Capitan 使用 Firefox 例如所有服务都可用),选择执行脚本 Applescript 粘贴以下脚本分为2个脚本或1个脚本。

on run

    set Sn to ""

    tell application "System Events"
        set Sn to the short name of first process whose frontmost is true --here we look for and find which application to launch the service

        tell process Sn

            set frontmost to true
            try
                click menu item "Copy" of menu "Edit" of menu bar 1 -- there is used the Copier of the menu Editing of the application
            end try
        end tell
    end tell
    return the clipboard

end run   

     In the script following the entry is done with the contents of the Clipboard
    On run {input}

on run {input}

    set input to (the clipboard) -- Here we paste the contents of the Clipboard into a variable
    try
        set input to do shell script "Perl -MURI :: Escape -lne 'print uri_escape ($ _)' <<< " & quoted form of input --To format the text to make it usable with Firefox and the url of translate.google.com

        tell application id "org.mozilla.firefox"
            activate
            open location "https://translate.google.com/#auto/en/" & input --here since version 50 of Firefox you must open a tab and not a new url window of translate.google.com, with option #auto automatic detection by google of the language, and fr to translate into French (these options are modifiable at will)
        end tell
    end try
end run -- end of the operation you have a tab open on translate, a text to translate and a translated text