Newbies Guide to Applescript

11 February 2008 · 1 comment

Why use a Mac and do all the work yourself? In this Newbies Guide Miraz Jordan opens the door to making life easier with Applescript. [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 [...]

 

Why use a Mac and do all the work yourself? In this Newbies Guide Miraz Jordan opens the door to making life easier with Applescript. [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

Applescript tells the application Finder to pay attention.

Screenshot 1: Applescript “tells” the application “Finder” to pay attention.

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.

Save now

Open hundreds of folders with one Applescript.

Screenshot 2: I can just as easily open hundreds of folders with one Applescript.

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.

Applescript Allsorts

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.

Choose one of the example scripts.

Screenshot 3: Choose one of the example scripts.

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.

An application

Save a script as an application.

Screenshot 4: Save a script as an application for ease of use.

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

Assign a Keyboard Maestro keystroke.

Screenshot 5: I’ve set a keystroke for my script by using Keyboard Maestro.

Use a macro application like Keyboard Maestro or iKey to set a custom keystroke to run your new application.

Use a launcher

Launch a script with Launchbar.

Screenshot 6: A couple of keystrokes locates and launches my script with Launchbar.

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.

Similar launcher utilities include Butler and Quicksilver.

Run a script from iCal

tell application "Finder"
    display dialog "Subscribe to the free weekly MacTips."
end tell

Set a script as an Alarm in iCal.

Screenshot 7: I set a reminder script as an Alarm in iCal.

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.

iCal launches a script as an Alarm.

Screenshot 8: iCal launches my script as an Alarm.

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.

1 comment

These Posts may interest you too:

  1. Applescript 102 Applescript 102 Mac Tip #215/05-October-2005 In the last Tip I introduced you to making an Applescript, in order to save time and effort. First you’ll want to save the script...
  2. Applescript 101 I’m spending a lot of time these days thinking about the organisation of this website. It has three distinct areas: blog, tips and learning centre. All three areas contain articles...
  3. Applescript 103 Applescript 103 Mac Tip #216/12-October-2005 The last couple of Tips have aimed to make you aware of Applescript and how useful it can be for a Mac user. I gave...
  4. Applescript to open a project I recently realised that if client X asks me to edit paragraph 3 on services.html I have to open my Client X folder on my computer, my Client X email...
  5. iTunes alerts Like to use iTunes tracks as alerts for meetings and appointments? Apple has instructions for iCal v1.5.1 to execute scripts as an event action. As with any iCal event, scripts...

Tell us what you think.
Note: there may be a delay before your comment appears. I now approve all comments from new visitors, in an attempt to keep spam at bay.

Add your Comment

{ 1 trackback }

Previous post:

Next post: