AppleScript
Cheatsheet
- Get actual path of finder window if opened
#!/usr/bin/env bash
if ! osascript -s o <<EndOfScript
tell application "Finder"
try
set c to (count of Finder windows)
on error message number -1743
error "Privacy settings prevent access to Finder"
end try
if c is 0 then
return POSIX path of (desktop as alias)
else
return POSIX path of ((target of Finder window 1) as alias)
end if
end tell
EndOfScript
then
echo "osascript failed"
fi
- Display an hello world window
osascript -e 'tell application "Finder"
activate
display dialog "Hello, World!"
end tell'
- Display an hello world window but without sending events to another process
osascript <<EndOfScript
display dialog "Hello, World!"
EndOfScript
The useful commands in the Standard Additions extension include:
- user interaction: choose file/folder/from list, display dialog/alert/notification
- file commands: mount volume
- clipboard commands: get the clipboard, set the clipboard to
- sound control: set volume, get volume settings
- system info
When your script uses only these commands, make sure they are not contained in tell blocks. This will avoid unnecessary prompts for access approval.
Exempt AppleScript commands
Some AppleScript commands are treated differently and will not trigger privacy approval:
- activate: launch application and/or bring to front
- open: open a file
- open location: open a URL
- quit: quit the application
osascript <<EndOfScript
tell application "Firefox"
open location "https://scriptingosx.com"
end
EndOfScript