Scripting Google Chrome for OSX using AppleScript

When it comes to software tools, i like to spend my time bleeding on the edge, where possible. One of the downsides to this, however is when you hit the carotid artery and bleed your tech heart all over the place. Having recently experienced this problem while using the dev channel build of Chrome, i’m fairly cautious of all the tabs I have open and losing them was a (queue Elton John…) sad sad situation, to say the least.

More recently I became the custodian of an older generation MacBook Pro as a challenge to see how much of my life could transport over to OSX from Windows. One of the things I wanted to make sure of was that (at the very least) I managed to keep a backup of all my open tabs, should the shit hit the fan and a dev build of Chrome for Mac bit the proverbial. The nice thing about Chrome is that when your normal life is replaced with a gLife, you can sync all your bookmarks with big brother and they’ll keep them around for you to access anywhere.

So after a few tweets and pointers in the right direction, I learned that Mac has long had this thing called AppleScript – basically a language which can be used to automate any part of the operating system and programs which it hosts. After a lot of googling, reading help files and finding the incredibly useful Ukelele i managed to scrounge together the following script (saved here in case i ever need it again)

tell application "Google Chrome" to activate
tell application "System Events"
	-- bookmark all tabs
	keystroke "d" using {command down, shift down}
	tell application process "Google Chrome"
		repeat until sheet 1 of window 1 exists
		end repeat
	end tell
	
	keystroke ((current date) as string)
	
	-- tab to bookmark folder list
	key code 48
	
	keystroke "Other"
	key code 124 -- retain focus on "Other..." folder
	key code 125 -- down arrow for "Tabs" subfolder
	
	keystroke return
end tell

It’s not a highly dynamic or robust script, but then again nor does it need to be – it’s running in a controlled environment and does the job of what I need (near) perfectly well.

Scheduled an iCal event to execute the script at 1am, and hey presto i’m happy to be playing around on the edge once more.

Notice in a previous paragraph I said the script is near perfect. One of the limitations of AppleScript is that its higher-level functions require a library of commands to be built into any application you intend on scripting. Currently, the script library for Chrome is pretty average, so everything has to be done by simulating keystrokes. The only thing I can’t do (or at least couldn’t immediately see how to do) was to prune old backups – the only way I’ve found I could do this was to highlight the bookmark folder from the bookmarks bar and right-click to delete.

Either way, im happy with the result, and it’s another language I can add to my arsenal.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>