The Computer Oracle

Automated Interaction with Google Chrome

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping

--

Chapters
00:00 Automated Interaction With Google Chrome
00:57 Accepted Answer Score 8
02:17 Thank you

--

Full question
https://superuser.com/questions/310191/a...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#macos #googlechrome #applescript #automator #bashscripting

#avk47



ACCEPTED ANSWER

Score 8


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]