AppleScript is a scripting language created by Apple Inc. that facilitates automated control over scriptable Mac applications. First introduced in System 7, it is currently included in all versions of macOS as part of a package of system automation tools. The term "AppleScript" may refer to the language itself, to an individual script written in the language, or, informally, to the macOS Open Scripting Architecture that underlies the language.
AppleScript doesn’t use the same environment as Terminal to run shell scripts?
quoted form of POSIX path of "filename:path:to:file.avi"
Basic Structure
Apparently Applescript needs "" instead of '' ?
Objects
Everything in MacOS is an object, files,apps, disks, windows, etc.
All objects exist in a hierarchy and belong to something else
Files in folders in disks in Finder, data in a window in an app, etc.
Properties and Values
Items/objects have properties which have a value
Icon size = 48
Name = filename1
References (Hierarchy)
folder "Smith Project" of folder "Documents" of folder of folder "Users" of disk "Macintosh HD"
folder "Smith Project" of folder "Documents" of home
Index
1 front window
2 one layer behind from window
Etc.
Comments
- -- comment
- Can use both for beginning of line or for end of line comments
- # is also acceptable
- Multi-line Comments
- Method 1
-- comment 1 -- comment 2
- Method 2
(* This is a multiline comment that takes up multiple lines. *)
Objects
Files
Filenames
Icons
Applications
Clipboard
Running terminal commands
Running Python scripts
Window positions
Desktop
Internet
Shutdown, restart
Backing up
Networking
PDFs
Images
Text
Control
Conditionals
if, then, else, end if
if application "Terminal" is not running then reopen activate do script "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 " & python_path in front window else activate do script "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 " & python_path end if
application "Terminal" is running
Sleep, delay
Delays
delay 0.5
Loops
repeat, end repeat
For Loop Equivalent
repeat 3 times say "hello" end repeat
For Loop with List
set myList to {"Hello", "Goodbye", "I must be going"} repeat with
theItem in myList say theItem end repeat
While Loop Equivalent
set theName to "" repeat while theName = "" display dialog "Dude, enter your name:" default answer ""
set theName to text returned of result say
theName end repeat
Variables
set
set myName to "Sheila"
set yourName to myName
I/O
Print to screen
Dialogue Box
display dialog "Foo?" with icon file "Macintosh
HD:Users:Shared:icon.jpg" default answer "Yes" set theText to text
returned of the result
with icon file "[file:path]"
with icon note
with icon stop
with icon caution
display alert
- Different style of dialogue box, I think has room for more text
Speak Text
say "text"
say "text" using "Samantha"
set volume 10
Play Sound File
Click Window Buttons
Send Keystrokes to Application
tell application "System Events" to keystroke "k" using {control down, option down, command down}
Specific
Miscellaneous Commands
target
tell
tell application "Finder" to get the name of Finder Window 2 is equivalent to tell application "Finder" to get the index of the second Finder window due to conversational nature of AppleScript
tell application "Finder" to close every window
tell application "Finder" to open the startup disk
tell application "Finder" to get the name of front Finder window
tell application "Finder" to get the index of Finder window "Macintosh HD"
tell application "Finder" to get the index of the Finder window before the last Finder window
tell application "Finder" to set the index of the last Finder window to 1
Nested Tell Blocks
Tell application “Finder” close every window tell the front Finder
window set the current view to column view end tell end tell
activate
reopen
restore
get
set
open
close
select
beep
CurrentTab
set folderPath to ((POSIX path of thisFolder) as string)
To Do
Folder Actions?
Clickable script in Finder toolbar?
Open selected Finder file in your favorite text editor, like Sublime
New text document here in Finder folder or desktop
tellapplication"System Events"totell process "iTerm2"to keystroke "h" using command down
tellapplication"Mail"
launch -- launch opens the app in the background, rather than 'activate' which brings it frontmostset miniaturized ofevery window totrueendtell