2006 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2007 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2008 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2009 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2010 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2011 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2012 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2010-10-24(日)
■[Mac][Chrome]Google ChromeをAppleScriptで操作する
Google Chromeはver.7以降でAppleScriptに対応している。正確に書くと、Google ChromeのプロセスにAppleEventを送ることが出来るようになった。
AppleScriptはAppleScriptエディタを使って書いて実行するか、エディタで書いてosascriptコマンドで実行することが出来る。
つまり、他のアプリケーションやshellからGoogle Chromeを操作する、Google Chromeの情報を取得することが可能になった。
例えば、Google Chrome内で選択したテキストをそのままEmacsのバッファに挿入したい場合、(AppleScriptを使わなければ、Google Chromeでテキストを選択→Command-c→Emacsに移動→(kill-ringとpasteboardが連動していることを前提にして)C-yとする必要があったが)
Google Chromeでテキストを選択して、
C-u M-! osascript -e 'tell application "Google Chrome" to get copy selection of active tab of window 1' ; pbpaste
で出来るようになった。
ちなみに、vimで同じことをするには、
:r! osascript -e 'tell application "Google Chrome" to get copy selection of active tab of window 1' ; pbpaste
選択テキストをEmacsのバッファにそのまま挿入するのではなく、加工して挿入したい場合、例えば、前に作ったGoogle翻訳コマンドラインツールを使えば、
C-u M-! osascript -e 'tell application "Google Chrome" to get copy selection of active tab of window 1' ; pbpaste | transrator
AppleScriptからGoogle Chromeに対して何が出来るかは、AppleScriptエディタにGoogle ChromeをD&Dすれば(もしくはAppleScriptエディタの[ファイル]-[用語説明を開く]からGoogle Chromeを選択すれば)、コマンド一覧と説明が見える。
幾つかのコマンドをAppleScriptで使ってみる。
-- Google Chromeの1つ目のウィンドウのプロパティを取得
tell application "Google Chrome"
get properties of window 1
end tell
------------------------------
-- 結果
{zoomed:true, miniaturized:false, name:"\"applescript\" commands - Google 検索", active tab:tab id 132 of window id 4 of application "Google Chrome", mode:"normal", miniaturizable:true, class:window, closeable:true, resizable:true, visible:true, zoomable:true, id:4, bounds:{50, 22, 1213, 796}, index:1, active tab index:13}
-- 1つ目のウィンドウの5つ目のタブのプロパティを取得
tell application "Google Chrome"
get properties of tab 5 of window 1
end tell
------------------------------
-- 結果
{name:"dojo/regexp - DojoCampus - Docs", URL:"http://docs.dojocampus.org/dojo/regexp", id:13, class:tab, loading:false}
-- タブに対するコマンドの一部 tell application "Google Chrome" view source of active tab of window 1 -- ソースを表示 save active tab of window 1 -- 保存 print active tab of window 1 -- 印刷 reload active tab of window 1 -- 再読み込み go back active tab of window 1 -- 戻る go forward active tab of window 1 -- 進む copy selection of active tab of window 1 -- 選択したテキストをpasteboardへコピー paste selection active tab of window 1 -- pasteboard内のテキストをタブに(テキストエリアにフォーカスがある場合はそのエリアに)貼りつけ end tell
コマンドを組み合わせることで、Google Chromeの操作を自動化出来る。
tell application "Google Chrome"
set aWin to make new window with properties {mode:"incognito"}
-- シークレットモードでウィンドウを作る; 通常モードは {mode:"normal"}
tell aWin
set newTab to make new tab with properties {URL:"http://www.facebook.com/"}
-- Facebookを新しいタブで開く
tell active tab
repeat -- ページのロードを待つためのループ
set curStat to loading
if curStat = false then exit repeat
delay 0.1
end repeat
end tell
end tell
end tell
AppleScriptのコードはアプリケーションとして保存出来るので、作ったアプリケーションのアイコンをダブルクリックしてGoogle Chromeに自動作業をさせる、ということも出来る。
追記: Google Chromeを操作するAppleScriptの中にJavaScriptを書く - DiaryException







