How to detect USB disconnect and execute shell script mac osx

1k Views Asked by At

With below plist added to /Library/LaunchAgents, it will launch my app on USB (custom device) connected.

<?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>com.company.cable</string>
    <key>LaunchEvents</key>
    <dict>
        <key>com.apple.iokit.matching</key>
        <dict>
            <key>com.apple.device-attach</key>
            <dict>
                <key>IOMatchLaunchStream</key>
                <true/>
                <key>IOProviderClass</key>
                <string>IOUSBDevice</string>
                <key>idProduct</key>
                <integer>124</integer>
                <key>idVendor</key>
                <integer>11414</integer>
            </dict>
        </dict>
    </dict>
    <key>LaunchOnlyOnce</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/open</string>
        <string>-W</string>
        <string>/Users/karthik/Desktop/MYApp.app</string>
    </array>
    <key>StartOnMount</key>
    <true/>
</dict>
</plist>

But on USB disconnect, irrespective of app status(opened/closed), i need to reload the above plist file to make deamon back to listen for USB connect. I tried quite few possiblities, dint work yet. If i change key LaunchOnlyOnce false, the application keeps reopening on app close while cable is still connected, don't want that to happen.

0

There are 0 best solutions below