「OS付属の辞書、Logophile、Googleで検索する」AppleScript
Macで「OS付属の辞書、Logophile、Googleで検索する」AppleScript。
アプリケーション形式で保存しての使用前提。
実行するとダイアログが出るので、それに従う。
要Python。
--DESCRIPTION:OS付属の辞書、Logophile、Googleで検索する.scpt (* 作成日:2015-04-24 修正日:2017-06-21 作成環境:Mac OS X 10.7.5 *) property name : "OS付属の辞書、Logophile、Googleで検索する.scpt" property target_URL_g_sr : "https://www.google.co.jp/search?hl=ja&q=" property target_URL_g_tr : "https://translate.google.com/?hl=ja#auto/ja/" --参照URL --http://sakito.jp/mac/dictionary.html --雑多の備忘録 - AppleScript/文字列 --http://qz.tsugumi.org/AppleScript%2F%CA%B8%BB%FA%CE%F3.html --AppleScript for Python Programmers (Comparison Chart), by Aurelio Jargas --http://aurelio.net/articles/applescript-vs-python.html --MacScripter / Trim [Remove Spaces] --http://macscripter.net/viewtopic.php?id=18519 --ASCIIコード表 --http://www9.plala.or.jp/sgwr-t/c_sub/ascii.html on run --set gotValue to my getStringAtFrontMostApp() try set gotValue to the clipboard as Unicode text set gotValue to my trim(true, gotValue) on error set gotValue to "" end try if gotValue is "" then set theResult to display dialog "Please input words to search." default answer "" buttons {"Cancel", "Dictionary", "Google"} default button 2 set gotValue to text returned of theResult --display dialog gotValue set gotButton to button returned of theResult else set theResult to display dialog "Please check words to search." default answer gotValue buttons {"Cancel", "Dictionary", "Google"} default button 2 set gotValue to text returned of theResult set gotButton to button returned of theResult end if if (gotButton is "Dictionary") then lookupInDictionary(gotValue) --delay 1 set targetApp to application "Dictionary" lookupInLogophile(targetApp) else if (gotButton is "Google") then set theResult to display dialog "Please select search or translation." & return & return & "Search word(s): " & gotValue buttons {"Search", "Both", "Translation"} default button 2 set gotButton to button returned of theResult set wordsNumber to count of word in gotValue set search_string to my myUrlEncode(gotValue) if gotButton is "Search" then set locationURL_g_sr to target_URL_g_sr & search_string as Unicode text open location locationURL_g_sr --検索結果ページを開きます else if gotButton is "Translation" then set locationURL_g_tr to target_URL_g_tr & search_string as Unicode text open location locationURL_g_tr --検索結果ページを開きます else if gotButton is "Both" then set locationURL_g_sr to target_URL_g_sr & search_string as Unicode text set locationURL_g_tr to target_URL_g_tr & search_string as Unicode text open location locationURL_g_sr --検索結果ページを開きます open location locationURL_g_tr --検索結果ページを開きます end if end if end run --------------------サブルーチン-------------------- on trim(theseCharacters, someText) --下記URL記載のものを若干修正して流用 --http://macscripter.net/viewtopic.php?id=18519 -- Lazy default (AppleScript doesn't support default values) if theseCharacters is true then set theseCharacters to ¬ {space, tab, linefeed, return, ASCII character 0} -- ASCII character 0 は NUL(null文字) repeat until first character of someText is not in theseCharacters set someText to text 2 thru -1 of someText end repeat repeat until last character of someText is not in theseCharacters set someText to text 1 thru -2 of someText end repeat return someText end trim on getStringAtFrontMostApp() set frontmostApp to path to frontmost application tell application "Finder" set appName to name of frontmostApp end tell --display dialog appName if appName ends with ".app" then set appName to text 1 thru -5 of appName end if activate application appName tell application "System Events" tell application process appName keystroke "c" using command down end tell end tell delay 1 tell application appName try set theText to the clipboard as Unicode text on error set theText to "" end try end tell return theText end getStringAtFrontMostApp on lookupInLogophile(targetApp) --set frontmostApp to path to frontmost application set frontmostApp to path to targetApp tell application "Finder" set appName to name of frontmostApp end tell if appName ends with ".app" then set appName to text 1 thru -5 of appName end if activate application appName tell application "System Events" tell application process appName keystroke "e" using {command down, shift down} end tell end tell activate application "Logophile" end lookupInLogophile on lookupInDictionary(gotValue) try set myWordForSearch to gotValue as Unicode text on error set myWordForSearch to "" end try if myWordForSearch is not "" then set escapedMyWordForSearch to my escapeForShellScript(myWordForSearch) --display dialog escapedMyWordForSearch set myURL to "dict://" & escapedMyWordForSearch as Unicode text --display dialog myURL set myShellScript to "open '" & myURL & "'" --display dialog myShellScript --open location myURL --「付属」等ローマ字以外の文字列を渡すと開けない? do shell script myShellScript activate application "Dictionary" else say "No word is available." activate application "Dictionary" end if end lookupInDictionary on escapeForShellScript(theText) --最初は単純にエスケープ文字に置換したがうまくいかなかったので、URLエンコーディングの表現に置換するようにした。 --下記のwebツールで変換した --http://www.tagindex.com/tool/url.html set modifiedText to theText if (offset of "'" in modifiedText) > 0 then --set modifiedText to my replaceText(modifiedText, "'", "\\'") -- %27 set modifiedText to my replaceText(modifiedText, "'", "%27") -- %27 end if if (offset of "\"" in modifiedText) > 0 then --set modifiedText to my replaceText(modifiedText, "\"", "\\\"") set modifiedText to my replaceText(modifiedText, "\"", "%22") end if return modifiedText end escapeForShellScript --以下はurlエンコーディングする関数、「python」を使用 --urlエンコーディングは下記URLのページを参照 --http://www2.airnet.ne.jp/~kenshi/mac08.html on myUrlEncode(sourceText) do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of sourceText -- python で URL エンコードする。 set encodedText to the result -- 結果を取得する。 return encodedText end myUrlEncode on devText(theText, aDelimiter) set tmp to AppleScript's text item delimiters set AppleScript's text item delimiters to aDelimiter set theList to every text item of theText set AppleScript's text item delimiters to tmp return theList end devText on joinList(theList, aDelimiter) set tmp to AppleScript's text item delimiters set AppleScript's text item delimiters to aDelimiter set theText to theList as string set AppleScript's text item delimiters to tmp return theText end joinList on replaceText(theText, serchStr, replaceStr) set tmp to AppleScript's text item delimiters set AppleScript's text item delimiters to serchStr set theList to every text item of theText set AppleScript's text item delimiters to replaceStr set theText to theList as string set AppleScript's text item delimiters to tmp return theText end replaceText