2

I'm trying to create a LaunchDaemon that runs a script when a USB-C or Thunderbolt dock is connected. I've found several solutions to run a script when a specific device is connected. But I need a solution that runs a script when any dock is connected.

As virtually all docks include a USB Hub, I tried to identify those and include them in a LauchDeamon: No luck. Then I looked into just triggering a script on any USB device like this:

<key>LaunchEvents</key>
<dict>
        <key>com.apple.iokit.matching</key>
        <dict>
                <key>com.apple.device-attach</key>
                <dict>
                    <key>IOProviderClass</key>
                        <string>IOUSBHostDevice</string>
                        <key>IOMatchLaunchStream</key>
                        <true/>
                        <key>IOMatchStream</key>
                        <true/>
                </dict>
        </dict>
</dict>

(Not the complete files)

Basically the same approach I've found for specific devices, but removing the device identifiers. Also no luck.

I've found EventScript. This application can run a script when a USB device is attached. But I prefer a solution that leverages macOS built-in tools/capabilities.

0

1 Answer 1

0

I've been able to create a launch daemon that in my limited tests runs whenever any drive is mounted. However, this daemon runs in more cases than it should, as I'll explain later.

<?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>com.example.mountdrive</string>

    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>Drive mounted.</string>
    </array>

    <key>WatchPaths</key>
    <array>
        <string>/Volumes/</string>
    </array>
</dict>
</plist>

Save this code in ~/Library/LaunchAgents/com.example.mountdrive.plist and load it by running sudo launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.mountdrive.plist (unload by changing the subcommand to bootout)

When I have this daemon loaded and I plug in a portable storage device, I can hear my Mac say 'drive mounted' (turn up the volume or invoke another utility to test it yourself).

This watches for any changes in the /Volumes directory, which is where drives show up. Sadly this also means that this gets triggered whenever a device get UNmounted, and also when you plug in a device it gets triggered once and again when the mount is finished properly. Even though this means the event gets triggered more often than you said you want, I think that you can use this daemon to call a shell script to double-check whatever changed in the /Volumes is an event that you care about, and then perform the action that you want.

2
  • 1
    Thanks for the suggestion. I'm looking for a solution that works with Docks, alternatively HID devices or all USB devices. Therefore, your suggestion to work with volumes doesn't work for me
    – Volsk
    Commented Aug 3, 2022 at 7:15
  • I have given that some thought - what about monitoring plug and unplug on USB using e.g. a Phyton script and go from there? Commented Aug 3, 2022 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .