What Is AppleScript? Writing Your First Mac Automation Script

If you feel comfortable in the world of scripting and you work on a Mac, AppleScript might be the automating solution for you. AppleScript is a powerful language that gives you the power to control any app, as long as it provides an AppleScript library.

Use it for such mundane tasks as resizing Photoshop photos automatically, renaming folders, and locking files with a password. We’ll show you how to start using it.

What Is AppleScript?

Like bash, AppleScript is a scripting language. And similar to Automator, it interacts primarily with apps and Finder to automate tasks for you. It released as part of Mac OS System 7, all the way back in 1993. It’s stuck around since then, nestled in the Utilities folder.

AppleScript increased in power with the debut of Mac OS X. The Cocoa framework made it much easier for app developers to include AppleScript compatibility. That increased flexibility, combined with AppleScript’s ability to talk directly to the command line, makes AppleScript one of the best tools for tinkerers. It also gives macOS the edge over iOS when it comes to automation.

Overview of Pre-Installed AppleScripts

Before we get into breaking down exactly what an AppleScript says, let’s take a look at the scripts that come pre-installed with Script Editor and how you can use them.

The preinstalled scripts live in Macintosh HD > Library > Scripts. You can also access them by opening the Script Editor (search for it with Spotlight), going to Preferences > General > Show Script menu in menu bar, and then clicking the script icon that appears in the menu bar.

The menu bar item for Scripts Editor

You can simply run one of these script from the menu bar.

Let’s take a look at Folder Actions. A Folder Action is an AppleScript that’s attached to a folder. When enabled, the script will run on any file that is added to that folder.

If you go to Folder Actions > Attach Scripts to a Folder, a window popup will ask what kind of script you want to add to a folder. You can flip photos horizontally or vertically, duplicate them as JPEG or PNG, rotate them, or prompt an alert when a new item is added.

Pre installed AppleScripts

Once you’ve selected your script and the folder you want to attach it to, right-click on the folder itself. Go down to Services > Folder Action Setup, and make sure that Enable Folder Actions is checked. Then drag a file on top of the folder to see your AppleScript run.

Play around with the Scripts menu bar to get a sense of what else AppleScript can do for you. To take a look at what’s going on under the hood, go to the Scripts folder, right-click on any script, and open it with Script Editor.

Understanding the Tell Statement

New item alert AppleScript

AppleScript uses a human-readable syntax. This means that, compared with many other programming languages, it’s written in an understandable format. Because it uses full words and sentences to send commands, it’s easy to understand and straightforward to learn.

Let’s look at the beginning syntax of the add – new item alert.scpt in Folder Actions. This will give an idea of the most fundamental statement in AppleScript: the tell statement.

 on adding folder items to this_folder after receiving added_items
		tell application "Finder"
			--get the name of the folder
			set the folder_name to the name of this_folder
		end tell

A “tell statement” is composed of three parts:

  1. The word “tell”
  2. The object to reference (in this case, the application “Finder”)
  3. The action to perform (here, “set the folder_name to the name of this_folder”).

In layman’s terms, the tell statement above is saying “Tell Finder to use the name of the folder this script is attached to whenever the script asks for “this_folder”.

The purpose of AppleScript is to automate tasks for you by telling apps to perform tasks you don’t feel like doing yourself. Therefore, the “tell” command is essential. You can get far in the AppleScript world with “tell” alone.

Also note: the line that says --get the name of the folder is actually just a comment, telling the user what the script is doing at that moment. Comments are essential—not just for telling other people what your script did, but for reminding yourself.

Writing Your First AppleScript

Hello World dialog box

If you have some programming experience and are familiar with concepts like variables, do-while loops, and conditionals, you can get a lot out of AppleScript beyond the scope of this introduction. For now, we’re just going to show you how to create, write, run, and save a basic script:

  1. Create the script: Open the Script Editor and go to File > New.
  2. Write your script: The Script Editor window is divided into two halves. The top half is for entering your script; the bottom half will show you the output when you run it. Type: tell application "System Events" to display dialog "Hello world!". Then hit the hammer button in the menu bar right above the script to compile it. This will run through your script to check for syntax errors. If you receive no error dialog, and your script changes formatting and font, then it compiled successfully.
  3. Run your script: Next to the hammer button is a Play button. Hit that, and see what happens.
  4. Save your script: Now that you have a basic script, you can save it as an clickable application. Go to File > Save, and under File Format, choose Application. Now, instead of opening the Script Editor and hitting Play, you can simply double-click your script to run it. If you like to script in bash, you can use AppleScript to turn your bash scripts into clickable applications.

Save Script as an Application

With this simple syntax down, you can tell nearly any Mac app to do pretty much anything. To review the available commands for a given app, go to File > Open Dictionary and choose the application. From there, you can see all the available AppleScript commands.

Applescript commands dictionary for iPhoto

For Simpler Mac Automation, Use Automator

If programming gives you a headache, there are simpler ways to automate your tasks. Automator uses a friendly GUI and a simple interface to turn mind-numbing routines into one-click set-and-forget tasks.

While Automator is not as customizable or intricate as AppleScript, it is simpler and much harder to break. Take a look at some Automator workflows that will save you time if you’re interested.

Read the full article: What Is AppleScript? Writing Your First Mac Automation Script

Leave a Reply

Your email address will not be published. Required fields are marked *