This Week's Sponsor:

Winterfest 2024

The Festival of Artisanal Software


The Ultimate Introduction to Applescript, Part 2: Scripting Concepts and Samples

Applescript, that amazing resource many Mac users ignore because they think it’s too complex. Guys, Applescript is an amazing tool to get things done automatically, though I recognize many of you just don’t want to get started because you don’t have the right motivation, or you just don’t know enough about it. I wrote a post which collected the best examples of Applescript but still, there wasn’t a real introduction.

That’s why I asked my friend Jesse from iScript Netcast to get started on a posts series about Applescript. This is the second part of the series, be sure to read the first part “The Ultimate Introduction to Applescript” if you missed it. You can also get in touch with iScript Netcast on Twitter here.

Enjoy!

The First Steps

When learning any programming language, and especially when learning AppleScript, there are a few concepts which you must understand to go beyond simply telling applications what to do and actually writing a script that does something useful for you! These concepts are the building blocks of every script you write and before long you will be using them like second nature.

The first concept to be aware of when scripting is the command. Commands are english words that actually perform the actions for a script. Commands can take the form of single words like “play”, “pause”, or “activate” or they can be longer and more complex, “make new folder on the desktop”. Currently, OS X can handle 10,000 AppleScript commands per second. This is fast enough that even though AppleScript is an interpreted language you will hardly ever notice.

The second concept that is vital to AppleScript and most any programming language is variables. Variables are words or small phrases that the programmer comes up with to store values that are often the result of commands, the responses of applications. Variables are valuable to programmers/scripters because it allows them to interact with data and to manipulate it within scripts.

The final concept we will discuss in this post is the concept of code blocks. These exist all over AppleScript in forms that you will come to know as “tell blocks”, “if-then” statements, “repeat” loops, and many others. These “blocks of code” allow you to organize and group your statements together so that your code will be executed in the proper logical order as well as make it easier to read.

Your First Script

One of my first scripts worked with iTunes. I had created a smart playlist that would list all the songs that had a played count of zero. Essentially the list showed all of the songs in my iTunes library that I had never listened to. However, as I started listening to the songs in the list I realized that there were a number of songs that I had already listened to. Thus, I decided to set out to write a script that would allow me to change the played count of a track from zero to one. This would then remove the track from my smart playlist.

Your first script should be something similar. Do not try to set out to concur the internet or script an entire workflow or you will become discouraged during your learning process. Take scripts one at at time. As you get better you can begin combining scripts to have more and more functionality. Eventually you will be writing scripts that have lots of functionality or even stand on their own as full-featured applications.

Today though, we will take a quick peek at my first script. You can setup the same situation yourself and then see how the script works. *NOTE: If you write this script exactly as it is below it will remove ALL tracks from your playlist.

The Code

Applescript Sample

Applescript Sample

Explanation

Essentially, this is how the script works. In the first line you are creating a tell block where you will be interacting with the application “iTunes”. Then you are creating a variable called theTracks where you store a list of the unique ids for every track in the playlist called “Not Listened To”. (Hopefully you created this playlist as I described earlier).

Next, a repeat loop runs through each item in the list of ids, one at a time, and allows you to run more commands for each item. The command we are running on each item (designated as a new variable named x) is to set the played count to 1. This effectively makes the track no longer match the requirements for the smart playlist and removes it.

That is all for this week. Next time we will take a look at a few more concepts in AppleScript that will allow you to take your scripts beyond a single tell block and deeper into scripting your favorite applications as well as creating stand alone scripts that are almost self contained applications.

Access Extra Content and Perks

Founded in 2015, Club MacStories has delivered exclusive content every week for nearly a decade.

What started with weekly and monthly email newsletters has blossomed into a family of memberships designed every MacStories fan.

Learn more here and from our Club FAQs.

Club MacStories: Weekly and monthly newsletters via email and the web that are brimming with apps, tips, automation workflows, longform writing, early access to the MacStories Unwind podcast, periodic giveaways, and more;

Club MacStories+: Everything that Club MacStories offers, plus an active Discord community, advanced search and custom RSS features for exploring the Club’s entire back catalog, bonus columns, and dozens of app discounts;

Club Premier: All of the above and AppStories+, an extended version of our flagship podcast that’s delivered early, ad-free, and in high-bitrate audio.