Run action when device is detached I'm using a launchd agent to run a shell script whenever I connect my thunderbolt dock. I've been able to use IOKit notifications to detect when the dock is being attached, but I also want to run a shell script whenever the dock is being detached.

To detect the attachment of the dock I use com.apple.iokit.matching's device-attach event:

 &lt;key&gt;LaunchEvents&lt;/key&gt;
 &lt;dict&gt;
     &lt;key&gt;com.apple.iokit.matching&lt;/key&gt;
     &lt;dict&gt;
         &lt;key&gt;com.apple.device-attach&lt;/key&gt;
         &lt;dict&gt;
             &lt;key&gt;IOProviderClass&lt;/key&gt;
             &lt;string&gt;IOPCIDevice&lt;/string&gt;
             &lt;key&gt;idVendor&lt;/key&gt;
             &lt;integer&gt;21&lt;/integer&gt;
             &lt;key&gt;idProduct&lt;/key&gt;
             &lt;integer&gt;4&lt;/integer&gt;
             &lt;key&gt;IOMatchStream&lt;/key&gt;
             &lt;true/&gt;
             &lt;key&gt;IOMatchLaunchStream&lt;/key&gt;
             &lt;true/&gt;
         &lt;/dict&gt;
    &lt;/dict&gt;
&lt;/dict&gt;


There should be an event that is fired on detach too, although I haven't fond it yet. The apple documentation on this subject is deplorable.

I really want to avoid using a polling method that keeps checking if the device is still attached. Some better solutions must 

Also growl, ControlPlane and other similar apps are out of the question.

Edit:

To be clear. I want to run a shell script every time this specific dock is connected or disconnected, not whenever any device is attached/detached.
//////////////////////////////////////////////////////////////////////////////

EDIT: Upon rereading the question, I see that my answer was actually off topic.

//////////////////////////////////////////////////////////////////////////////

I have a solution for what you would like to do. It's actually really simple, but it would be to difficult for me to explain it in writing so I recorded a videos for you.

Trigger a USB event using Apple's Automator app

:)

PS. In the video I talk about UBS devices because I didn't realize you wanted this to work for Thunderbolt. But I'm pretty sure the solution will still work just as well for Thunderbolt. :)