AppleScript for Automators
I know this is hard to believe: I work with MacScripter and I don’t understand coding. At all. If you’re a scripter, your brain is not wired like mine. I look at an AppleScript and I just see a foreign language. I realize that you may look at it and see a beautiful poem. It’s making me cry right now. Not the poem, but the fact that I don’t get it.
For those like me, Automator feels like a Magnetic Poetry Kit with whole stanzas already assembled for us. If we piece a few of these together, we’ve got eloquence too. Greeting-card-like eloquence, but eloquence none the less. Now we non-scripters can program too. Scary thought, huh?
Well fellow Automator, since you’re here, I’ll have to assume you’ve picked up a few of the basics of Automator. You’ve popped a few Actions into a Workflow and managed to get it to do something. Now you’re looking to extend Automator before you get bored with it. Then go to Automator Actions.
You’ll find many additional Action here. But I also urge you to start using AppleScript and shell scripts. Why? Actions aren’t portable. Third party actions will need to be installed on any machine your Workflow runs on. But scripts, they travel with the Workflow. And they’re just as powerful as Actions. More powerful than actions really. That’s because most Actions are just hunks of AppleScript and UNIX code. Literally. The Action is just a package to a script. So you can actually use AppleScript to create PseudoActions that don’t even exist as Actions yet.
Don’t run away. By no means am I encouraging for you to learn AppleScript. Jees! That would mean learning a new language. But you can learn (or steal) snippets of script just as you’ve learned French phrases, but never learned French. You know, like “that certain je ne sais quoi” or “we’re vis-a-vis here” or “pass the French fries.”
Start by stealing these scriptlets. We don’t mind. I’ve stolen most of them from some great sources here at MacScripter. I actually managed to figure out a few on my own too. Here’s a very simple one. One of the shortcomings of Automator is that certain things don’t seem automated. Let’s say I’ve created a Workflow that does a huge amount of file processing that takes forever. And let’s say that I want to remind myself that it takes forever when I run it. I could put up an Ask for Confirmation Action from the Automator Library that says “This process will take forever. Go to Starbuck’s and get a venti Hot Chocolate while processing.” Works fine, but requires the user to click on the OK button before the forever-taking action starts. If I go to Starbuck’s before I click OK, then the process sits ready to run and won’t start until I click OK. However I can Automate this process with a tiny scriptlet. Add a Run AppleScript Action from the Automator Library to your Workflow just before the forever-taking process. Paste in the following script where appropriate.
display dialog "TextGoesHere" giving up after 2
We’ll modify it so you can see what it does. The “–Pop Box” line is not even needed for the script to run. Anything with two dashes in front of it is just a comment. I put these in so later I’ll know what the scriptlet does, since I really don’t speak AppleScript. So the only real script is “display dialog “TextGoesHere” giving up after 2” This looks surprisingly like English, doesn’t it? That’s because AppleScript is designed to be written in plain English. “display dialog” tells your Mac to pop up a dialog box with the text in quotes showing and to give up on this after 2 seconds. So if we modify our script to:
display dialog "This process will take forever. Go out to Starbucks and get a venti Hot Chocolate while processing." giving up after 4
You get this on your screen when run:
It will stay on your screen for 4 seconds, disappear and continue to run the process without you having to click anything.
This PseudoAction is very simple. AppleScript can do a lot more. Try this one:
--Email current Web address to self
tell application "System Events"
tell application "Safari" to activate
tell process "Safari" to keystroke "i" using command down & shift down
keystroke "Web Address"
keystroke "d" using command down & shift down
keystroke "h" using command down
Say you’re at work and you’re browsing a really cool Froogle page of battery-powered socks at a killer price that you’d like to view at home. This script automates the entire process of sending the URL to yourself by email. Just change “YourEmail@YourDomain” to your email at your domain, pop it into a Run AppleScript action, save it as a Workflow or app, and you’re in business. When you’re at the PlanetElectricSock site, fire it up and forget it. As you can see by this, AppleScript can help customize Automator to your exact needs. Custom programming is probably the greatest strength of AppleScript.
I’d better note that this scriptlet uses UI Scripting, which you’ll need to turn on. UI Scripting greatly extends AppleScript by allowing you control to menu items, text fields, and buttons.
There’s a brief explanation here and a download for an Automator app that helps you turn it on. Incidentally the app is just a scriptlet too.
The possibilities of what you can do with scriptlets in Automator are as endless as AppleScript itself. On the MacScripter Automator Applet forum you’ll find some good starters with very useful scriptlets. Or if these aren’t what you’re looking for, go to the Automator forum and ask for help.