Automated Interaction with Google Chrome
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:
- Open a chrome window to a specific screen
- Point that window to a specific URL
- 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.
Solution 1:
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:
- Activate Chrome (either opens it, or if it's open, makes it the focus)
- Close any open browser windows
- 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)
- After each browser window is opened, send a "full screen" command (Command + Shift + F)
- 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]