himbeles / mac-device-connect-daemon

Run shell script or executable triggered by device detection on a Mac.
MIT License
61 stars 12 forks source link

What about detaching? #1

Open deg0nz opened 6 years ago

deg0nz commented 6 years ago

Hi,

first of all: Thanks for this really helpful piece of code and the tutorial! It works like a charm.

I use it to trigger a script when my docking station is connected. The script I'm triggering turns off WiFi, activates another sound output device and other stuff like this..

Now, I'd like to trigger a script that reverses all these things when I disconnect the Mac from the docking station.

After searching the net for some hours now I can't find an event key that triggers on detaching. I tried to use com.apple.device-detach and com.apple.device-remove but they don't seem to work...

Do you have any idea how this could be achieved?

Thanks and greetings Beh

himbeles commented 6 years ago

Hi,

glad you could make use of the script! Unfortunately, I have no idea on how apple called that event, if a handler even exists. Their documentation is quite lacking in that regard ...

Good luck and best regards

himbeles commented 6 years ago

I have found this discussion on apple forums mentioning the handling of disconnects on an abstract level. https://forums.developer.apple.com/thread/108927#332600

tommyknows commented 2 years ago

Looking for that as well, trying to change Keyboard layouts when plugging in my external USB keyboard, and changing back when unplugging.

If anyone found a solution, please let us know :-)

himbeles commented 1 year ago

Please check out my app "Stecker". In addition to device attachment, the app can also detect device detachment. It triggers the execution of selected Shortcuts from the macOS Shortcuts App. The app is very lightweight and also only acts on IOKit notifications. In contrast to the launchd approach, the direct use of the IOKit framework permits to filter for additional types of com.apple.iokit.matching events, such as device detachment. I have not found a way to filter for that type of event in launchd plists.

sv-vv commented 1 week ago

@deg0nz The name com.apple.device-attach does not have any special meaning. It is just an identifier that the author of the original example chose to use; its value is misleading and it made you think that com.apple.device-detach could be its counterpart. It is not.

You can use whatever value you want there; it is returned by the call xpc_dictionary_get_string(object, XPC_EVENT_KEY_NAME) on line 32.