DiaryException このページをアンテナに追加 RSSフィード Twitter

2005 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
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(日)

[][]Google ChromeAppleScript操作する

Google Chromeはver.7以降でAppleScript対応している。正確に書くと、Google ChromeプロセスにAppleEventを送ることが出来るようになった。

AppleScriptAppleScriptエディタを使って書いて実行するか、エディタで書いてosascriptコマンドで実行することが出来る。

つまり、他のアプリケーションやshellからGoogle Chrome操作する、Google Chrome情報を取得することが可能になった。

例えば、Google Chrome内で選択したテキストをそのままEmacsバッファに挿入したい場合、(AppleScriptを使わなければ、Google Chromeテキストを選択→Command-c→Emacsに移動→(kill-ringとpasteboardが連動していることを前提にして)C-yとする必要があったが)

Google Chromeテキストを選択して、

f:id:LaclefYoshi:20101024104951p:image

Emacs

C-u M-! osascript -e 'tell application "Google Chrome" to get copy selection of active tab of window 1' ; pbpaste

f:id:LaclefYoshi:20101024104949p:image

で出来るようになった。

f:id:LaclefYoshi:20101024104950p:image

ちなみに、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

で選択したテキスト翻訳文章が挿入される。

f:id:LaclefYoshi:20101024114020p:image



AppleScriptからGoogle Chromeに対して何が出来るかは、AppleScriptエディタGoogle ChromeD&Dすれば(もしくはAppleScriptエディタの[ファイル]-[用語説明を開く]からGoogle Chromeを選択すれば)、コマンド一覧と説明が見える。

f:id:LaclefYoshi:20101024110635p:image f:id:LaclefYoshi:20101024110636p:image

幾つかのコマンド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