What is AppleScript?

 · 4 mins

AppleScript

If you are a Mac user, you most probably have looked through all the apps you have in your Launchpad and may have noticed an app called “Script Editor” (called “AppleScript Editor” in older versions).


Looking upon that you may have wondered “What is AppleScript?” This article will tell you about that.

What is AppleScript?

AppleScript is a native scripting language for macOS designed to help you automate your tasks by providing rich access to various system features (such as window buttons, filesystem and so on). The syntax of the language is close to natural speech.

How to use

To start developing AppleScripts, open the Script Editor app (by default located in “Other” in Launchpad).
When you first type your code it will not be syntax highlighted, start or “build” it to perform syntax check and highlighting.


Press the “Play” button in the toolbar to execute the script. That will check and build as well.

Basic Examples

A hello world alert

To display an alert you can use the display alert method:

display alert "Hello world"

As expected, this pops up a little alert box saying “Hello world”:

A notification example

To display a notification you can use the display notification method:

display notification "Work is done" with title "My awesome app" subtitle "Work status changed"

This will pop up a basic system notification with the given parameters.

All arguments except the first one (the main text) are optional.

Modals

You use the optional buttons argument of display dialog to turn it into a modal:

display alert "Are you sure? This dialog will automatically close after 2,5 minutes." buttons {"Yes", "No"} giving up after 150

Result:

(this dialog will close in 150 seconds)

Using the response

In order to use the data received from the prompt you need to save it into a variable (we will use result):

set result to (display alert "Are you sure? This dialog will automatically close after 2,5 minutes." buttons {"Yes", "No"} giving up after 150)
if button returned of result = "Yes" then 
    display alert "Action not cancelled"
else
    display alert "Action cancelled"
end if

In this example we are asking the user for confirmation and displaying different alerts depending on what was the response.

Asking user for information

We need to use display dialog with default answer set to any value (blank to provide empty box). This will make the dialog show a text box for user to answer the question. Example:

set result to (display dialog "Enter your name" default answer "" giving up after 150)
set userName to text returned of result
display alert "The user name is " & userName

This code asks the user for their name and shows a new alert box with it.

Conclusion

AppleScript was designed as a simple way of automating tasks and is one of the ways to do so. Modern versions of Script Editor also support a special subset of JavaScript so if you don’t like the unusual syntax you may feel more comfortable writing code in JS.


Published by FreeAppSW, translated by Rémy Samkocwa