Concepts – Terminology
A little bit clarification on what’s what.
- AppleScript = a scripting language
- en.wikipedia.org/wiki/AppleScript
- “For the time being, AppleScript remains one component of macOS automation technologies, along with Automator, Shortcuts, Services, and shell scripting.”
- en.wikipedia.org/wiki/AppleScript#Development_tools
- en.wikipedia.org/wiki/AppleScript
- Script Editor (a.k.a. AppleScript Editor, /Applications/Utilities/) = an application for editing sources, like written in AppleScript
- en.wikipedia.org/wiki/AppleScript_Editor
- support.apple.com/guide/script-editor/about-script-editor-scpedt6935/mac
- “You can use various scripting languages, including AppleScript, JavaScript for Automation, shell scripts, and also some third-party scripting languages.”
- google.com/search?q=applescript+editor
- Automator (/Application) = an application for creating/editing and running workflows, which COULD be based on / using AppleScript, but not necessary.
- Shortcuts (/Applications/)
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 iOS, iPadOS, macOS, 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)
- developer.apple.com/documentation
- (Hard to – can’t – find anything on AppleScript here…)
- developer.apple.com/documentation/technologies
- (Hard to – can’t – find anything on AppleScript here…)
AppleScript Finder Guide (1994)
Even if old, assume still apply to at least some valuable degree. Backward compatibility and such.
Archive (Apple)
- developer.apple.com/library/archive/navigation/
- Mac Automation Scripting Guide – developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/index.html
- Mentions AppleScript and JavaScript
- AppleScript
- JavaScript for Automation (JXA)
- 2014-10-17 New feature in OS X v10.10
- 2015-09-17 Updated to include new JavaScript for Automation features in OS X 10.11.
- developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html
- google.com/search?q=javascript+for+automation
- How to Use JavaScript for Automation – DEVONtechnologies – devontechnologies.com/blog/20211005-jxa-javascript-for-applications
- The unexpected return of JavaScript for Automation – scriptingosx.com/2021/11/the-unexpected-return-of-javascript-for-automation/
Topics
Basics
Dialogs
- google.com/search?q=applescript+display+dialog
- Good overview at applescript.fandom.com/wiki/Display_Dialog.
- google.com/search?q=applescript+non-modal+dialog
- Displaying Dialogs and Alerts –
developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/DisplayDialogsandAlerts.html
File System
- google.com/search?q=applescript+get+list+of+files+in+folder
- List all files in a folder –
discussions.apple.com/thread/536193 - applescript process folder action inputs
- Watching Folders –
developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/WatchFolders.html - applescript get name of file
AppleScript and POSIX paths
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 file. POSIX 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:"
POSIX path also understands AppleScript’s file references.
set p to POSIX path of a
-- "/usr/local/bin/"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 domain, local domain or system domain. Compare to the example just above.
set a to path to application support from user domain
For the exhaustive list of the keywords recognized by path to, use the following tool: User Scripts > More Smile commands > Scripting > Path to menu.
set p to POSIX path of a
-- "/Users/<login>/Library/Application Support/"
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&…
Lists
Logging
- automator log
- applescript log
- stackoverflow.com/questions/13653358/how-to-log-objects-to-a-console-with-applescript
Permissions
- google.com/search?q=macos+applescript+service+permissions
- Command and Scripting: AppleScript, attack.mitre.org/techniques/T1059/002/
- MacOS – Can only run AppleScript from Automator, itectec.com/askdifferent/macos-can-only-run-applescript-from-automator/
- stackoverflow.com/questions/32907909/allow-applescript-script-to-run-without-asking-for-permission
- apple.stackexchange.com/questions/368117/how-to-add-a-service-to-accessibility-permissions
- google.com/search?q=A+unknown+token+can%E2%80%99t+go+here.
- reddit.com/r/applescript/comments/9709ln/a_unknown_token_cant_go_after_this_identifier/
- stackoverflow.com/questions/55735382/how-to-fix-syntax-error-a-unknown-token-can-t-go-here-when-running-exec-comm
- google.com/search?q=osascript+script+A+unknown+token+can%E2%80%99t+go+here.
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
- google.com/search?q=applescript+count+of+windows
- mattcrampton.com/blog/applescript_to_gather_all_windows_on_osx/
- apple.stackexchange.com/questions/9415/count-the-number-of-open-windows
- google.com/search?q=show+macos+applications+windows+programmatically
Examples
AppleScript from Terminal AND Getting Info on Finder Items (File System)
I.e., exemplifies two completely different topics:
- How to run AppleScript code from terminal (bash env).
- 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
##