This is the first of what will hopefully be a series of tutorials on Apple’s own scripting language Applescript. I should perhaps begin with a confession, “I’m not a programmer”, by training I’m a chemist and I spent the majority of my career as a Medicinal Chemist. This might actually make me the ideal person to write this since I’ve always thought of Applescript as the programming language for the rest of us. Applescript is a scripting language that allows users to automate reptitive or complex tasks, or customise applications and as such it is really useful for little tools or widgets that make life easier. An added benefit is that it easy to read Applescript code, so the many free applescripts that are available make an invaluable resource for beginners.

Getting Started

One of the beauties of Applescript is that you don’t have to install anything, the standard Mac OS X install includes everything you might need to get started. If you look in the Applications folder you will see a folder called Utilities, in there is the Script Editor application. Doubler click on it to open it and you should see a window like this.

Your First Script

In the text box type

Then click on “Compile”, then on “Run” and you should see a dialog box pop up as shown in the image below. Click on “OK” when you are finished admiring your handiwork. Click on the “Description”, or “Event Log” tabs at the bottom of the Script Editor window and rerun the script to get more information of what is actually happening. If you want you can save the script for posterity :-).

And now for something more interesting.

The other button on the Script Editor window is “Record” this allows you to record a set of actions. Open a new Script Editor window, click “Record” and then double click on the Macintosh HD icon on your desktop, then double click on the “Applications” folder. Now click on the “Stop” button in Script Editor. You should find that it has kept a record of your actions.

end tell If you now click “Run” it should open another Finder window and navigate to the “Applications” folder. We can now save this as an application, Choose “Save AS” and then select application as the File Format, check “run Only” and uncheck “Startup Screen”, then save it to your desktop. Now double click on the “App_Folder” icon and a Finder window should open displaying your applications folder. You could put this in the “Dock” for easy access, obviously a shortcut to the applications folder is not very useful, but you could use it to navigate to a deeply buried folder. There are simpler ways to write this but this works!

Checking to see if a folder exists, and if it does not create one.

So navigating to an existing folder is easy, what happens if the folder does not exist, Especially if the rest of the script relies on the presence of a specific folder? Well we can get Applescript to check if a folder exists and if not create one. Suppose we need a folder called “TIFF Images” in the users folder. First we define the name of the folder we need to check for “TIFF Images”, then since there may be multiple user accounts we get the path to the current user folder and set this to the variable “this_folder”. The display dialog is not really needed but it serves to show the format of paths in Applescript, in particular the use of “:” as the separator. We could hardcode the path into the script as shown on the next line (the — means this is a comment only, and will be ignored when the script is run) but then it would only work in the defined user account. Next we tell the “Finder” if the folder “TIFF Images” does not exist then make a folder called “TIFF Images”

You will quickly learn that there are many ways to achieve the same action in Applescript, for instance, this also works.

Bossing Applications

Whilst we don’t usually think of the “Finder” as an application it really is and as we have seen we can control it with Applescript, many applications support Applescript to a greater or lesser extent. Lets see what we can do with Mail, the applescript below uses a dialog box to capture user input and then puts it into a new mail message. So the first part opens up Mail, activate brings it to the front. We are now using the display dialog to get user input, the text result of the input is put into the variable thetext. Mail is than told (we are still in the “tell application Mail” block) to make a new message with the content set to thistext. If the user does not type anything into the dialog box then the text set in the property at the start of the script is used instead.

The syntax for creating the mail message is not that intuitive, and this is probably a good point to look at the scripting dictionary. In Script Editor under the “File” menu you will find “Open Dictionary”, click on this and in the dialog box that opens navigate to Mail and click on “Open”. You should see the display below.

If you select “Mail” in the first panel and “outgoing message” in the second you can see the applescript properties of an outgoing email message. So we could have set all the properties of the email using applescript as so.

You then need to tell the new message to add a recipient.

Last updated 6 March 2023

Related Posts

One thought on “Getting started with AppleScript

Comments are closed.