2010-10-23

Google Chrome meets AppleScript

Google Chrome ver.7 or later can be controlled by AppleScript.

Firstly, I D&Ded Google Chrome's icon on AppleScript Editor's icon. So AppleScript Editor displayed a list of commands and these descriptions in Google Chrome.



I tried to use some commands.

-- get properties of Google Chrome
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}

-- get the number of tabs of window 1
tell application "Google Chrome"
 get count every tab of window 1
end tell

13

-- get properties of fifth tab of first window
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}

-- get URL of active tab
tell application "Google Chrome"
 get URL of active tab of window 1
end tell

"http://docs.dojocampus.org/dojo/xhrGet"

-- control tab using some commands
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
 paste selection active tab of window 1
end tell

tell application "Google Chrome"
 set aWin to make new window with properties {mode:"incognito"}
 -- open window with secret mode, if with normal mode then use {mode:"normal"}
 tell aWin
  set newTab to make new tab with properties {URL:"http://www.facebook.com/"}
  -- open new tab and set URL
  tell active tab
   -- set URL to "http://www.facebook.com/"
   -- can also set URL here
   repeat  -- wait completion of loading
    set curStat to loading
    if curStat = false then exit repeat
    delay 0.1
   end repeat
  end tell
 end tell
end tell

AppleScript code can be saved as an application or used in a shell with osascript. So I can control Google Chrome automatically.

In Terminal I got selected text on Google Chrome and translate it into Japanese with Google Translate command-line tool.

$ osascript -e 'tell application "Google Chrome" to get copy selection of active tab of window 1' ; pbpaste
The Developer Community is more than the sum of software written, it is also the conversations that the community has; what is important, what to do next, how to improve the old.
$ osascript -e 'tell application "Google Chrome" to get copy selection of active tab of window 1' ; pbpaste | translation 
開発者のコミュニティが作成されたソフトウェアの合計よりも、それはコミュニティが持っている会話もある。何が、重要ですが、古いものを改善する方法を次に何をするか。

In Emacs, if I want to insert selected text in Google Chrome to current buffer, I can use following command::

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

Or, in Vim::

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

Excursus: AppleScript and JavaScript for Google Chrome

Thanks.

6 comments:

  1. Thanks for the post, the examples are great. Was able to make a Keyboard Maestro macro that waits exactly until the current tab is done loading then executes the rest of the Macro.

    set isloading to 1
    repeat while isloading is not false
    tell application "Google Chrome"
    set isloading to loading of active tab of window 1
    end tell
    end repeat

    ReplyDelete
  2. Wow, is there ANY way to get the source of the current tab? Is refusing to work in Chrome, grr. Even with UI scripting it's not playing nice.

    ReplyDelete
  3. Thank you for nice macro.

    I see we can get source of a tab on current Chrome with view source, select all and copy selection .

    tell application "Google Chrome"
    view source of active tab of window 1
    repeat
    set curStat to loading of active tab of window 1
    if curStat = false then exit repeat
    delay 0.1
    end repeat
    select all of active tab of window 1
    copy selection of active tab of window 1
    end tell


    It may be that its scripting interface becomes more good later because Chrome is supported by opensource project.

    ReplyDelete
  4. Is there a way to get the output of the javascript command?

    ReplyDelete
  5. arigatou gozaimasu :)

    ReplyDelete
  6. How to open the Chrome using AppleScript ?

    ReplyDelete