This is a follow-up to TJ Luoma's answer, which I hope will fix the issues with the .plist
definition that stops the job from being run in the correct manner.
From man
launchd.plist
:
WatchPaths <array of strings>
This optional key causes the job to be started if any one of the listed
paths are modified.
IMPORTANT: Use of this key is highly discouraged, as filesystem event
monitoring is highly race-prone, and it is entirely possible for modifications to be missed. When modifications are caught, there is no guarantee that the file will be in a consistent state when the job is launched.
StartOnMount <boolean>
This optional key causes the job to be started every time a filesystem is
mounted.
Here is a revised .plist
definition incorporating StartOnMount
in favour of WatchPaths
. It is currently labelled "local.startOnMount.iTunes"
, which, if unchanged, means the file should be saved at ~/Library/LaunchAgents/local.StartOnMount.iTunes.plist
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.StartOnMount.iTunes</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>if (list disks) contains "Media" then ¬
run application id "com.apple.iTunes"</string>
</array>
<key>RunAtLoad</key>
<false/>
<key>StandardErrorPath</key>
<string>/tmp/local.StartOnMount.iTunes.stderr</string>
<key>StandardOutPath</key>
<string>/tmp/local.StartOnMount.iTunes.stdout</string>
<key>StartOnMount</key>
<true/>
</dict>
</plist>
The ProgramArguments
key can alternatively be more akin to TJ's original bash
command:
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>[[ -d "/Volumes/Media" ]] && open -jg -b "com.apple.iTunes"</string>
</array>
However, in testing, I found that, if iTunes is already running and is hidden, then callng the open
command—even with the -j
and/or -g
option(s)—brings iTunes to the foreground. So I elected to do use osascript
to perform the same actions, but in a more consistent manner.
This new .plist
appears to resolve the issues, and only runs the job when a filesystem is mounted, which in turn only performs an action if it's the correct disk name. To replace:
cd ~/Library/LaunchAgents
launchctl unload com.tjluoma.itunes-on-mount.plist
launchctl load local.StartOnMount.iTunes.plist
However, if anything is not performing as expected, and it's not immediately obvious why, then look in the /tmp/
folder for file(s) named local.StartOnMount.iTunes.stderr
and/or local.StartOnMount.iTunes.stdout
:
cat local.StartOnMount.iTunes.stderr
EDIT: Post Catalina Bonus
Added by @dunxd on 2021-03-18
Apple split iTunes into separate apps (Music, TV and Podcasts) with the launch of Catalina. It is possible to adjust the above script to launch Music and TV in one go by adding extra strings to the ProgramArguments array like so:
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>if (list disks) contains "Media" then run application id "com.apple.music"</string>
<string>-e</string>
<string>if (list disks) contains "Media" then run application id "com.apple.TV"</string>
</array>
EDIT: Refinement of "Post Catalina Bonus"
Added by @CJK on 2021-04-17
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>
if (list disks) does not contain "Media" then return
run application id "com.apple.music"
run application id "com.apple.TV"
</string>
</array>