Evan Harmon - Memex

AppleScript

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.
wikipedia:: AppleScript

Meta

Syntax

Escaping Characters in a path

  • quoted form of

Environment

Running

  • Run Applescript remotely
    • tell application "System Events" of machine "eppc://ipaddress" to say "hello"
  • Script Editor - can run scripts directly
  • Save as script file
  • Run as an Applet/Application
  • Keyboard shortcut
    • Create a new service in automator (must be a service)
      • Set input to any
      • Drag run AppleScript into window and name it something descriptive
      • Save it and name the actual script something descriptive
    • then create custom keyboard shortcut to that automator action
      • It should be in Services - General and it should also be available from anywhere via that shortcut, plus via the Mac Services menubar
      • Sometimes the keyboard shortcut won't work and you need to invoke the script in the Finder's scripts menubar and then the keyboard shortcut will work
  • Application’s script menu (like DEVONThink)
  • System wide scripts menubar
  • Put Automator app in Finder Toolbar
    • Open Finder and go to the folder where you saved the Automator app. Hold down the Option and Cmd keys and drag the app file to the toolbar.
    • omnioutliner:///open?focus=mXLVo9hjiGA&row=bx7wPYe14LQ

Shell Scripts

  • do script
    • goes to Terminal?
  • do shell script
    • Uses system shell instead of Terminal BASH?
    • 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

Sources

Inbox

tell application "System Events" to tell process "iTerm2" to keystroke "h" using command down
tell application "Mail"
	launch -- launch opens the app in the background, rather than 'activate' which brings it frontmost
	set miniaturized of every window to true
end tell
tell application "Arc"
    set currentURL to URL of active tab of window 1
    set currentTitle to title of active tab of window 1
end tell
AppleScript
Interactive graph
On this page
AppleScript
Meta
Syntax
Escaping Characters in a path
Environment
Running
Shell Scripts
Basic Structure
Objects
Control
Conditionals
Sleep, delay
Loops
Variables
I/O
Send Keystrokes to Application
Specific
Tell application “Finder” close every window tell the front Finder
To Do
Sources
Inbox