Automated Interaction with Google Chrome

9,626

I figured it out!

Applescript is a great resource, and chrome has a dictionary (you can view it in the "Applescript Editor" and then select "Open Dictionary" and find Chrome in the list).

The Code:

set screenCount to 8
set screenWidth to 1950
set baseURL to "http://localhost:8000"

tell application "Google Chrome"
    activate
    repeat while window 1 exists
        close window 1
    end repeat
    repeat with x from 1 to screenCount

        set w to make new window with properties {bounds:{screenWidth * (x - 1), 500, 500 + screenWidth * (x - 1), 1000}}
        tell application "System Events" to keystroke "F" using {command down, shift down}
        set URL of active tab of w to (baseURL & "/" & "#" & (x - 1))

    end repeat
end tell

The script does the following:

  1. Activate Chrome (either opens it, or if it's open, makes it the focus)
  2. Close any open browser windows
  3. Create [screenCount] browser windows, opening them at a specific coordinate (the first two terms in the {bounds} are the x,y of the upper left. The second two terms in the {bounds} are the x,y of the lower right)
  4. After each browser window is opened, send a "full screen" command (Command + Shift + F)
  5. After each browser window is full screened, set the URL to the desired location.

Keep in mind that my URLs fit a specific formula (e.g. http://localhost:8000/#0) so it was easy to dynamically generate them. For anyone with randomly similar needs you can use this as a starting point.

Finally, as for the terminal requirement, you can run applescript on terminal using the following line:

osascript [scriptname]

Share:
9,626

Related videos on Youtube

slifty
Author by

slifty

CMU and MIT graduate, Open News alum, worked at the Internet Archive, Boston Globe, Mozilla. Currently a Corporate Overlord at Bad Idea Factory where I develop open source tools to help people thinking face emoji. A lot of what I do is related to journalism and media analysis.

Updated on September 18, 2022

Comments

  • slifty
    slifty over 1 year

    All right SU, I have a fun nut to crack.

    I have 8 screens set up in a line, all hooked into one beefy G5 running Snow Leopard (yes, 4 graphics cards; about 15,000 pixels wide!). I am setting up a demo which uses 8 Chrome browser windows full screened, each one pointed to a different URL. It is a pain in the ass to do this manually every time the computer is restarted.

    Given this set-up I want to write a script to automatically:

    1. Open a chrome window to a specific screen
    2. Point that window to a specific URL
    3. Full screen that window (command-F is the keyboard shortcut)

    I haven't used automator and I'm not particularly familiar with shell scripting, but I was wondering if there were any fancy suggestions from the crowd.

    Update: Even if I need to use Automator for the high level functionality, I'm ultimately hoping for a way to trigger the script from a command-line / script. I'd like to be able to turn on this demo remotely from a shell if needed.

    • James T Snell
      James T Snell almost 13 years
      I think Automater is a great place to start.. Though I too haven't used it that much. I used to use a product "Egg Plant Purple" that you may be able to use for this - Though I'd like to think there are far better options out there..