AppleScript

Concepts – Terminology

A little bit clarification on what’s what.

 

AppleScript Script Editor Automator Shortcuts
What A language, a technology An application An application An application
For Scripts executable on macOS computers For editing, and running/debugging, scripts written in (e.g.) AppleScript,  JavaScript for Automation (since macOS 10.10), and more. “A graphical, modular editing environment in which workflows are built up from actions. It is intended to duplicate many of the functions of AppleScript without the necessity for programming knowledge. Automator has an action specifically designed to contain and run AppleScripts, for tasks that are too complex for Automator’s simplified framework.[34]” [src] Shortcuts (formerly Workflow) is a visual scripting application developed by Apple and provided on their iOSiPadOSmacOS, and watchOS operating systems.” [src]
History From 1993, System 7.1 From 1997 From 2005, 10.4 Tiger From 2014 (iOS), 2021 (macOS)
Usefulness Great to know for advanced users familiar with programming concepts! Quite useful for advanced users, writing scripts but also for learning more on scripting capabilities in different applications (File > Open Dictionary…, on e.g. Finder, Mail, et c)
JandP.biz This page (This page) Automator (/is/apps/macos/automator) (Only this page, at this time)

 

Developer Resources

Current (Apple)

AppleScript Finder Guide (1994)

Even if old, assume still apply to at least some valuable degree. Backward compatibility and such.

AppleScript Finder Guide – applescriptlibrary.files.wordpress.com/2013/11/applescript-finder-guide.pdf (1994)

Archive (Apple)

 

Topics

Basics

developer.apple.com/library/archive/documentation/AppleApplications/Conceptual/AutomatorTutorialAppleScript/WritingScript/WritingScript.html

 

Dialogs

 

File System

 

AppleScript and POSIX paths

Referencing Files and Folders –
developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReferenceFilesandFolders.html

The below initially based on AppleScript and POSIX paths at satimage.fr/software/en/smile/external_codes/file_paths.html

  • Where POSIX uses the slash / as the separator for directories, AppleScript uses the colon :.
  • You can use colon : in a POSIX file name, and you can use slash / in an AppleScript file name: they translate one into the other.
  • To translate a UNIX path (file or directory, valid or not) into an AppleScript file reference, use POSIX filePOSIX file returns a file reference, that your script can use directly to create or use the file.set p to "/usr/local/bin/"
    set a to POSIX file p
    -- file "Macintosh HD:usr:local:bin:"
  • To translate an AppleScript path (file or directory, valid or not) into a POSIX path use POSIX path of.
    set a to "Macintosh HD:usr:local:bin:"
    set p to POSIX path of a
    -- "/usr/local/bin/"
    POSIX path also understands AppleScript’s file references.set a to alias "Macintosh HD:usr:local:bin:"
    set p to POSIX path of a
    -- "/usr/local/bin/"
    set a to file "Macintosh HD:usr:local:bin:"
    set p to POSIX path of a
    -- "/usr/local/bin/"
  • A file name may include characters which need to be escaped to be passed to a shell command. Use quoted form to get the quoted form of a string.

    set a to path to application support
    set p to POSIX path of a
    --  "/Library/Application Support/" -- contains a space
    do shell script "ls " & p -- does not work
    --  ""
    do shell script "ls " & quoted form of p -- works
    —  Result:
    Adobe
    Lexmark Z53 Files
    etc.
  • The reserved directories may have various names (for localization purposes). Instead of accessing those directories with their names, you use keywords and the path to command. Most of the keywords are given in the dictionary of StandardAdditions.osax (select path to, press ⌘⇧F).path to accepts a from parameter to specify either the user domainlocal domain or system domain. Compare to the example just above.set a to path to application support from user domain
    set p to POSIX path of a
    --  "/Users/<login>/Library/Application Support/"
    For the exhaustive list of the keywords recognized by path to, use the following tool: User Scripts > More Smile commands > Scripting > Path to menu.

 

Paths

get home folder from applescript – google.com/search?q=get+home+folder+from+applescript

Getting path to user’s home directory – discussions.apple.com/thread/1652165

Size File System Items, Finder …

tell application \”Finder\” to get (size of item “E+”

tell application \”Finder\” to get (size of item (
– books.google.com/books?id=6YpoLXJBIpcC&…

An AppleScript to calculate item sizes in the Finder – hints.macworld.com/article.php?story=20060608030023113

 

Lists

Logging

Permissions

Shell

apple.stackexchange.com/questions/344255/applescript-shell-script

google.com/search?q=osascript+bash

User Interaction from bash Scripts –
scriptingosx.com/2018/08/user-interaction-from-bash-scripts/

gist.github.com/scriptingosx/c086e8ffb77fe1525d179e8648e5ee02

 

Windows, and Apps

 

Examples

AppleScript from Terminal AND Getting Info on Finder Items (File System)

I.e., exemplifies two completely different topics:

  1. How to run AppleScript code from terminal (bash env).
  2. How to retrieve information as shown in Finder’s Get Info dialog for items in file system, for instance SIZE of a file or folder.
$ osascript -e 'tell application "Finder" to get (physical size of folder (POSIX file "/Applications" as alias)) / (1000 ^ 3) as text'
43.91016448

$ osascript -e 'tell application "Finder" to get (physical size of folder (POSIX file "/Applications/Xcode.app" as alias)) / (1000 ^ 3) as text'
110:118: execution error: Finder got an error: Can’t make alias "Macintosh HD:Applications:Xcode.app:" into type folder. (-1700)

$ osascript -e 'tell application "Finder" to get (physical size of file (POSIX file "/Applications/Xcode.app" as alias)) / (1000 ^ 3) as text'
16.584630272

$ osascript -e 'tell application "Finder" to get (size of file (POSIX file "/Applications/Xcode.app" as alias)) / (1000 ^ 3) as text'
31.616003904

$ osascript -e 'tell application "Finder" to get (size of item (POSIX file "/Applications/Xcode.app" as alias)) / (1000 ^ 3) as text'
31.616003904

$ osascript -e 'tell application "Finder" to get (size of item (POSIX file "/Applications" as alias)) / (1000 ^ 3) as text'
59.391803353

$ osascript -e 'tell application "Finder" to get (physical size of item (POSIX file "/Applications/" as alias)) / (1000 ^ 3) as text'
43.91016448

Automator Action Retrieving and Displaying Finder Get Info – data

Get Info.workflow (an Automator Quick Action)

on run {input, parameters}
  my dofunc()
  return input
end run

on dofunc()
  tell application "Finder"
    -- Get information of 1st item from the list of all selected folders. 
    -- To get info of all folders put the code in loop 
    set selectedItem to (item 1 of (get selection)) 
    set informationList to {}
    copy ("FileName: " & displayed name of selectedItem) to end of informationList
    copy ("Type: " & kind of selectedItem) to end of informationList 
    copy ("Size: " & size of selectedItem & " (" & physical size of selectedItem & ")") to end of informationList 
    copy ("Where: " & (selectedItem as alias) as string) to end of informationList 
    copy ("CreationDate: " & creation date of selectedItem) to end of informationList 
    copy ("LastModified: " & modification date of selectedItem) to end of informationList 
    -- Full name of file or folder with extension 
    copy ("FullName: " & name of selectedItem) to end of informationList 
    copy ("IsLocked: " & locked of selectedItem) to end of informationList 
  end tell 
  set {delimiter, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return} 
  set infoAsString to informationList as string 
  display dialog infoAsString buttons {"OK"} default button "OK" -- `cancel button "OK" 
end dofunc 

##