2

I've set up a bunch of scripts that need to happen when I connect my thunderbolt dock at home (disable wifi, enable bluetooth, ...).

To detect the thunderbolt dock I make use of the following launchctl service:

<?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>romeovs.dock.agent</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/romeovs/.bin/dock/run.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>LaunchEvents</key>
    <dict>
        <key>com.apple.notifyd.matching</key>
        <dict>
            <key>com.apple.system.config.network_change</key>
            <dict>
                <key>Notification</key>
                <string>com.apple.system.config.network_change</string>
            </dict>
        </dict>
    </dict>
</dict>
</plist>

This service listens to launchd for a network change to happen (detecting the wired network that is connected to the dock). This setup seems to work but it is really slow to detect and act when the dock is connected.

It already takes a long time for the dock network to be set up, but after that it also takes another 5 seconds before the script actually gets called.

Is there a way to make the service detect the network changes any faster (without setting up a polling service)?

1

The delay is most likely to be within the notification centre; the notifyd service is not time critical.

Consider using a process observing IOKit level notifications. At the IOKit layer notifications are immediately dispatched and designed for processes reacting to hardware changes.

Trying using Growl and the HardwareGrowler addition to trigger your script.

  • I'm not fond of installing Growl again so I'm investigation the IOKit suggestion you gave. Is it possible to listen to hardware actions using launchd? – romeovs Jul 30 '14 at 16:03
  • The launchd.plist manual page suggests it is not possible without an external process or tool; developer.apple.com/library/mac/documentation/Darwin/Reference/… – Graham Miln Jul 30 '14 at 20:01
  • This answer seems to indicate that it is possible to detect usb events from within luanchd. It would seem plausible that it could also detect thunderbolt events, but I can't seem to find the right syntax. – romeovs Jul 31 '14 at 7:56
  • Consider asking a second question specifically for this functionality. You will be more likely to attract others who know the answer. – Graham Miln Jul 31 '14 at 15:07
  • Yes I found the solution already. You can see how I did it in this follow up question I had. – romeovs Jul 31 '14 at 17:26

You must log in to answer this question.

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