Learning AppleScript can really pay off.
It isn’t always the easiest language to work with, as anyone who’s ever used it will tell you. Commands that look like they should work sometimes don’t, you occasionally have to use bizarre methods to refer to that thing right there. It’s not a picnic.
But I’ve been using it for a little while and am pretty comfortable with it. I’ve ended up using it more and more for everyday tasks, or one-off situations that would usually require a lot of donkey work.
Let me share one that’s just happened.
I was browsing the New Yorker’s print shop and — I thought — was opening up the individual pages for covers I liked and wanted to go back to after I’d finished browsing.
It turns out that I was just opening up the preview images in tabs. Whoops. And their URLs don’t contain a unique code that’s shared with their purchase pages. Ah. And there are 10 of them. Erm.
The one similarity they do have is that the filename is kinda sorta repeated on the purchase page, but it’s hyphen-separated so (I thought, wrongly) you can’t just copy and paste it into a search field.
So I cooked up this little script in about a minute:
applescript: tell application "Safari" set theURL to (get the URL of the front document) end tell set printsOffset to (get the offset of "/prints/" in theURL) set jpgOffset to (get the offset of ".jpg" in theURL) set hyphenString to characters (printsOffset + 8) ¬ through (jpgOffset - 1) of theURL as text set AppleScript's text item delimiters to "-" set theWords to (get the text items of hyphenString) set AppleScript's text item delimiters to " " set searchString to theWords as string set AppleScript's text item delimiters to "" -- return to default tell application "Safari" tell the front document search the web for searchString end tell end tell
Then I saved it, gave it a shortcut in FastScripts, opened up the first tab in Safari, hit the shortcut, tabbed to select the first URL in the search results, then hit enter to load it. Rinse, repeat.
As I was writing this I tried just pasting the filename into Google and — shocker — that works. Which makes me look a little dumb.
And the script isn’t even that sophisticated; I could have used a repeat command to loop through each tab, for example.
However my wider point about getting your teeth into automation still stands. I’ve written loads of little scripts like this over the past few months and, combined, they’ve saved me bags of time and plenty of frustration.
Hell, the InDesign script I wrote to set a text frame’s name (≥CS5) to its script label (≤CS4) alone saved me half an hour.
The problem of AppleScript being frustrating to learn — and continually frustrating with certain applications’ opaque AppleScript dictionaries and command parsing — does stand in the way of more people using it (which is why Automator exists, as limited as it is).
But if you’re tempted, even a little, give it a go. If you don’t have any programming experience I recommend AppleScript 1-2-3. (Don’t buy the Kindle edition. You want the paper copy.)
Also make sure to check out Script Debugger 5, which at $199 (about £170 including VAT) is a bit pricey if you’re just starting but makes things so much easier it’s a joke. It’s like switching from MSPaint to Photoshop. You’ll never go back to (Apple)Script Editor.
“Learning AppleScript can really pay off. Once you’ve suffered through its oddities enough to be able to write it without weeping, you’ll be tempted to write scripts that achieve things you can do with copy and paste, like this one.”
Maybe I should have thought harder about that first line.