AppleScript facts for kids
Paradigm | Natural language programming, Scripting |
---|---|
Developer | Apple Inc. |
First appeared | 1993 |
Stable release |
2.8 / October 16, 2014
|
Typing discipline | Weak, dynamic |
OS | System 7, Mac OS 8, Mac OS 9, macOS |
License | Proprietary (parts available under APSL) |
Filename extensions | .scpt, .scptd, .applescript |
Influenced by | |
Natural language, HyperTalk |
AppleScript is a special computer language made by Apple Inc.. It helps you make your Mac apps do things automatically. Think of it like giving your computer a list of instructions to follow.
AppleScript first came out in 1993 with System 7. Today, it's still part of macOS, which is the operating system for Macs. It's included with other tools that help you automate tasks. The name "AppleScript" can mean the language itself, a set of instructions written in it, or the system that makes it all work on your Mac.
This language mainly uses something called "Apple events." These are like special messages that apps send to each other. AppleScript uses these messages to control apps and share information between them. It can also do simple math and work with text. You can even add more features to AppleScript using special "scripting additions."
AppleScript is made specifically for the Mac environment. It's not like other programming languages, such as Python, that can be used for almost anything. AppleScript needs apps to have special "dictionaries." These dictionaries tell AppleScript what the app can do and what parts of it can be controlled.
AppleScript is not designed for really big math problems or super complex text work. But it can work with other tools to handle harder tasks. It uses ideas from different ways of programming, including a "natural language" style that makes it easier to read.
Contents
How AppleScript Started
In the late 1980s, Apple thought about using HyperCard's HyperTalk language. This language was easy for beginners to use. Apple engineers realized they could make a similar, but more advanced, language. This new language would work with any app. That's how the AppleScript project began. It became part of System 7.
AppleScript was released in October 1993. It was part of System 7.1.1. One of the first big apps to use AppleScript was QuarkXPress (version 3.2). This made AppleScript very popular in the publishing world. It helped people connect different programs to do complex tasks. This was important for Macs to stay strong in publishing, even when other apps moved to Windows computers.
When Macs switched to Mac OS X around 2002, AppleScript became even more useful. New tools in Mac OS X made it easier for app developers to add AppleScript support. This meant more apps could be controlled by scripts. Also, Mac OS X was built on Unix, and AppleScript could run Unix commands directly. This gave AppleScripts much more control over the computer system.
Later, tools like AppleScript Studio and AppleScriptObjC allowed people to build full Mac apps using AppleScript. In 2006, Macworld magazine called AppleScript one of Apple's 30 most important products.
In 2013, a writer named John Gruber said that AppleScript was "the best thing we have that works." He noted that it showed how Macs were better than iOS for people who like to customize things.
In 2016, Sal Soghoian, who managed AppleScript for a long time, left Apple. Many people in the Mac community worried about the future of automation tools. Apple said they would continue to support these tools. Today, AppleScript is still a part of macOS automation. It works alongside tools like Automator and Shortcuts.
What AppleScript Does
AppleScript was made to be easy for anyone to use. It helps users control apps and work with their files and information. It uses "Apple events," which are like standard messages the Mac system sends to apps. These messages are similar to how web services talk to each other.
Apple events let a script work with many apps at once. It can pass information between them. This means complex jobs can be done without a person clicking every step. For example, an AppleScript could create a simple web photo gallery:
- It could open a photo in a photo-editing app.
- Then, it could tell that app to change the picture. For example, it could make it smaller or add a border.
- Next, it could tell the app to save the new picture in a different folder.
- After that, it could send the new picture's location to a text editor.
- It could tell the editor to write a link for the photo into an HTML file.
- It could repeat these steps for hundreds of photos.
- Finally, it could upload the HTML file and pictures to a website.
For you, this means hundreds of steps in many apps become just one action: running the script. The job gets done much faster and without mistakes. You could use a big script once, or use smaller ones again and again.
How Apps Talk to AppleScript
Apps that work with AppleScript have a "Scripting Dictionary." You can see this dictionary in a script editor. It shows you what the app can do. Things are usually grouped into "suites."
There are two main parts in any suite:
- Classes: These are the things an app can control. For example, a text app might have classes for windows, documents, and text. These classes have properties you can change, like window size or text font. They can also contain other classes, like a document containing text.
- Commands: These are instructions you can give to the classes. AppleScript usually tells an object to run a command.
All scriptable apps share some basic commands. These are often called the "Standard Suite." They include commands to open, close, save, print, or quit an app. Many apps have many suites that can do anything the app itself can do.
AppleScript was also designed so you could record your actions. If an app supports recording, you can click "Record" in the Script Editor. Then, when you do things in the app, AppleScript turns your actions into commands. You can save this script and run it again to repeat your actions. You can also change it to make it more useful.
Talking Like a Human
AppleScript tries to use words that sound like natural language. This makes it easier for beginners to understand. It's like how your computer's screen uses a "desktop" idea. A good AppleScript should be easy to read and change.
The language uses words that act like nouns and verbs. For example, to print something, AppleScript uses one "print" verb. You can combine it with what you want to print:
print page 1
print document 2
print pages 1 thru 5 of document 2
To tell AppleScript which app to control, you use a "tell" command:
tell application "Microsoft Word"
quit
end tell
You can also write it on one line:
tell application "Microsoft Word" to quit
If you want to quit an app, you can also say:
quit application "Microsoft Word"
You can also tell AppleScript to go deeper into an app's parts:
tell application "QuarkXPress"
tell document 1
tell page 2
tell text box 1
set word 5 to "Apple"
end tell
end tell
end tell
end tell
AppleScript lets you use words like "first paragraph" or "paragraph one." You can also use numbers as words ("five") or digits ("5"). The word "the" can be used anywhere to make it easier to read. It doesn't change what the script does.
Simple Examples
Hello, World!
Here are some simple AppleScript examples. They show how to make your Mac say "Hello, world!"
To show a pop-up window with "OK" and "Cancel" buttons:
display dialog "Hello, world!"
To show a pop-up window with an "OK" button and a warning icon:
display alert "Hello, world!"
To make your Mac speak "Hello, world!" using a computer voice:
say "Hello, world!"
Basic Calculator
This script asks for two numbers and then lets you choose to add, subtract, multiply, or divide them. It also checks if you entered numbers correctly.
tell application "Finder"
-- Ask for the first number
set the1 to text returned of (display dialog "Enter the 1st number:" default answer "Number here" buttons {"Continue"} default button 1)
-- Ask for the second number
set the2 to text returned of (display dialog "Enter the 2nd number:" default answer "Number here" buttons {"Continue"} default button 1)
-- Check if the inputs are numbers
try
set the1 to the1 as integer
set the2 to the2 as integer
on error
display dialog "You can only enter numbers for a calculator." with title "ERROR" buttons {"OK"} default button 1
return -- Stop the script if there's an error
end try
-- Ask if you want to add
if the button returned of (display dialog "Do you want to add?" buttons {"No", "Yes"} default button 2) is "Yes" then
set ans to (the1 + the2)
display dialog ans with title "Answer" buttons {"OK"} default button 1
say ans
-- Ask if you want to subtract
else if the button returned of (display dialog "Do you want to subtract?" buttons {"No", "Yes"} default button 2) is "Yes" then
set ans to (the1 - the2)
display dialog ans with title "Answer" buttons {"OK"} default button 1
say ans
-- Ask if you want to multiply
else if the button returned of (display dialog "Do you want to multiply?" buttons {"No", "Yes"} default button 2) is "Yes" then
set ans to (the1 * the2)
display dialog ans with title "Answer" buttons {"OK"} default button 1
say ans
-- Ask if you want to divide
else if the button returned of (display dialog "Do you want to divide?" buttons {"No", "Yes"} default button 2) is "Yes" then
set ans to (the1 / the2)
display dialog ans with title "Answer" buttons {"OK"} default button 1
say ans
else
delay 1
say "You didn't choose an operation. The task has been canceled."
end if
end tell
Simple Login
This script pretends to be a login screen. It checks if the username is "John" and the password is "app123".
tell application "Finder"
set passAns to "app123"
set userAns to "John"
-- Ask for username
if the text returned of (display dialog "Enter Username" default answer "") is userAns then
display dialog "Correct username!" buttons {"Continue"} default button 1
-- Ask for password if username is correct
if the text returned of (display dialog "Username: John" & return & "Enter Password" default answer "" buttons {"Continue"} default button 1 with hidden answer) is passAns then
display dialog "Access granted!" buttons {"OK"} default button 1
else
display dialog "Incorrect password." buttons {"OK"} default button 1
end if
else
display dialog "Incorrect username." buttons {"OK"} default button 1
end if
end tell
Tools for AppleScript
These tools help you write, test, and run AppleScripts. They let you create, check, and fix your scripts. Some also help you see what an app can do with AppleScript.
Script Editors
- Script Editor: This is an editor from Apple that comes with macOS. You can write, test, and run scripts here. It also helps you find information about what apps can do. Since 2014 (OS X Yosemite), Script Editor can also be used to write in JavaScript.
- Xcode: This is a bigger set of tools from Apple for making apps. It has features for editing scripts and making standalone apps with AppleScript.
- Script Debugger: This is a paid tool that helps you find and fix problems in your AppleScripts. It lets you go through your script step-by-step to see what's happening. It also has a special browser to help you understand app dictionaries.
- Smile and SmileLab: These are other tools for AppleScript. Smile is free and good for general AppleScript work. SmileLab is a paid version with more features for math, charts, and web tasks. They let you create more complex pop-up windows for your scripts.
Running Your Scripts
You can run a script from an editor while you're working on it. But for automation, you want scripts to run without opening another app. Here are some ways to do that:
- Applets: You can save your AppleScripts as small applications. These are called "applets." If they can accept files by dragging and dropping, they are called "droplets." You can run applets from your Dock, from Finder windows, or from Spotlight.
- Folder Actions: You can set up scripts to run when something changes in a folder. For example, a script could run when you add new files to a folder. You can set this up by right-clicking a folder and choosing "Folder Actions Setup..."
- Hotkey Launchers: You can assign keyboard shortcuts to your AppleScripts. This lets you run a script by pressing certain keys. Many third-party apps like Alfred or Keyboard Maestro can also do this.
- Script Menu: This is a menu that appears at the top of your screen, no matter what app you're using. You can put your AppleScripts here to easily run them. When you first turn it on, it shows some example scripts.
- Unix Command Line: You can run AppleScripts from the Unix command line using a tool called `osascript`. This is useful for scheduled tasks.
Advanced Features
AppleScript Libraries
These are like reusable blocks of AppleScript code. You can write them once and then use them in many different scripts. They can even have their own dictionaries, just like apps.
AppleScriptObjC
This is a special tool that lets AppleScripts use parts of Cocoa. Cocoa is a set of tools that Mac apps are built with. AppleScriptObjC lets your scripts do even more powerful things by directly using these Cocoa features. It has been available in macOS since Mac OS X Snow Leopard.
Automator
Automator is a visual tool where you build "workflows" by dragging and dropping "actions." It's designed to do many of the same things as AppleScript, but without needing to write code. Automator also has an action that lets you run AppleScripts for tasks that are too complex for Automator's simpler setup.
Core System Apps
macOS includes some hidden apps that AppleScript uses. These apps help AppleScript control things that aren't normally scriptable. Examples include:
- VoiceOver: For controlling the screen reader.
- System Events: For controlling apps that don't have their own script support, and for basic system tasks.
- Image Events: For changing pictures.
Scripting Additions (OSAX)
These are like plug-ins for AppleScript. They add new commands to the language. This makes AppleScript more powerful and less dependent on what individual apps can do. macOS includes "Standard Additions," which add commands for things like showing pop-up windows, reading and writing files, and doing math. Without these, AppleScript couldn't do many basic tasks.
How the Language Works
Variables and Types
In AppleScript, you don't have to declare what kind of information a variable will hold. A variable can hold any type of data, like numbers, text, or even other scripts. For example:
-- This creates a number variable called variable1
set variable1 to 1
-- This creates a text variable called variable2
set variable2 to "Hello"
-- This creates a list variable called variable3
copy {17, "doubleday"} to variable3
-- This copies items from variable3 into two new variables
set {variable4, variable5} to variable3
-- This sets a variable to be a script
set variable6 to script myScript
Code Blocks
AppleScript uses special words to start and end blocks of code. You can write them on one line or multiple lines. For example:
-- Simple one-line command
tell application "Safari" to activate
-- A block of commands for an app
tell application "MyApp"
-- commands for this app go here
end tell
Script Objects
A script object is like a container for a script. It can hold its own commands and data. You can load script objects from files and save them. They can also "tell" other objects to do things, just like apps.
script scriptName
-- commands and special functions for this script
end script
Loops
Loops let you repeat commands many times. AppleScript uses the word repeat for loops. You can stop a loop using exit repeat.
- Repeat forever:
repeat
-- these commands will repeat forever until stopped
end repeat
- Repeat a certain number of times:
repeat 10 times
-- these commands will repeat 10 times
end repeat
- Repeat while a condition is true:
set x to 5
repeat while x > 0
set x to x - 1 -- x will go down by 1 each time
end repeat
- Repeat until a condition is true:
set x to 5
repeat until x ≤ 0
set x to x - 1 -- x will go down by 1 each time
end repeat
- Repeat with a variable (counting):
-- This repeats 2000 times, with 'i' going from 1 to 2000
repeat with i from 1 to 2000
-- commands to be repeated
end repeat
-- This repeats 4 times, with 'i' going from 100 down to 25
repeat with i from 100 to 25 by -25
-- commands to be repeated
end repeat
- Repeat for each item in a list:
set total to 0
repeat with loopVariable in {1, 2, 3, 4, 5}
set total to total + loopVariable -- adds each number in the list to 'total'
end repeat
Handlers (Functions)
A handler is like a mini-program or function within your script. It lets you group commands together and run them whenever you need.
- Basic function handler:
on myFunction(parameters...)
-- commands for this function
end myFunction
- Folder actions handler: This runs when items are added to a folder.
on adding folder items to thisFolder after receiving theseItems
-- commands to apply to the folder or items
end adding folder items to
- Run handler: This is the main part of your script that runs when you start it.
on run
-- commands that run when the script starts
end run
There are four main types of handlers in AppleScript: `run`, `open`, `idle`, and `quit`.
- Run handler: This is the main code that runs when you start the script.
- Open handler: This runs when you drag files or folders onto a script saved as a "droplet."
on open theItems
repeat with thisItem in theItems
tell application "Finder" to update thisItem
end repeat
end open
- Idle handler: This runs over and over again when your script is not busy. It's useful for scripts that need to keep checking for something.
on idle
-- code to run when the script is waiting
return 60 -- wait 60 seconds before running again
end idle
- Quit handler: This runs when your script is told to quit. You can use it to save things or clean up before the script closes.
on quit
-- commands to run before the script quits
continue quit -- this is needed for the script to actually quit
end quit
Comments
Comments are notes in your code that the computer ignores. They help you and others understand what your script does.
- A single-line comment starts with `--` or `#` (in newer versions):
-- This is a line comment
# So is this!
- A block comment (for multiple lines) starts with `(*` and ends with `*)`:
(* This is a
multiple
line
comment *)
User Interaction
AppleScript can show pop-up windows, alerts, and lists for you to choose from.
- Dialog (pop-up window):
set dialogReply to display dialog "Dialog Text"
default answer "Text Answer" -- text already in the box
hidden answer false -- shows what you type
buttons {"Skip", "Okay", "Cancel"} -- buttons to click
default button "Okay" -- which button is highlighted
cancel button "Skip" -- which button cancels the dialog
with title "Dialog Window Title" -- title of the window
with icon note -- adds a note icon
giving up after 15 -- closes after 15 seconds
- Choose from list:
set chosenListItem to choose from list {"A", "B", "3"}
with title "List Title"
with prompt "Prompt Text"
default items "B"
OK button name "Looks Good!"
cancel button name "Nope, try again"
multiple selections allowed false
with empty selection allowed
- Alert (warning pop-up):
set resultAlertReply to display alert "Alert Text"
as warning -- shows a warning icon
buttons {"Skip", "Okay", "Cancel"}
default button 2
cancel button 1
giving up after 2
These interactions can give you back information, like which button was clicked or what text was typed.
display alert "Hello, world!" buttons {"Rudely decline", "Happily accept"}
set theAnswer to button returned of the result
if theAnswer is "Happily accept" then
beep 5 -- makes a sound 5 times
else
say "Piffle!" -- makes the computer say "Piffle!"
end if
Open Scripting Architecture (OSA)
Apple created the Open Scripting Architecture (OSA) to let other scripting languages work with AppleScript. This means other languages can also control apps on your Mac. AppleScript is a part of this system. The rules for how these languages connect are public. This allows other developers to add their own scripting tools to the system.
One cool feature of OSA is "scripting additions" (also called OSAX). These are like plug-ins that add new commands to AppleScript. These commands work everywhere on your system, not just in one app.
JavaScript for Automation (JXA)
Since 2014 (OS X Yosemite), JavaScript for Automation (JXA) is another important language that works with OSA. While other languages like Perl and Python can also work with Apple events, JXA is a direct alternative to AppleScript within the OSA system. JXA lets you use Objective-C (a programming language) directly from JavaScript.
See also
In Spanish: AppleScript para niños
- ARexx