Sub topic 16
Light automation with AppleScript and the shell
This chapter assumes you can open Terminal and Script Editor, and that your accounts are already set up in Mail, Calendar, and Contacts. You will run a few read-only scripts, hand off a task to Mail from the shell, and look at what a vCard file contains. Automation is a small part of the plan, so the goal is a working starting point.
How scripting reaches the apps
Mail, Calendar, and Contacts each publish a scripting dictionary. This is a list of objects (accounts, calendars, groups) and commands that AppleScript can use. The shell reaches the apps by running AppleScript through osascript, or by handing a URL or file to the default app with open.
Scripting access is governed by the permissions you met in the privacy chapter. The first time a script controls an app, macOS asks whether your terminal or editor may control it. If you click Don't Allow, the script fails until you change the choice in Privacy & Security under Automation.
Reading data with AppleScript
Start with commands that only read. This one lists the accounts configured in Mail, which confirms that scripting access works.
tell application "Mail"
get name of every account
end tellThe result is a list of names, such as {"iCloud", "Work"}. The names match what you see in Internet Accounts settings.
Calendar works the same way. The exact calendar name matters, because later scripts that create events must refer to a calendar by name.
tell application "Calendar"
get name of every calendar
end tellContacts lists its groups with the same pattern.
tell application "Contacts"
get name of every group
end tellThe pattern is always tell application, then get, then a property, then of every and an object type. Once you can read names, you can explore the other properties in the app's dictionary. In Script Editor, choose File > Open Dictionary to browse it.
Running AppleScript from the shell
Save a script and run it from Terminal, or pass it inline with -e:
osascript -e 'tell application "Calendar" to get name of every calendar'Terminal prints the list on one line, separated by commas. Use osascript when you want to call a script from a shell script or a scheduled job.
Handing off a task to Mail
You do not always need AppleScript. The open command passes a mailto: URL to your default mail app, which opens a new message with the fields filled in.
open "mailto:[email protected]?subject=Hello"This is a hand-off. Mail opens a draft, and you review and send it yourself. That makes it safer than a script that sends mail automatically.
Looking at a vCard
A vCard is a plain text file that describes a contact. Save the following as ada.vcf, then double-click it to import it into Contacts.
BEGIN:VCARD
VERSION:3.0
FN:Ada Example
N:Example;Ada;;;
EMAIL;TYPE=INTERNET:[email protected]
TEL;TYPE=CELL:+1-555-0100
END:VCARDEach line is a field. FN is the display name, N holds the name parts, and EMAIL and TEL carry the address and number.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "Not authorized to send Apple events" | Automation permission was denied | Allow the app under Privacy & Security > Automation |
| "Can't get account" or a similar error | The name does not match exactly | List the names first, then copy one |
| `open` starts the wrong mail app | Another app is the default mail handler | Change the default in Mail settings |
Exercise
Which command lists the names of every calendar from Terminal, and why would you run it before writing a script that creates an event?
Show answer
osascript -e 'tell application "Calendar" to get name of every calendar'. Scripts refer to calendars by exact name, so listing the names first lets you copy the right one and avoid a "can't get calendar" error.
Exercise
What is the difference between open "mailto:..." and an AppleScript that sends a message?
Show answer
open only creates a draft in the default mail app, so you review and send it yourself. An AppleScript send command can deliver the message without further confirmation.