You can use launchd. Try man launchd and man launchd.plist.
It seems that launchd can work with USB events, even though this feature is poorly documented.
I found it on: man xpc_set_event_stream_handler
Here's an example. If you put the following into: ~/Library/LaunchAgents/com.example.plist
, your program should start when a USB device is connected.
<?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.program</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/program</string>
</array>
<key>LaunchEvents</key>
<dict>
<key>com.apple.iokit.matching</key>
<dict>
<key>com.apple.device-attach</key>
<dict>
<key>idProduct</key>
<integer>1234</integer>
<key>idVendor</key>
<integer>1337</integer>
<key>IOProviderClass</key>
<string>IOUSBDevice</string>
<key>IOMatchLaunchStream</key>
<true/>
</dict>
</dict>
</dict>
</dict>
</plist>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…