First published February 2006, and written for Mac OS X 10.4, Tiger. Leopard introduced some new features. Watch for an update.
Hiding away inside the Applescript folder in your Applications folder is a piece of software you may never have even considered using. Its name is Script Editor and it may be more useful than you’d think. Its biggest problem is that unless you already know what to do with it you won’t get far by experimenting. Type in some text and press Run and all you’ll see is a meaningless Syntax Error message.
Tell an application to get to work
Script Editor allows you to write sets of instructions for your Mac to carry out on your behalf. This might be something simple, such as opening several web pages or several folders, or an extremely complex sequence of hundreds of actions. In this Guide we’re going to tell the Mac to do some work for us.
Make a new blank Script Editor document (File menu > New) and enter these two instructions on separate lines. Don’t forget the double speech marks around the word Finder. Then press the Compile button.
tell application "Finder" end tell
This snippet of code “tells” the Finder to listen out for instructions and also tells it when to stop listening. All we’ve done so far is tell it to start listening and to stop listening. That’s not specially useful, but it’s a starting point.
Let’s tell Finder to open the Documents folder. In the example below my username is
miraz; you need to replace that with your username. Notice how all the folder names are separated by a colon (
:) but no spaces, and don’t forget those speech marks.
tell application "Finder" open "Macintosh HD:Users:miraz:Documents" end tell
Now go to the Script menu and choose Run (or type Command R or click the Run button on the Toolbar) and watch your Documents folder spring open.
Choose Save from the File menu and save the script somewhere in the script format. I have a folder named Scripts in my Documents folder to keep all my Applescripts together. Once saved, you can easily run it — sometimes using other utilities so you can trigger a script of dozens or hundreds of actions with one keystroke.
You can do more than open folders; you can open files, web pages and applications, too, as this example shows.
tell application "Finder" open "Macintosh HD:Users:miraz:Documents:writing:tips" open "Macintosh HD:Users:miraz:Documents:clients:TimeSheet.oo3" open location "http://oddity59.geek.nz" open location "http://mactips.info" open "Macintosh HD:Applications:Dictionary.app" open "Macintosh HD:Applications:iTunes.app" end tell
See more examples
Hidden away on your Mac are heaps of example scripts. The easiest way to find them is to install the Script menu. If you use Tiger go to the Applescript folder inside your Applications folder and double click the item called AppleScript Utility. Check the box to Show Script Menu in Menu Bar and Show Library Scripts. For older operating systems double click the Install Script Menu item in the Applescript folder.
Now you should see a scroll-shaped icon in your Menu bar. Click on it and you’ll see a list of ready-made Applescripts. They may be inside folders named for the Applications you have installed.
Have a good look around that scripts menu. You may find all kinds of handy things you didn’t know existed. Try connecting to the Internet, opening Safari and choosing one of the scripts under URLs. You should go straight to the web page you chose.
If you like the script but want to go to a different URL then you should be able to see how to edit it. [Quick trick: to edit one of those scripts hold down the Option key while you choose it from the menu. It opens right into Script Editor.]
Even more scripts
Many software manufacturers and enthusiasts create scripts — just try Googling for what you want. Most scripts come with instructions about how to install and use them.
If you enjoy using iTunes then you’ll find hundreds of very useful scripts at Doug’s AppleScripts for iTunes.
Automate it even more
It may seem pointless to create a script if you still have to find the script, open it and choose Run just to make things happen. There are several ways you can shortcut that process.
Save the script as an application and in future double click it to make it run. Keep it handy on your Dock. Go to the File menu > Save As and choose Application from the File Format pop-up.
Set a custom keystroke
Use a launcher
Software such as Obdev’s Launchbar make it extremely easy to run a script. Just hit your chosen key combination to call up Launchbar, type a few letters from the name of the script and hit Return. Because Launchbar automatically finds new files and learns your keystrokes this is a very efficient way to run scripts.
Run a script from iCal
tell application "Finder" display dialog "Subscribe to the free weekly MacTips." end tell
iCal can run an Applescript as part of its Alarm function. You could make a script to remind you to buy gift magazine subscriptions for all your friends and set iCal to run that script at a certain time.
Open iCal and make a new appointment. Under Alarm choose Run Script. Click on the line below that and choose Other… and locate your script. Set the time for the alarm.
Keep it simple
Applescript has a mystique about it. There’s an idea we should be writing huge, arcane scripts for enormously complex tasks. That’s just not so. A script can be tiny, but make life easier for you.
I backup to an external firewire drive called “Pleiades”. When the backup is finished I need to Eject the drive. I could click on it and choose Eject from the File menu, but instead I use this script:
tell application "Finder" eject disk "Pleiades" end tell
I have versions of this to eject a compact flash card with photos and my iPod. The trick is to insert the card or the iPod and see what it’s called and then use that in place of “Pleiades” in the script above. I call these scripts from Launchbar with a keystroke. It’s quick and easy; no fumbling required.
Power without end
Applescript is very powerful. You can use it to control a great many applications. It can take data from here, manipulate it, add it to there, clean it up, put it elsewhere. You can create very complex and sophisticated routines. You can make it your life’s work.
Or you can make the most of a few very simple scripts to make your day to day life just a bit easier. It’s up to you.
First published in Macguide magazine Issue #25 January / February 2006 and republished with permission. This article may have been modified from the original.