Learn To Speak AppleScript, Part 3: Coding Like Picasso

Let’s Do This!

Okay… we’re properly situated, so let’s dip our toes in the pool and begin our first bits of actual coding!

To recap a couple of “AppleScript Rules” from Part 2:

Rule #1:
First, we say it in English…. then we say it in “English-like”.

Rule #2:
AppleScript is like an energetic child. It’s eager to please — but in need of clear, constant direction.

In English, the purpose of our script is to save selected emails from Apple’s Mail application as text files.

If you’ll remember, our English Language Goals for this script are for it to:

  1. Know which email (or emails) in Apple Mail we want to save.
  2. Let us decide where the text files are saved to.
  3. Save those emails as text files.
  4. Give those new text files distinctive names so we can tell them apart more easily.
  5. Come up with those names automatically so that we don’t have to do it for each file.

Applying Rule #2 (…With Some Help from My Friend Pablo)

Now is the time to start to translate the Engish into AppleScript.

Looking at the first item on our list, we’ll need to figure out a way to select all of the emails that we want to save as text files. Since you’re probably already in Mail and saying to yourself “I wish I could save these messages as text files…”, it probably makes sense for us to try to use Mail’s built-in ability to select messages.

Our first coding question becomes “Is it possible for AppleScript to know which messages are selected (i.e., highlighted) inside of Mail?”

The answer can be found in a number of ways: Looking through the AppleScript Dictionary for Mail, looking it up in an AppleScript book (like the excellent AppleScript 1-2-3 or many others that are available), or just with a quick Google search for something like “AppleScript Mail selected message”.

“Face-Saving” in The Age of the Internet

If we weren’t able to find an answer, that would be the time to ask in an online forum like MacScripter or Mac OS X Hintsbut not before we tried to search for the answer ourselves. The community has a tradition of helping newcomers, but it also has a tradition of tearing the faces off of people who couldn’t even be bothered to try to find the answer on their own… After reading the same basic questions asked by people like that over a span of years, some long-timers are a little…uh…. cranky. You’ve been warned! đŸ˜‰

Thankfully, our search confirms that it **IS** possible for AppleScript. Any of the above methods will quickly show you that Mail has a term in its dictionary called “selection” which, when you tell AppleScript to look for it, will return whatever you have selected inside of Mail.

Since the messages that we’ll select will vary each time we use the AppleScript, we’ll set up something called a “variable” that serves as a sort of placeholder name for the specific message or messages that are selected each time. Let’s give it a name like “theMessages” so that we remember what it is later on.

Referring back to “Rule #2” above, we need to be really specific with AppleScript. When we want to tell the Mail application that we are talking to it, we have to say so in no uncertain terms. Likewise, we have to tell Mail when we’re done telling it what to do.

Putting all of the above together, your bit of code that talks to Mail and finds out which messages are highlighted will probably look something like this:

tell application "Mail"
set theMessages to selection
end tell

The AppleScript above:

  • Tells Mail to listen for your instructions
  • Tells Mail “I want you to set something I am calling “theMessages” to the value of whatever messages currently selected. In other words, remember what the selected messages are… but from now on, we’ll just call them “theMessages” to save time. Capice?”
  • Tells Mail you’re done talking to it for now.

One More Example…

We’ll try to cross off one more item from our English language list for this lesson. The second item was “Let us decide where the text files are saved to.”

We’ll need to be able choose different Folders on our hard drive as locations to save the exported text files, so we should set up a variable called “saveLocation” to refer to whatever place ends up being chosen.

So — which program might we be able to ask to help us find and select a folder on our computer? Hmmm…. you think maybe “Finder” is a good bet as a program that can work with the files on our hard drive?

Using the same combination of Dictionary/Google/Book-fu as we did for the first bit of code, you’ll discover lots of examples where people use AppleScript to tell Finder to prompt you to choose a folder on your computer. Most will look something like this:

tell application "Finder"
set saveLocation to choose folder
end tell

The AppleScript above:

  • Tells Finder to listen for your instructions
  • Tells Finder “Prompt me to choose a folder and, when I do, we’ll just refer to the place that I picked as “saveLocation” from now on.”
  • Tells Finder you’re done talking to it for now.

Getting the flavor of this? In Part Four, we’ll finish the script — and show you some easy ways to give it some polish!