2016-11-21 30 views
1

我長時間使用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項目,所以如果你可以看看這個問題,這將是我們大家非常有用的。 感謝您的關注。 謝謝你的回答。

回答

1

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

-1

你好,下面的服務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 
相關問題