# 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](https://en.wikipedia.org/wiki/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 - Applets - can be run like any other file/application, from the dock, etc. - https://stackoverflow.com/questions/281372/executing-shell-scripts-from-the-os-x-dock - 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 - 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 - Tagging - http://www.russellbeattie.com/blog/fun-with-the-os-x-finder-and-applescript ## Sources - https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/index.html\#//apple\_ref/doc/uid/TP40016239-CH56-SW1 - https://alvinalexander.com/osx ## Inbox ```applescript tell application "System Events" to tell process "iTerm2" to keystroke "h" using command down ``` ```applescript 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 ``` Copy all tabs of browser window as markdown - [Copy name and URL of all the opened tabs of the frontmost Arc Browser window - Questions & Suggestions - Keyboard Maestro Discourse](https://forum.keyboardmaestro.com/t/copy-name-and-url-of-all-the-opened-tabs-of-the-frontmost-arc-browser-window/35178) ```applescript 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 ``` [Arc AppleScript API](https://browserinc.notion.site/Arc-AppleScript-API-897272210c784627ba3925a7610b02b6)