Trigger on DVD mount?

I can't seem to figure out how to trigger a macro when a DVD is mounted.

See Mounted Volume trigger.

Thanks JM, but that seems too generic/too specific: It can trigger on any mounted volume or any single mounted volulme. But I'm looking for "any DVD"

Trigger on any mounted value and then determine if it is a DVD. I'm not sure how exactly to do that, but that should be possible using some sort of script.

Yes Peter, that's where I was heading. I suppose the presence of the VIDEO_TS directory might be enough to call it a DVD.

Thanks!

Well… the first way that I can think of… it’s fairly terrible / clunky, but it should mostly work:

Option 1: Automator App

System Preferences.app will let you have an app launch whenever you insert a "blank" or "video" DVD.

You could use Automator or Script Editor to create an "app" which does nothing except run a Keyboard Maestro macro and then exits, and set that app to run when one is found.

That will not help you for data DVDs, unfortunately. Plus, did I mention it's ugly?

I don’t really like this idea. Forget I mentioned it.

Option 2: Check for 'read-only'

A better way, I think, I would look for drives which are mounted as "read-only" because nothing is mounted as "read-only" except CD/DVDs/etc.

(Well, and I guess the system partition in Catalina, but that would be easy to work around.)

#!/usr/bin/env zsh -f

MOUNT_POINT=$(/sbin/mount \
			| fgrep 'read-only' \
			| sed 's#.*/Volumes/#/Volumes/#g ; s# (.*##g')

if [[ "$MOUNT_POINT" == "" ]]
then
	echo "No mount point found." >>/dev/stderr
	exit 1
fi

echo "$MOUNT_POINT"

exit 0

That’s better, but I still don’t like it. The read-only thing is a kludge, and trying to get the mount name that way is a kludge too.

Option 3: Now we’re talking

Oh! I think I have it!

The command drutil is used to interact with optical drives. We can use that to have it report to us if a disc is mounted via the optical drive.

Unfortunately we can’t do this entirely directly, but we can get there.

drutil status | awk -F' ' '/Name:/{print $NF}'

will tell us the device name that the optical drive is using. If it’s empty, it means there’s no disc mounted via the optical drive,

If it is not empty, we can use that together with the diskutil command to get the mount point of the device.

If there is a mount point, then we can run our macro.

Here's a little script that I whipped together as an example:

#!/usr/bin/env zsh -f

NAME="$0:t:r"

if [[ -e "$HOME/.path" ]]
then
	source "$HOME/.path"
else
	PATH='/usr/local/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'
fi

DEVICE=$(drutil status | awk -F' ' '/Name:/{print $NF}')

if [[ "$DEVICE" == "" ]]
then
	echo "$NAME: No CD/DVD mounted" >>/dev/stderr
	exit 1
fi

MOUNT_POINT=$(diskutil list -plist \
			| fgrep -A 10 "$DEVICE:t" \
			| awk '/<key>MountPoint<\/key>/{getline; print }' \
			| sed 's#.*<string>##g ; s#</string>##')

if [[ "$MOUNT_POINT" == "" ]]
then
	echo "$NAME: No mount point found for '$DEVICE'." >>/dev/stderr
	exit 1
else
	echo "$MOUNT_POINT"
fi

# Here you could call a Keyboard Maestro macro

osascript -e "tell application \"Keyboard Maestro Engine\" to do script \"12356ABCD-1234-ABCD-5678-8675309\" with parameter \"$MOUNT_POINT\""

# replace '12356ABCD-1234-ABCD-5678-8675309' with the UUID of your Macro

exit 0
#EOF

Now the question is: “How do we run the script?”

I think the best way would be to have Keyboard Maestro watch for changes to /Volumes/ and run this script after each change. If it finds a mount, we can tell it to run another macro, as shown at the end of the script.

That’s where I’d start.

Let me know if you have questions -- I’d be surprised if you didn’t! :slight_smile:

3 Likes

Hey, that's great! I'm gonna call that one "solved". (I haven't implemented yet, but I'm sure I can make #3 work for me.)

Thanks very much, this is solve one of my new Catalina-inspired issues.