getting to grips with AppleScript
Learning AppleScript is probably the second most productive thing you can do (the first is learning the Terminal) to improve your Mac experience. You know all those utilities that you see in the Mac App Store, on MacUpdate and so on, with developers charging anything from 99 cents to $20? Well, many of those are just doing simple jobs that you can actually script yourself for free with a little learning of OS X’s unique scripting language (and, indeed, some of those apps have been built in exactly this way).
To keep this post short and practical, I’m going to leave aside the wider discussion of what AppleScript is (and isn’t), what it can (and can’t) do and all manner of other interesting but theoretical things, and instead give you a taste of what you can do with it. I’ll give you some references at the end where you can find out more and learn everything you need.
Let’s get straight to it. Open up the AppleScript Editor by opening the Spotlight search bar and typing
Apples. Hit ‘return’ and you’ll be faced with a new editor window. Let’s type something in it.
tell application "Finder"
display dialog "What's your name?" default answer "" with icon note
set myName to the text returned of the result
display dialog "Hi there, " & myName & "! Welcome to AppleScript!" with icon note
You could just copy and paste this into the editor, but I’m going to recommend that you don’t. There’s a good reason to take the tedious route and type it in yourself. Like learning a human language, learning a computer language requires using it, and using it repetitively. As you type in the language, you’ll get a feel for its syntax that you won’t get if you just copy and paste. And, unlike a real human language, learning a computer language’s syntax is pretty much the whole battle of mastering it.
After you’ve finished typing, press ‘Command-K’ on the keyboard. If you typed everything correctly, you see the script change into a multi-colored jamboree, like this:
If you weren’t so lucky, examine what you typed against what’s on the page here. Part of the frustration of any computer language is rooting out typos! Eventually, you get a feel for it and start to learn where to look first, based on the error messages you see. For now, you’ll have to peck and hunt (if you get really fed up, you can always go the cut-and-paste route!).
Assuming you’ve got your script to compile, now it’s time to run it and see what it does. Hit ‘Command-R’ (you can of course use the icons in the toolbar for compiling and running, too) and you should get this:
So go ahead, type your name!
OK, you’re getting the idea. Let’s try something different. Press Control-N to open up a clean editor window and enter this:
say "What's your name?" using "Vicki"
display dialog "My name is " default answer ""
set myName to the text returned of the result
say "His name is " & myName using "Vicki"
say "Welcome to AppleScript, " & myName using "Alex"
As before, press ‘Command-K’ to compile and ‘Command-R’ to run.
As you can see (or hear!), you can even have your computer continue a dialogue (with or without you!) using OS X’s many voices.
It’s worth noting that this feature is extremely useful if you’re learning a foreign language.
The Voices options in System Preferences > Dictation & Speech | Text to Speech | System Voice include many optional voices that you can download that will speak foreign text. You can paste the target text into a dialog box (like the ones you just created), and then listen and practice your language skills as repetitively as you desire. For those wanting to learn Thai, for example, download the voice “Narisa”. You can paste Thai script from the web or from the Dictionary.app (if you have the Thai extension installed) into a dialog box and have “Narisa” say it in a very passable Thai accent. Great for learning!
One last one. How about your own screencapture program? Tired of remembering those shortcut keybindings, or having to fire up Preview or SnagIt for the occasional screen grab? Why not have your own app in the Dock that captures the screen with a single click? Here’s how:
Open a new editor window and enter this (in this case, you might want to copy and paste it, for reasons I’ll explain shortly):
do shell script "screencapture -x ~/Desktop/" & time string of (current date) & ".png"
Do the Command-K thing, but then this time do Command-S instead of Command-R. From the resulting save box, change the File Format near the bottom of the box from ‘Script’ to ‘Application’. Give it a fancy name (ScreenGrab, say?) and save it in your Applications folder.
Once that’s all done, go to your Apps folder, grab the ScreenGrab.app and drag it to the Dock. Clicking on it puts a timestamped screenshot on your Desktop. If you’ve got multiple spaces open, flip between them clicking the ScreenGrab app. That’s one easy way to get a record of your entire set up! Cool, huh? (Don’t forget you can easily change the icon in the Dock for something more to your taste, as I explain here. Also, if you don’t like the / delimiters in the file name, use this version for your app.)
That last little script demonstrates one of AppleScript’s most powerful features: the ability to run other scripts (and apps). The command in the last script (and the reason why I suggested you paste it) was actually a Bash shell command (aka Terminal command), and we know those are very easy to mistype! AppleScript can actually run the commands of many apps from within its own scripts, putting the power of those apps at your disposal (and that includes some apps you’re very likely familiar with, like Word and Excel).
I hope this short intro has given you a taste for exploring AppleScript and finding out more. It’s an incredibly powerful language that you can use to enormous advantage, and profit. In order to do that, you’ll need to go on a learning adventure, but I promise, the following sources will make that relatively painless!
If you’re absolutely brand new to AppleScript, then the must-have starter’s book is
Once you’ve got through that you’ll be able to pretty much teach yourself the rest, picking it up from sources like
Other references you should consult once you’re up and running are
and don’t forget Apple’s own free guide:
AppleScript Language Guide which you can also download as a PDF for offline use.
Finally, my Mac OS X Technologies User Tip contains some of the above links as well as others that may be of interest.
Happy scripting! 🙂
Featured picture: wall and paper stencil by -endlesshate
Posted on March 12, 2013, in AppleScript and tagged applescript, getting started, introduction, learning, Narisa, say, screen capture, screenshot, system voice, tell, Thai, voice. Bookmark the permalink. 2 Comments.
Hi. Your first example should not be enclosed within a tell application “Finder” block and does not need a delay.
The Finder tell block is necessary to get the “friendly-faced” Finder icon in the dialogue box. Without it, you’d get the rather plain and “unfriendly” Applescript editor icon. 😉
The delay is a matter of opinion but again, its about HCI (human-computer interaction). Delays can be useful not only to let the system do things before the next operation, but also for perceptual reasons. Programs can seem more “human” and “friendly” with short delays between dialogues.
As a general point, it’s perhaps worth mentioning that unlike programming back-end processes that the user never sees, scripting the UI needs to take into account both code efficiency and the user experience (“usability” in a broad sense). After all, if people don’t like interacting with our software, they might decide not to use it, which will make our code about as ‘inefficient’ as any code could get, no matter how fast it runs. 😉
Thanks for your comment and interest. The more interest I can get in topics like this, the more I’ll post on it. 🙂