What Is AppleScript? Writing Your First Mac Automation Script
Whatsapp Pinterest
Advertisement

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
	try
		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 How to Turn Bash Scripts Into Clickable Apps Using AppleScript How to Turn Bash Scripts Into Clickable Apps Using AppleScript Bash scripts are powerful, but not always convenient. Here's how AppleScript can turn Bash commands into clickable apps. Read More .

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 8 Time-Saving Automator Workflows for Your Mac 8 Time-Saving Automator Workflows for Your Mac The Automator app on your Mac makes it easy to automate tedious tasks. Here are awesome time-saving workflows you should set up. Read More if you’re interested.

Explore more about: Applescript, Computer Automation, Mac Tricks, Scripting, Task Automation.

Enjoyed this article? Stay informed by joining our newsletter!

Enter your Email

Leave a Reply

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

  1. Ravinder
    May 23, 2019 at 9:55 am

    Hi Tim,
    Well I have gone through the term Scripting Language many times , can you answer when a programming language said to be scripting language i.e what characteristics should it have?

  2. Bob
    December 15, 2010 at 11:34 am

    I as well have 10.6.5 and do not have the above noted info scripts>default actions.
    Perhaps this is no longer offered in Snow Leopard?
    I will instal your copy from dropbox. Thanks for sharing.

  3. Arjay67
    November 30, 2010 at 2:55 pm

    Will part 2 be coming soon?

  4. Bakari
    October 28, 2010 at 5:55 pm

    tg108, sorry about that. I'm not sure why those files showed up on my computer and it's not showing up on others. I checked the default settings of Snow Leopard and they should have been there. Anyway, you can download them here: http://dl.dropbox.com/u/252347...

  5. Bakari
    October 28, 2010 at 3:55 pm

    tg108, sorry about that. I'm not sure why those files showed up on my computer and it's not showing up on others. I checked the default settings of Snow Leopard and they should have been there. Anyway, you can download them here: http://dl.dropbox.com/u/252347/Scripts.zip

    • Bob
      December 15, 2010 at 10:34 am

      I as well have 10.6.5 and do not have the above noted info scripts>default actions.
      Perhaps this is no longer offered in Snow Leopard?
      I will instal your copy from dropbox. Thanks for sharing.

      • Bakari
        December 15, 2010 at 5:09 pm

        Yeah, this must be the case. I know for sure that it was installed by default in my OS, but I'm not sure why it's not showing up now. Thanks for letting me know.

  6. tg108
    October 28, 2010 at 4:42 am

    I'm using 10.6.4 and also do not have the info scripts > default actions

  7. Zane
    October 23, 2010 at 6:55 pm

    Ok, so I got through the whole thing, and I have to say, it is AWESOME! I can really use this! Can't wait for the next one! Keep up the good work, Bakari!

  8. Zane
    October 23, 2010 at 6:54 pm

    Ok, so I got through the whole thing, and I have to say, it is AWESOME! I can't really use this! Can't wait for the next one! Keep up the good work, Bakari!

  9. Bakari
    October 23, 2010 at 6:53 pm

    Cool, Zane. Glad it works. I'm still not sure why the default files were not in your system. I'm wondering if my default AppleScript scripts were carried over from previous versions of the OS X? I can't understand why Apple would stop including them.

    • Zane
      October 23, 2010 at 7:21 pm

      Yes, I tried to carry over information from my old computer, but it didn't work. Because I got halfway through and it died. So i just started from scratch, but I think a couple of the old system things are still on this computer...if you can translate all that...

  10. Zane
    October 23, 2010 at 6:48 pm

    Ok, beautiful, I downloaded that, and dragged it into Macintosh HD:Library, and it worked!

  11. Bakari
    October 23, 2010 at 5:35 am

    Hmmm, that's weird. Did you check Library > Scripts?
    If not there, I posted them on my dropbox account. You can download and put them in your Scripts folder: http://dl.dropbox.com/u/252347...
    What version of Mac OS X are using?

  12. Bakari
    October 23, 2010 at 3:35 am

    Hmmm, that's weird. Did you check Library > Scripts?
    If not there, I posted them on my dropbox account. You can download and put them in your Scripts folder: http://dl.dropbox.com/u/252347/Scripts.zip
    What version of Mac OS X are using?

    • Marta
      October 23, 2010 at 7:11 am

      Couldn't find it either (OS 10.6.4).

  13. Zane
    October 23, 2010 at 2:43 am

    I don't have the default actions or finder's script folders on mine.