title | updated | layout | category | prism_languages | intro | |
---|---|---|---|---|---|---|
AppleScript |
2023-04-05 |
2017/sheet |
macOS |
|
AppleScript is a scripting language for automating macOS. |
osascript -e "..."
display notification "X" with title "Y"
-- This is a single line comment
# This is another single line comment
(*
This is
a multi
line comment
*)
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
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
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
-- Define the handler
on doTheThing()
-- Do something
return true
end doTheThing
-- Call the handler
doTheThing()
-- 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()
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
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
-- default voice
say "Hi I am a Mac"
-- specified voice
say "Hi I am a Mac" using "Zarvox"
-- beep once
beep
-- beep 10 times
beep 10
-- delay for 5 seconds
delay 5
-- dialog with custom buttons
display dialog "Hello, world!" buttons {"Cancel", "Continue"} default button "Continue" cancel button "Cancel"
-- ping apple.com 3 times
do shell script "ping -c 3 apple.com"
- AppleScript Language Guide (developer.apple.com)
- AppleScript Users Mailing List (lists.apple.com)
- macOS Automation Sites (macosxautomation.com)
- MacScripter (macscripter.net)
- Late Night Software Forum (forum.latenightsw.com)
Thank you!!! This is amazing