2016-11-21 10 views
1

私はPcまたはMacで私の唯一のブラウザとして長い間Firefoxを使用しています。 私の問題:私はautomaterと AppleScriptを使ってmac上でサービスを作成して、translate.google.comを使って非常に瞬時に翻訳したいと考えています。まったくFirefoxで動作しません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 

同じこと(スクリプト)(スクリプトの4つのまたは5行の下)SafariやChromeで素晴らしい作品は何 は、私が不可能な問題を回避するためにさまざまな方法 でみてください

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 

それは何も変更せずにサファリで動作する場合、問題はFirefoxのエントリであるので、あなたがこの問題を見ることができれば、それは私たちすべてに非常に有用であろう。 ありがとうございました。 ありがとうございます。

答えて

1

SafariとChromeはURLの予約文字のnecessary encodingを実行しますが、Firefoxはそうではありません。

したがって、クエリ文字列値(URLに埋め込むテキスト)を明示的にエンコードする必要があります。

(明白ではないが)最も簡単な方法は、シェルコマンドを経由して、perlを使用することです感謝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 

上記のアプローチが言及した3つのすべてのブラウザで動作します:Firefoxの、サファリ、およびGoogleのクロム。

注:(少なくとも)FirefoxのV50のよう

、Firefoxはデフォルトで現在のフロントウィンドウに新しいタブ内でURLを開きます。

あなたは、FirefoxはFirefoxの好みのGeneralタブにOpen new windows in a new tab insteadをオフにして、代わりに新しいウィンドウでURLを開くことができます。

ただし、これは永続的な設定であり、すべてのURLはFirefoxの外部から開かれています。

設定変更に依存しない新しいウィンドウで開くアドホックソリューションについては、this answerを参照してください。

+0

に分かれて:それは好奇心旺盛であるだけで14F2105を構築ヨセミテ上のFirefox 50のインストールと、それは予想通り(タブで開いた)働いた新鮮でそれを試してみました。 – mklement0

-1

こんにちは、いくつかのバージョンのAutomatorで問題が発生する可能性があります。スクリプトを変更して、ほぼすべての場所で動作するようにしました。 システムの頻繁なエラーは、システム環境設定タブのセキュリティとプライバシーによって処理されるコンピュータを制御するアプリケーションに対するアクセス許可です。このサービスを使用しているFirefox、Texeditおよびその他のキーボードショートカットを許可するかどうかを尋ねます。 enter image description here また、Automator create service(全般に表示され、すべてのアプリケーションに表示されます)にはエントリーがありません(El Capitan以来のYosemiteまで、たとえばFirefoxですべてのサービスを利用できることがわかりました) 。以下の2スクリプトまたは1つのスクリプトのみdeek5 @

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 
関連する問題