-- or #
Browse / AppleScript Cheatsheet: Scripting Mac Automation
AppleScript Cheatsheet: Scripting Mac Automation
Unlock the power of macOS automation with this comprehensive AppleScript cheatsheet covering basic syntax, application scripting, file system operations, UI scripting, and essential tips.
AppleScript Basics & Syntax
Core Syntax Elements
|
|
Single-line comments. |
|
|
Multi-line comments. |
|
|
Variable assignment. |
|
|
Show a dialog box. |
|
|
Make the computer speak. |
|
|
Get the result of the last command or block. |
|
|
Return a value from a script or handler. |
|
|
Explicitly run the main handler of a script. |
Data Types
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Control Structures
|
Conditional Statements:
|
|
Repeat Loops (basic):
|
|
Repeat Loop (iterating over list):
|
|
Repeat Loop (with counter):
|
|
Exiting Loops:
|
|
Error Handling:
|
Scripting Applications
Targeting Applications
|
The
|
|
To see available commands for an application, open it in the Script Editor, go to File > Open Dictionary…, and select the application. |
|
Accessing properties within a
|
|
Using
|
|
Referring to the current script:
|
|
Referring to the current application (the one running the script, e.g., Script Editor):
|
Common Finder Commands
|
|
Brings the application to the front. |
|
|
Opens a file or folder. |
|
|
Duplicates an item. |
|
|
Moves an item. |
|
|
Deletes an item (moves to Trash). |
|
|
Creates a new folder. |
|
|
Gets a record of an item’s properties. |
|
|
Renames an item. |
|
|
Checks if an item exists. |
Working with Paths
|
Colon-delimited path |
|
|
Slash-delimited path |
|
|
|
Get standard folder paths (e.g., |
|
|
Reference an item by its HFS path. |
|
|
Convert POSIX path to alias. |
|
|
Convert alias to POSIX path. |
|
|
Escape spaces and special characters for use in shell scripts. |
UI Scripting & System Events
System Events Application
|
|
|
Enable ‘Enable access for assistive devices’ or grant permissions under Security & Privacy > Accessibility in System Settings for UI scripting to work. |
|
Inspecting UI Elements: Use the ‘Accessibility Inspector’ developer tool (or third-party tools like UI Browser) to find names/IDs of UI elements (windows, buttons, text fields, etc.). |
|
Example: Click a button in a specific window:
|
|
Example: Set value of a text field:
|
|
Example: Get list of running processes:
|
Keyboard Interaction
|
|
Types the specified string. |
|
|
Sends a key press by its key code. |
|
|
Sends a keystroke with modifier keys (command, shift, option, control). |
|
|
Sends key code with multiple modifiers (e.g., Command+Option+Return) |
|
Common Key Codes |
Return: 36, Tab: 48, Space: 49, Escape: 53, Left Arrow: 123, Right Arrow: 124, Down Arrow: 125, Up Arrow: 126 |
Running Shell Scripts
|
Execute shell commands directly.
|
|
Running with administrator privileges:
Note: Hardcoding passwords is a security risk. Consider using |
|
Handling paths and special characters in shell scripts requires careful quoting. Use
|
Advanced Topics & Tips
Handlers (Functions/Subroutines)
|
Define reusable blocks of code.
|
|
Handlers with multiple parameters:
|
|
Handlers with labeled parameters:
|
Working with Lists & Records
|
Accessing list items |
|
|
Counting items |
|
|
Adding items |
|
|
Converting list to string |
|
|
Accessing record properties |
|
|
Checking if record has property |
|
Useful Commands & Tips
|
|
Pause script execution (e.g., |
|
|
Get information about the clipboard content. |
|
|
Get clipboard content as text, alias list, etc. (e.g., |
|
|
Set clipboard content. |
|
|
Escapes special characters in a string for shell scripts. |
|
|
Change the delimiter used by |
|
Best Practice: Save as Application |
Saving a script as an application ( |
|
Best Practice: Stay in Scope |
Use nested |
|
Tip: Debugging |
Use |