I built a Keyboard Maestro that very nicely sets up my work-from-home computer (Mac#1) in the morning (hides unwanted apps, opens my calendar, etc). One other thing it does is mounts an external drive that's attached to my Mac, which uses this 'Execute AppleScript' Action.
set myVolumeLabel to "External Drive"
tell application "Finder"
set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[0-9]*' "
do shell script "diskutil mountDisk " & diskDev
end tell
I'd like to use this macro Mac #2. Most of what it does would be useful there too, although there is no external drive to mount. The macro won't run and I get a "Macro Cancelled" notification:
KEYBOARD MAESTRO ENGINE
Macro Cancelled
Execute an AppleScript failed with script error: text- script:85:173: execution error: Finder got an error:
The command exited with a non-zero status. (1).
Macro Good Morning! copy" cancelled (while
executing Execute AppleScript).
I assume that this is because the AppleScript doesn't know what to do when it's asked to look for the external drive from Mac #1, which isn't on Mac #2.
I don't see anything in Keyboard Maestro's conditional action that would only run the AppleScript only if the external drive was plugged in (usually unmounted, in the morning). So I'm guessing it's either an AppleScript or a Shell script solution.
Presumably I'd need to address the external drive by its UUID because it's static (unlike 'disk2s1' or whatever it's called at the time). I don't see anything in the Terminal diskutil manual that would give something meaning 'if 780836D1-3383-3A98-A32B-BF36DD8B916E exists, mount it, else ignore'.
And you can handle that directly in the AppleScript itself.
set thisComputer to computer name of (system info)
if thisComputer = "MyComputerName" then
set myVolumeLabel to "NUTS!"
tell application "Finder"
set shCmdStr to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[0-9]*' "
do shell script shCmdStr
end tell
end if
OR
try
set myVolumeLabel to "NUTS!"
tell application "Finder"
set shCmdStr to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[0-9]*' "
do shell script shCmdStr
end tell
on error errMsg number errNum
# NULL
end try
There are more options of course, but that covers the basics.