Here’s a cheat sheet for AppleScript, a scripting language developed by Apple for automating tasks on macOS:
Comments:
-- This is a comment
Variables:
set variableName to "Hello, World!"
Display Dialog:
display dialog "Hello, World!" buttons {"OK"}
User Input:
set userInput to text returned of (display dialog "Enter your name:" default answer "")
Conditional Statements:
if condition then
-- Code to execute if condition is true
else
-- Code to execute if condition is false
end if
Repeat Loop:
repeat 5 times
-- Code to repeat 5 times
end repeat
Handling Lists:
set myList to {1, 2, 3, 4, 5}
set itemAtIndex to item 3 of myList
AppleScript Handlers:
on sayHello()
display dialog "Hello from the handler!"
end sayHello
sayHello()
Working with Files:
set filePath to choose file "Select a file:"
set fileContent to read filePath as «class utf8»
System Events:
tell application "System Events"
keystroke "Hello, World!"
end tell
Finder Operations:
tell application "Finder"
set desktopFolder to desktop
set filesList to every file of desktopFolder
end tell
Scripting Applications:
tell application "TextEdit"
activate
make new document
set text of document 1 to "Hello, World!"
end tell
Error Handling:
try
-- Code that might generate an error
on error errorMessage
display dialog "An error occurred: " & errorMessage
end try
Display Notification:
display notification "Task completed" with title "Notification"
Running Shell Commands:
do shell script "echo 'Hello, World!'"
Quit Application:
tell application "TextEdit" to quit
Getting System Information:
set systemVersion to system version of (system info)
Setting System Volume:
set volume 50
Sleeping:
delay 5 -- Sleeps for 5 seconds
Opening URLs:
open location "https://www.example.com"
This is a basic cheat sheet for AppleScript. For more detailed information and advanced scripting, refer to the official AppleScript documentation.