Macos AppleScript:对传递给Firefox的URL中的任意查询字符串值进行编码

Macos AppleScript:对传递给Firefox的URL中的任意查询字符串值进行编码,macos,firefox,applescript,urlencode,Macos,Firefox,Applescript,Urlencode,很长一段时间以来,我一直使用firefox作为Pc或Mac上的唯一浏览器。 简单地说,我的问题是:我想在mac上用automator和 使用translate.google.com,使用Applescript进行即时翻译。 Safari或Chrome最适合的功能(在脚本的4行或5行以下) 同样的事情(脚本)在Firefox上根本不起作用,我尝试了不同的方法 绕过不可能解决的问题 On run {input, parameters} Set theProcess to "Firefox" Set

很长一段时间以来,我一直使用firefox作为Pc或Mac上的唯一浏览器。 简单地说,我的问题是:我想在mac上用automator和 使用translate.google.com,使用Applescript进行即时翻译。 Safari或Chrome最适合的功能(在脚本的4行或5行以下)

同样的事情(脚本)在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中保留字符的搜索,但Firefox没有

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
因此,您需要显式地执行查询字符串值(要嵌入URL中的文本)的编码

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

#包含字符的示例输入。在URL中具有特殊含义的(“&”和“?”)
将剪贴板设置为“不,我不后悔,我不后悔&rien-ne-va plus?”
#从剪贴板获取文本并对其进行URL编码。
将encodedText设置为执行shell脚本

“perl-MURI::Escape-lne‘print uri_Escape($)”Hello在服务自动机下面使用某些版本时可能会遇到问题,我修改了脚本,使其几乎可以在任何地方工作。 一个常见的系统错误是允许应用程序控制您的计算机,该权限由“系统首选项”选项卡“安全和隐私”处理。系统会询问您是否允许Firefox、TexEdit和其他人使用此服务作为其键盘快捷键。 此外,在Automator create service(一般来说(并出现在所有应用程序中)中,无条目(直到Yosemite,因为El Capitan,我看到Firefox的所有服务都可用),选择Execute a script Applescript paste将下面的脚本分为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
运行时

将序号设置为“”
告诉应用程序“系统事件”
将Sn设置为最前面为true的第一个进程的短名称——在这里,我们查找并找到要启动服务的应用程序
告诉流程序号
将frontmost设置为true
尝试
单击菜单栏1菜单“编辑”的菜单项“复制”-应用程序菜单编辑的复印机已使用
结束尝试
结束语
结束语
返回剪贴板
终点
在脚本中,以下条目是使用剪贴板的内容完成的
运行时{input}
运行时{input}
将输入设置为(剪贴板)——这里我们将剪贴板的内容粘贴到一个变量中
尝试

将输入设置为执行shell脚本“Perl-MURI::Escape-lne'print uri_Escape($)@deek5:这很奇怪。刚刚在Yosemite build 14F2105上新安装的Firefox 50上尝试了它,它按预期工作(在选项卡中打开)。