Learn To Speak AppleScript, Part 2: Baby Steps

Click Here To Read Part One of “Learn To Speak AppleScript”


To recap the end of Part One, your goal will be to build a script that will save your emails as text files. The actual programming will be covered in Part Three and, when we get there, I’ll try to stay more “conceptual”.

My goal will be to give you a feel for how an AppleScript does its job — but still include enough real-life code to keep it interesting for more experienced scripters (or for people who came here from a search engine looking for a bit of code to get their scripts up and running!)

But, if you are a beginner, don’t let those computer code snippets intimidate you! They are included primarily so that you can see what a real-life Script looks like and not with any expectation that you master the intricacies of the AppleScript language.

After all, this is really an exercise in teaching you the fundamentals of thinking and speaking AppleScript. In other words, this is going to be more like an English class in literature where we talk about books and what they mean — and not a grammar lesson.

In fact, that metaphor is actually a pretty good way to get started!

The First Step Is Between Your Ears

As I mentioned in Part One, AppleScript is an “English-like” programming language.

What we are going to do in this lesson is to state our goals as clearly as we can — and do so in plain, everyday language. Once we’ve done that, we’ll set about writing an AppleScript that matches up with that description.

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

Writing a good AppleScript is like…. (wait for it) …. writing a good anything!

Most things that are well-written have probably started from a place of being well-thought-out, right? That’s not to say that you won’t discover some fun, unexpected stuff along the way — but actions begin with intentions. My experience is this: The clearer you are about your intentions when you start, the more you’ll make the right choices along the way.1

And the more likely you’ll end up with a working thing at the end of your journey.

The Goal of the Script (In Plain English)

First, we need to state our overall goal as clearly as we can. For this first sample script, that goal is:

“I want a script that will save emails as text files.”

The Basic Elements We’ll Need In the Script:

Now that we know what we want to do, we’ll flesh out the details more fully:

  1. I use the Apple Mail application to read my emails.
  2. I want to be able to save emails as text files.
  3. I want to be able to save more than one email at a time.
  4. I want to be able to control where the text files are saved to.
  5. I want the new text files to have distinctive names so that I can tell them apart more easily.
  6. I want the script to come up with those names so that I don’t have to do it for each file.
  1. I’d like to welcome everyone to the Veritrope.com “Den of Zen”… []

10 Responses to “Learn To Speak AppleScript, Part 2: Baby Steps”

  1. up0 March 8, 2010 at 1:59 pm #

    great analogy. Applescript is like learning to talk in some kids’ language…

    • Justin March 8, 2010 at 4:08 pm #

      Glad you enjoyed it… Part 3 coming soon!

  2. Rubarb April 7, 2010 at 1:56 am #

    where is part 3?

    • Justin April 7, 2010 at 2:10 am #

      I was hoping that there was someone out there who cared enough to ask — “Rubarb” is officially Veritrope’s “Reader of the Day”! 😀 ((Don’t Worry: I won’t put your picture up on a special plaque or anything!))

      Part 3 was slightly derailed by both a bad internet connection (not a surprise if you’ve seen the front page lately) and also some needed bug fixes to one or two of the AppleScripts I publish here.

      I’d say look for it by the end of this week… Fair enough?

      Are you enjoying the approach? Finding it helpful?

      • Rubarb April 7, 2010 at 2:16 am #

        Well, I like the idea of a plaque 😉

        Parts 1 and 2 are great to read! Very involving and engaging… Love to find more info about Apple Script! I’m new to this site so am looking forward to exploring more.

        Thanks Justin!

        R

        • Justin April 7, 2010 at 2:51 am #

          Just checked out your link — must be serendipity as I am a many-time-customer of macProVideo (I **KNEW** I liked you guys for a reason — you have excellent taste in websites! 😉 ). When my clients are looking for an in-depth training series, I often recommend your stuff as well.

          Anyhow, it had been a while since I checked in — saw your Advanced Snow Leopard tutorial on offer… Looks good! Will certainly scoop that up along with a few others once you guys release your iPad app! (wink-wink, nudge-nudge)

          If I can get Part 3 out the door this week, I’ll also try to post some Services for Snow Leopard that I use privately, but haven’t published yet. Here is a link to the Services that I **have** already published…and also a directory of others I have found on my internet travels. (Based on your tutorial, I thought you might “be feeling it”.)

          And now to find a plaque….

          • Rubarb April 7, 2010 at 4:25 am #

            Hi Justin,

            Thanks! We do indeed have impeccable taste in good websites… as you do have excellent taste in good training videos 😉

            Indeed, I am Rounik… and the Advanced Snow leopard tutorial is my first full foray into the wonderful world of online video tutorials.

            You can already use NEDi (for iPhone/Touch) on the iPad… but of course users can then watch the library of videos in iPhone native res or upscaled x2… I haven’t had a chance to test it out as the iPad hasn’t arrived this side of the pond yet in the UK, or indeed in Canada… 🙁

            But, until NEDi for iPad is released that’s the best solution.

            I’m looking forward to Part 3. I genuinely love your writing style. Part 3 will be great… (no pressure then 😉

            Yes, indeed, I pick up from the Mac OSX 101 tutorial and cover Automator and Services and the Terminal… So I’m downloading your freebees now. Many thanks!

            The kind words and link to my tutorial are better than any plaque could be!

            btw, if you’re interested in joining the macProVideo.com affiliate programme, here is the link:
            http://www.macprovideo.com/affiliates/

            If you’re ever around the forums on macprovideo pop in and say hi!

            Thanks again!
            Rounik