applescript

Getting started with applescript

Remarks#

AppleScript is a language that predates the Mac as we know it today, originating in 1993. While other scripting languages have more fully featured libraries and more robust syntax, the power of AppleScript is for automating Mac applications.

Any Mac application can include a scripting dictionary, and many that ship with macOS do. A scripting dictionary lets scripts know what items the app contains, and what actions can be performed.

A script can then use that information to automate repetitive tasks, and to form a “glue” between different apps, forming fully customizable workflows.

Versions#

VersionOS ReleaseRelease Date
1.610.02001-03-24
1.710.12001-09-25
1.910.22002-08-23
1.9.210.32003-10-24
1.1010.42005-04-29
2.010.52007-10-26
2.110.62009-08-28
2.210.72011-07-20
2.2.410.82012-07-25
2.310.92013-10-22
2.410.102014-10-16
2.510.112015-06-08

Your first AppleScript

  1. Open Script Editor.

    With Mac OS X Leopard and earlier, and OS X Yosemite and later, Script Editor is located at

    /Applications/Utilities/Script Editor.app

    Between Mac OS X Snow Leopard and OS X Mavericks inclusive, Script Editor is AppleScript Editor.

    /Applications/Utilities/AppleScript Editor.app

  2. Enter the following line of code:

    display dialog "Hello World"
  3. Click the run button.

    Two things happen: Script Editor compiles your script (if there are any errors, it’ll let you know what’s wrong and where), and then runs it.

  4. You will now be shown a dialog saying “Hello World”.

You’ve completed your first script!

What the code does:

display dialog is one command, though it uses two words. This is common in AppleScript, unlike other languages which commonly require joining words together with no spaces. The display dialog command tells AppleScript to display a pop-up dialog.

This last part in “double quotes” tells the script what text to display.

Exploring scripting dictionaries

The power of AppleScript lies in being able to automate many Mac applications. To find out what you can automate, you need to read an app’s scripting dictionary.

To do so, launch Script Editor, and select File > Open Dictionary…

Open Dictionary window

Once you choose an app, its dictionary will open up in a new window. At the top of the window, you’ll see a column view (like Finder’s). The first column contains various “suites” of AppleScript terminology. The “Standard Suite” contains items that apply to most apps (e.g. open, close, save, print), and the other suites define custom behavior.

Dictionary window


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow