launchd at login and watchpath
So I set up a launchd .plist and with an applescript, that will start up a certain program any time a certain Volume is mounted. This is my .plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>WatchVolumes</string>
<key>LowPriorityIO</key>
<true/>
<key>Program</key>
<string>/usr/bin/osascript</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/path/to/script/WatchingVolumes.scpt</string>
</array>
<key>ServiceDescription</key>
<string>Run application when volume is mounted</string>
<key>WatchPaths</key>
<array>
<string>/Volumes</string>
</array>
</dict>
</plist>
This works perfectly. As long as this user is logged in. No matter which user is active, it will start the program when I connect the specific drive (as designed).
However, when I login to the user, and the drive is already connected (either because I plugged it in, before logging in to that user, or because it was still connected after rebooting the computer), the application is not started. Which seems logic, as there is no change to /Volumes when I login.
The desired behavior is for the application to run, whenever that drive is connected.
Is there a way to trigger the launchd on both login and when the watchpath changes? Or do I need to create a second launchd just for login? Or is there another even better approach to this problem?
ps. Adding the application to the list of applications that is run on login is not desirable, because the application should not run if the drive is not connected.
MacBook