Intro

The list of commands defined by an application can be obtained by opening the associated directory in the script editor.

Basics

Commands:

beep 2 
say "Hello" using "Zarvox"

tell application "Finder"
   open the startup disk
end tell

Comments:

-- single line comment
# And this one too
(* Multiline
   comment *)

Variables:

set width to 4
set height to 2
set area to width * height
display dialog "Area is " & area buttons { "Ok" } default button 1 -- Or default button "Ok"
set len to the length of "Help me"
set num to "3" as number -- or string

Lists:

set myList to {1,2,3,4} & {5}
set myList to myList & {6} //Append
set item 2 of myList to 6 -- use -1 to access from end
get myList
set valueOfLastItem to the last item of myList
set middleItems to items 2 through 4 of myList
get reverse of myList
get the length of myList -- length
get some item of myList -- random item
get 4 as list -- coerce a list
set the end of myList to 7
myList contains 3 -- returns true
myList is equal to {1,2,3} -- returns false

Records:

set monster to {name:"dragon", size:4}
get name of monster
set numberOfFields to the count of monster
set biggerMonster to {name: name of monster, size: size of monster + 1}
monster contains weapon -- returns false
monster is equal to biggerMonster -- returns false

set val to display dialog "Proceed"
# returns {button returned:"OK"}
get button returned of val -- AppleScript has special field names with spaces

Records are copied by reference, not by value!

brush:tcl
set monster to {name:"dragon", size:4}
set biggerMonster to monster
set size of biggerMonster to 40
get monster -- returns {name:"dragon", size:40}

Use copy instead:

brush:tcl
set monster to {name:"dragon", size:4}
copy monster to biggerMonster
set size of biggerMonster to 40
get monster -- returns {name:"dragon", size:4}

Conditions:

brush:tcl
if size of monster > 4 then
  say "Run, you fools!"
else if size of monster < 1 then
  say "What is this? A joke?"
else 
  say "I'm not scared."
end if

Text manipulation:

set mycolor to "red" -- color is a reserved word
set type to "dragon"
set monster to mycolor & " " & type

Text conditions (can be negated with "does not" or "is not"):

if monster begins with "red" then say "I like red"
begins with (or, starts with) ends with
is equal to
comes before -- Comparison <
comes after  -- Comparison >
is in
contains

Some temporary scopes:

considering case
   if "A" is in "alps" then beep 1
end considering
ignoring white space -- or punctuation, diacriticals
   if "AB" = "A B" then say "We are brothers!"
end ignoring

Exceptions:

try
  -- somthing dangerous
on error the error_message number the error_message
  -- inform of consequences
  displace dialog error_message
end try

Loops:

repeat 2 times
  beep 1
end repeat

set sum to 0
repeat while sum < 10 -- or until
  set sum to sum + 1
end repeat

repeat with counter from 10 to 1 by 2
  say counter
end repeat

repeat with value in {1,3,8}
  say value
end repeat

set myList to {"Hello", "Goodbye", "I must be going"}
repeat with theItem in myList
  say theItem
end repeat

Functions define a local scope for variables and global variables.

global num
set num to 3
on mywarning(txt)
  display dialog "Oh oh oh " & txt
  return num
end mywarning
mywarning("bob")

There is also property, which remain across several invocations of the script:

property x : 1
set x to x + 1

Modules:

set module to (load script "path")
on xxx()
   return "xxx"
end xxx
tell module
   nameOfFunctionInModule()
   xxx() of me
end tell

Clicking with the right mouse button opens generators to add typical code.

You can save the script as:

  • Script
  • Script bundle
  • Application
  • Text

One reference.


GUI

Text dialog:

set ret to display dialog "What is your quest?" default answer "To seek the Holy Grail!"
# Returns {button returned:"OK", text returned:"To seek the Holy Grail!"}
get text returned of ret

Ok dialog

display dialog "Let's proceed"

Options

set val to display dialog "What is your favorite color?" buttons {"Red", "Green", "Blue"} default button 1 -- or "Red"
get button returned of val

Files and finder

Apparently you can have aliases and paths. Not sure how this works.

set dir to choose folder "Choose a folder" -- or file
tell application "Finder"
  open folder dir -- or file
  set thePath to a reference to folder "Macintosh HD:Users"
  open folder thePath
end tell

set folders to every folder of dir -- creates a list
repeat with f in folders
  -- do something
end repeat

GUI

dialog


Photos

Get all albums in a folder:

tell application "Photos"
  activate
  set eventFolder to folder "Events"
  repeat with a in every album of eventFolder
    set n to (name of a)
    log n
  end repeat
end tell