Volume Mount Trigger for Shortcuts
One of the most useful features of Shortcuts is its ability to respond to specific events or “triggers” that occur: such as achieving a time of day, or reaching a location. The following trigger example enables the execution of a chosen shortcut when a volume or disk image, is mounted on your computer.
On macOS, a volume mount trigger can be provided by a “stay-open” AppleScript applet that “watches” your system and runs a pre-chosen shortcut when a volume is mounted, passing the path to mounted volume to the shortcut when it is executed..
Trigger • Volume Mount Trigger for Shortcuts
Here is an example workflow that uses the “Run AppleScript” action to set the display of the contents of the mounted volume in its corresponding Finder window. (DOWNLOAD SHORTCUT)
1 Shortcut Input • The shortcut is set to receive text.
2 No Text Alert • If for some reason no text is passed into the shortcut, it will post an alert.
3 “Run AppleScript” action • This action is passed the shortcut input as its input.
4 AppleScript script • This AppleScript script converts the input into a file reference that is used to have the Finder application open the mounted volume and set the display of its contents in the frontmost Finder window.
NOTE: Install the shortcut and then choose it from the list of shortcuts displayed when the AppleScript applet is launched the first time.
The “Volume Watcher” Applet
The following is the AppleScript code of the “stay-open” applet that registers itself with macOS to receive a notification when a volume is mounted on the desktop.
This applet is written in AppleScriptObj-C which provides access the macOS frameworks through a “Cocoa Bridge” that exposes most of the functionality of the operating system to scripts.
Many thanks to the Script Debugger application for providing the tools for designing, editing, and notarizing AppleScript applications.
NOTE: Should you choose to edit or alter this script yourself, be sure to save it as a “Stay Open” applet that remains active until it is told to quit.
AppleScript Applet
use AppleScript version "2.4" -- Yosemite (10.10) or lateruse framework "Foundation"use framework "AppKit"use scripting additions-- classes, constants, and enums usedproperty NSCharacterSet : a reference to current application's NSCharacterSetproperty NSString : a reference to current application's NSStringproperty NSWorkspace : a reference to current application's NSWorkspaceproperty NSUserDefaults : a reference to current application's NSUserDefaultsproperty NSArray : a reference to current application's NSArrayproperty ignoredVolumes : {"home", "net", "Recovery HD"}global workflowNameon run-- SET VALUE OF PROPERTY TO STORED WORKFLOW PATHmy initializeDefaults()-- CHECK STATUS OF STORED WORKFLOW PATHif (workflowName as text) is "" thenmy promptForWorkflow()else-- DISPLAY OPENING DIALOGtell me to activatedisplay dialog "Shortcut: " & return & return & (workflowName as text) buttons {"Quit", "Change", "Start"} default button 3 with title (name of me)set buttonPressed to the button returned of the resultif the buttonPressed is "Change" then-- prompt for a new workflowmy promptForWorkflow()else if buttonPressed is "Quit" thentell me to quitelse -- check to see if workflow file existsif my workflowCheck() is false thentell me to activatedisplay alert "MISSING RESOURCE" message "There is no shortcut with the indicated name:" & ¬return & return & workflowName buttons {"Select", "Quit"} default button 2if button returned of the result is "Quit" thentell me to quitend if-- prompt for a new workflowmy promptForWorkflow()end ifend ifend if-- REGISTER FOR NOTIFICATIONmy registerToReceiveNotification()end runon initializeDefaults()-- Initialize default if needed, retrieve current valuetell NSUserDefaults to set defaults to standardUserDefaults()tell defaults to registerDefaults:{workflowName:""}-- set the value of the applet property to the stored valuetell defaults to set workflowName to (objectForKey_("workflowName")) as textend initializeDefaultson workflowCheck()set workflowTitles to (do shell script "shortcuts list|sort")set AppleScript's text item delimiters to returnset workflowTitles to every text item of workflowTitlesset AppleScript's text item delimiters to ""if workflowTitles contains workflowName thenreturn trueelsereturn falseend ifend workflowCheckon promptForWorkflow()-- prompt user to choose a workflowtryset workflowTitles to (do shell script "shortcuts list|sort")set AppleScript's text item delimiters to returnset workflowTitles to every text item of workflowTitlesset AppleScript's text item delimiters to ""if workflowTitles is {} thenbeepdisplay alert "Missing Resources" message "There are no saved shortcuts."error number -128end ifset the dialogResult to (choose from list workflowTitles with prompt "Choose shortcut to run when volume is mounted:")if dialogResult is false then error number -128set workflowName to dialogResult as stringtell NSUserDefaults to set defaults to standardUserDefaults()tell defaults to setObject:workflowName forKey:"workflowName"on errortell me to quitend tryend promptForWorkflowon registerToReceiveNotification()-- create an instance of the Shared Workspaceset workspace to NSWorkspace's sharedWorkspace()-- identify the Shared Workspace's notification centerset noteCenter to workspace's notificationCenter()-- register notification handlers for dealing with volume mountingnoteCenter's addObserver:me selector:"volumeWasMounted:" |name|:"NSWorkspaceDidMountNotification" object:(missing value)end registerToReceiveNotificationon volumeWasMounted:notif-- CHECK FOR WORKFLOW FILEif my workflowCheck() is false thenmy promptForWorkflow()end if-- get the path to the mounted volumeset mountedVolumePath to (notif's userInfo()'s NSWorkspaceVolumeURLKey's |path|())-- get last path itemset thisVolumeName to (mountedVolumePath's lastPathComponent()) as textif thisVolumeName is not in ignoredVolumes then-- display alertset currentApp to application (POSIX path of (path to frontmost application))tell me to activatedelay 1tell me to activatedisplay dialog "The volume “" & thisVolumeName & "” has been mounted on this system." & ¬return & return & "Do you want to run the associated shortcut?" buttons {"No", "Yes"} default button 2if the button returned of the result is "No" then error number -128-- RUN WORKFLOWtryset encodedWorkflowTitle to my encodeUsingPercentEncoding(workflowName)set encodedVolumePath to my encodeUsingPercentEncoding(mountedVolumePath as string)open location "shortcuts://run-shortcut?name=" & encodedWorkflowTitle & "&input=text&text=" & encodedVolumePathtell currentApp to activateon error errorMessageactivatedisplay alert "WORKFLOW EXECUTION ERROR" message errorMessagetell me to quitend tryend ifend volumeWasMounted:on encodeUsingPercentEncoding(sourceText)-- create a Cocoa string from the passed AppleScript stringset the sourceString to NSString's stringWithString:sourceText-- indicate the allowed characters that do not get encodedset allowedCharacterSet to NSCharacterSet's alphanumericCharacterSet-- apply the indicated transformation to the Cooca stringset the adjustedString to sourceString's stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet-- convert from Cocoa string to AppleScript stringreturn (adjustedString as string)end encodeUsingPercentEncoding
(⬇ see below ) When the applet is launched it displays a dialog for selecting or confirming the shortcut to be executed when a volume is mounted.
(⬇ see below ) A security confirmation dialog presented the first time the shortcut is executed. Approve this dialog to allow the applet to do its job. Once approved, you will not be prompted again.
(⬇ see below ) A confirmation alert that is displayed by the applet when a volume is mounted.