A quick reference guide to writing and running AppleScript on macOS, from command-line osascript invocations to system UI automation.
Running AppleScript
From Terminal (osascript)
# Execute inline expression
osascript -e 'display notification "Task completed!" with title "Build Script"'
# Run a script file (.scpt or .applescript)
osascript script.scpt
Native Notification
display notification "Backup finished successfully" with title "System Status" subtitle "Automated Backup"
Comments
-- This is a single-line comment
# This is also a single-line comment
(*
This is
a multi-line
comment block
*)
Sound & Audio
Speech Synthesis (say)
-- Default system voice
say "Build succeeded"
-- Specified system voice
say "Warning: low battery" using "Zarvox"
say "All systems nominal" using "Samantha"
System Beep
-- Beep once
beep
-- Beep multiple times
beep 3
Execution Delay
-- Pause execution for 5 seconds
delay 5
Handlers (Functions)
on doubleNumber(n)
return n * 2
end doubleNumber
set doubledValue to my doubleNumber(21)
-- → 42
Handler with Multiple Arguments
on showDialog(msg, titleText)
display alert titleText message msg as informational
end showDialog
my showDialog("Operation completed.", "Success")
Interacting with Applications
tell application "Finder"
activate
open folder "Documents" of home
end tell
tell application "Google Chrome"
reload active tab of front window
end tell