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:


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.


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. :)

  • OK I just updated the comment with a link to the video. – Vladimir Aug 3 '14 at 16:53
  • 1
    I really appreciate the effort you put into making this video for me. This solution, however, does not go into a mechanic for detecting a disconnecting device. Also, there has to be a storage device attached to the dock for this solution to work - and events will also trigger if storage devices other than those on the dock are connected. I'm giving you a +1 for the effort but this is not the answer I'm looking for. – romeovs Aug 3 '14 at 17:59
  • Actually, my solution doesn't need a device to be attached to the dock. (You are talking about the OSX "Dock" at the bottom of the screen, right?) – Vladimir Aug 3 '14 at 18:02
  • And I see what you mean about the detaching thing, I misread your question, sorry. – Vladimir Aug 3 '14 at 18:03
  • Yes, I see now that my reply was completely off topic. Sorry about ;) – Vladimir Aug 3 '14 at 18:05

When I was searching for a similar thing myself before, I heard about a bit of a messy solution that will run your code anytime a device is "Ejected."

What they did was find the OSX program that normally performs an USB Eject operation, and renamed it to something like Eject_old. Then they wrote their own program, which first runs the code they want and then calls the Eject_old program.

They named this new program whatever the other one was called, so now every time they eject a device it runs their program first. Of course this probably won't help if your dock is disconnected without being ejected first.

If you think this might be able to help with what you'd like to do, let me know and I can look into it some more.


PS. There are third party apps that do some similar things. Maybe you can contact them directly and ask how their programs work.

  • You can then have you code check which device is being ejected to determine if you need to do something special for it. – Vladimir Aug 3 '14 at 18:34

You must log in to answer this question.

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