When most people hear “AppleScript,” they think automation--and with good reason. AppleScript lets you control Microsoft Word, Adobe Photoshop, FileMaker Pro, and hundreds of other Mac programs with just a few lines of code. But if you think of AppleScript as only a nerdy, workflow-automation tool, you’re missing out on a lot of power. Truth is, AppleScript has lots of hidden tricks and timesavers built in--and they all come for free with your Mac. Here, then, are ten of the most useful.
In your /Applications/AppleScript folder, there’s an icon named Install Script Menu. When you double-click that, you’ll get a new icon on the far-left side of your menu bar, containing hundreds of example AppleScripts. Not only that, but you can also edit the scripts that appear in this menu by adding or removing scripts from your /Library/Scripts folder.
The Script Menu contains tons of useful scripts and a few that are just plain entertaining. For example, try the Internet Services –> Stock Quote script to get a 15-minute-delayed quote for the stock symbol of your choice. Or use URLs –> Download Weather Map to see a full-color map of the weather patterns in the continental United States.
And when you’re done with work for the day and just need a break, choose Mail Scripts –> Crazy Message Text to automatically generate a new email message with a wild variety of different fonts, colors, and sizes.
As described above, you can add or remove your own AppleScripts from the various subfolders within your /Library/Scripts folder. But even if you never write a line of AppleScript code in your life, you can still use the Script Menu to open programs and web pages that you use often. Simply drag any file or folder (or an address-bar icon from Safari) into your Home/Library/Scripts folder. When you click the Script Menu again, your new icon shows up on the bottom, giving you a convenient way to launch virtually anything from the menubar -- not just AppleScripts.
If you happen to be one of the lucky programmers with pre-release access to Mac OS X Tiger (10.4), you can quickly perform calculations from any program simply by summoning Dashboard and using the calculator widget. That’s convenient if you’re working on your tax returns, for example, and you need to subtract your deductions from your income.
On the other hand, if you’re still using Mac OS X Panther (10.3), you can’t use a calculator widget. Instead, just type whatever calculation you want to perform (say, 100 + 33) inside the program you’re working in, and then select the calculation with your mouse. Finally, open the Application menu of whatever program you’re in and choose Services –> Script Editor –> Get Result of AppleScript. After a second or two, AppleScript performs whatever calculation you requested and places the result right in the document you’re working in. (Alas, this trick works in most common Mac OS X programs, but not all.)
If your computer is on any sort of network -- and you’ve turned on Personal File Sharing in the System Preferences > Sharing pane -- people on your network can open and add files to your Home/Public folder. Unfortunately, if someone drops something into your Drop Box subfolder (say, a report that an employee has just finished for you), you’ll have no idea that you just received a file over your network.
Luckily, AppleScript lets you set up a special notification service, which displays a dialog box whenever someone adds a file to a particular folder. If you’d like to know whenever someone drops something into your network Drop Box, just follow these quick steps:
That’s it! Now, whenever someone on your network drops a folder in your Drop Box, you’ll get a special alert box like the one shown here.
If you do choose to get your hands dirty writing AppleScript code, you’ll find that it can become pretty tedious. That’s because AppleScript (and your /Applications/AppleScript/Script Editor program in particular) has a habit of using long words like “application” and “dialog,” words that, unfortunately, AppleScript has no keyboard shortcuts for. Instead, Apple assumes you’ll take the time to write out the words yourself, interrupting your creative flow to do so. If you’re a speed freak, this is simply unacceptable.
To shorten the tedium (but not eliminate it completely) open Script Editor’s preferences window and click on the Editing button. Then turn on the Script Assistant checkbox, and restart Script Editor.
Next time you start typing a long word, you’ll see an ellipsis after the first few letters. That’s Script Editor’s way of telling you, “I think I know what you mean to type here -- let me complete the word for you.” To submit to AppleScript’s guess, just press Option-Escape. (If there are several possibilities to complete your word, you get a list; otherwise, Script Editor completes the word with no questions asked.) Using this trick, you only need to type “appli” (and then press Option-Escape) rather than typing all the letters in “application,” for example.
Say you have a really long script -- like the one from /Library/Scripts/Internet Scripts/Stock Quote.scpt. If you want to find a particular word or phrase in the script, you’d probably choose Edit –> Find –> Find.
But when you think about it, that method is pretty slow, and you’re stuck with a Find dialog box covering up part of your screen. Instead, it’s much more convenient to search from right within Script Editor’s toolbar, which you can access with a single click.
To add the Find field to your toolbar, open a Script Editor window and choose View –> Customize Toolbar. Simply drag the Find field anywhere you want on your toolbar, and the rest of the buttons make room for it. Then click Done to get rid of the customization sheet.
Now, to perform searches, just type your search terms in the Find field and press Return. (To find subsequent occurrences of the terms, just press Return again.)
When writing a script, it’s often helpful to read a program’s dictionary (its list of commands, which you open with File –> Open Dictionary). That way, you can check which AppleScript commands a program accepts, and thus write scripts that perform whatever actions you want them to.
The biggest problem with dictionaries, though, is that it can be hard to find a particular command in them. For instance, if you’re looking through Microsoft Word’s dictionary and want to find the definition for the Check Spelling command, it could take several minutes to locate by hand.
Instead, try this: open Microsoft Word’s dictionary and choose Edit –> Find –> Find. In the Find dialog box, type the name of the command you want to find in the dictionary (in this example, Check Spelling) and press Return. Finally, choose Edit –> Find –> Find Next. In a split second, Script Editor displays the dictionary entry for whatever command you entered with no manual searching required.
When you’re done writing some code in Script Editor, you have to save your script. Normally, you do that by choosing File –> Save, naming the file, and just clicking Save.
But next time you go to save a file, take a look at the choices in the File Format menu. If you pick Script (the default option), you’ll get a normal AppleScript file ending in .scpt -- the same sorts of files that appear in your Script Menu (see Tip One). On the other hand, if you choose Application, you’ll end up with a self-contained program, which is far more convenient for keeping in your Dock, since you can run the script with a single click.
And finally, choose Text for the File Format if you want to send your script as an email attachment. This format takes up only a fraction of the space of the other two, which means it’ll take less time to send. (Also, Text is the only format that Windows and Linux users can open, even though they can’t actually run the code.)
If you’re planning to distribute a script over the internet, it can be annoying to package the script with all the help files, email links, and other auxiliary files in a separate folder. In true Mac form, though, you don’t have to do that; you can include the files inside the script.
Why do that? For one thing, you don’t have to worry about files getting lost in the transfer. For another, people who download your script won’t even have to know that there are a bunch of support files included. Finally, it’s nicer to see just a single icon than a bunch of unnecessary icons strewn across your desktop.
When saving your script, simply choose Script Bundle for the File Format. Then, in the Finder, navigate to your saved script, control-click it, choose Show Package Contents, and burrow into the Contents/Resources folder. Deposit whatever files you want there, and then close the folder up. That’s it -- now the files are included with your script.
Although most people use Script Editor for writing AppleScript code, it’s by no means the only program for the job. In fact, if you install the Apple Xcode Tools, you get Xcode, a much more powerful programming tool, for free.
Xcode includes all sorts of neat features: line numbering, great customizability, and even a debugger in case your scripts misbehave. Plus, you can open your existing AppleScript files in Xcode without any extra work, so you have nothing to lose in trying it.
As you’ve seen here, you don’t even need to know how to program in AppleScript to take advantage of much of its power. Whether you use a Mac at the office or at home, hopefully these tricks will make your life just a little bit easier.
Adam Goldstein is the author of AppleScript: The Missing Manual and also a full-time student.
Return to MacDevCenter.com.
Copyright © 2009 O'Reilly Media, Inc.