How to run an AppleScript *only* on a machine with a specific external drive?

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'.

Any ideas please?

I use the Computer Name in an If conditional to keep certain actions from running on certain machines. Sounds like that would work in your case.

Hi, @TomasWolpertinger and @drdrang,

The wiki page has this for working with enable/disable macro groups on different computers:

Disable for This Mac

If you are syncing your macros with another Mac, Macro Groups can be disabled specifically on this Mac.

https://wiki.keyboardmaestro.com/Macro_Groups

Hey Jasper,

It's doing exactly what you told it to do, and AppleScript is throwing an error just as it's supposed to do under the circumstance.

Technical Note TN2065: do shell script in AppleScript

@drdrang's suggestion is most relevant for Mac # 2.

If Then Else.kmactions (835 B)
(Actions import directly into the macro being edited.)

Keyboard Maestro Export

You can also alter how an Execute an AppleScript action handles errors.

image

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.

-Chris

Thank you both—I thought that I'd have to just WFH to avoid this problem!

I've got my Macro working nicely across machines and learned a new Keyboard Maestro trick to boot.

I went for the Conditional action that checked the machine's name.