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

问题描述:

我长时间使用firefox作为PC或Mac上唯一的浏览器。 简而言之,我的问题是:我想用automator创建一个mac服务,使用translate.google.com进行即时翻译的AppleScript也可以使用 。 什么使用Safari或Chrome的伟大工程(低于4个或5行脚本)AppleScript:在传递给Firefox的URL中编码任意查询字符串值

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和谷歌铬。

注:

由于(至少)V50火狐,火狐打开URL在默认情况下在当前前窗新标签

您可以让Firefox在新窗口而不是打开URL,通过Firefox的偏好General选项卡上取消选中Open new windows in a new tab instead

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

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

+0

@ deek5:那倒是奇怪刚在新的Firefox 50的上约塞米蒂安装建设14F2105和它的工作如预期(在标签打开)。 – mklement0

你好,下面的服务Automator与某些版本可能会遇到问题,我修改了脚本,使其几乎无处不在。 频繁的系统错误是系统偏好选项卡Security and Privacy处理您的计算机的应用程序权限,系统询问您是否允许使用此服务的Firefox,TexEdit和其他人使用其键盘快捷方式。 enter image description here在Automator中 还创建服务(为一般(和出现在所有的应用程序)的条目(最多优胜美地,因为埃尔卡皮坦我看到了与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