Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Created April 5, 2023 23:47
Show Gist options
  • Save SKaplanOfficial/662812d9268be001e74bd04aba2a47dc to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/662812d9268be001e74bd04aba2a47dc to your computer and use it in GitHub Desktop.
Cheatsheet providing an overview of AppleScript's features.
title updated layout category prism_languages intro
AppleScript
2023-04-05
2017/sheet
macOS
applescript
AppleScript is a scripting language for automating macOS.

Running

osascript -e "..."
display notification "X" with title "Y"

Language Features

Comments

-- This is a single line comment
# This is another single line comment
(*
This is
a multi
line comment
*)

Text

set myText to "Hello, world!"

-- Text properties
text 1 thru 5 of myText -- "Hello"
length of myText -- 13
words of myText -- {"Hello", "world"}
offset of "world" in myText -- 8

-- Concatenation
myText & " Goodbye!" -- "Hello, world! Goodbye!"

-- Comparing Strings
myText is "Hello, world!" -- true
"Hello" is in myText -- true
myText contains "Goodbye" -- false

considering white space but ignoring case and punctuation
	myText is "hElLo, WoRlD" -- true
	myText is "Hello,world!" -- false
end considering

Numbers

set x1 to 1 + 2 - 3 * 4 / 5 -- 0.6
set x2 to 3 ^ 2 -- 9.0
set x3 to 9 ^ 0.5 -- 3.0
set x4 to 9 mod 3 -- 0
set x5 to 9 div 2 -- 4
set y1 to 1.0 as integer -- 1
set y2 to 2 as real -- 2.0
set y3 to "5" as number -- 5
set y4 to true as integer -- 1
set z1 to 5 = 4 + 1 -- true
set z2 to 5 <= 4 + 1 -- true
set z3 to 5 > 4 + 1 -- false
set z4 to true or false -- true
set z5 to true and false -- false

Lists

set myList to {1, 2, 3, 4}
set listSize to count of myList

-- Accessing list items
set item1 to item 1 of myList
set sublist to items 2 thru 4 of myList

-- Manipulating list elements
set item 3 of myList to -3
copy 5 to the end of myList
set beginning of myList to 0
set theReverse to reverse of myList

-- Checking list membership
5 is in myList -- false
myList contains 4 -- true

Handlers

-- Define the handler
on doTheThing()
	-- Do something
	return true
end doTheThing

-- Call the handler
doTheThing()

Script Objects

-- Defining a script
script myScript
	property exampleProperty : "Hello, world!"
	
	on doTheThing()
		-- Do something
	end doTheThing
end script

-- Using a script
log myScript's exampleProperty
tell myScript to doTheThing()

File Manipulation

Reading

set myFile to POSIX file "/Users/exampleUser/Documents/example.txt"
set fileRef to open for access myFile with write permission
try
	set fileText to read fileRef
	close access fileRef
on error err
	close access fileRef
end try

Writing

set myFile to POSIX file "/Users/exampleUser/Documents/example.txt"
set fileRef to open for access myFile with write permission
try
	write "Hello, world!" to fileRef starting at eof
	close access fileRef
on error err
	close access fileRef
end try

Commands

Say

-- default voice
say "Hi I am a Mac"
-- specified voice
say "Hi I am a Mac" using "Zarvox"

Beep

-- beep once
beep
-- beep 10 times
beep 10

Delay

-- delay for 5 seconds
delay 5

Display Dialog

-- dialog with custom buttons
display dialog "Hello, world!" buttons {"Cancel", "Continue"} default button "Continue" cancel button "Cancel"

Do Shell Script

-- ping apple.com 3 times
do shell script "ping -c 3 apple.com"

Useful Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment