2017-06-14 5 views
0

Je voudrais lancer une commande shell lorsqu'un périphérique Bluetooth externe spécifique est connecté à mon Mac.Mac: commande de lancement lorsque l'appareil est connecté par Bluetooth

Une belle façon (sans installer un logiciel tiers) de le faire est par l'ajout d'un fichier plist dans ~/Library/LaunchAgents

Sur this page, il est un exemple de lancer un événement lorsque la connexion Wi-Fi se connecte à un Position spécifique. Cela se fait en regardant un fichier spécifique:

<key>WatchPaths</key> 
<array> 
    <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string> 
</array> 

Pensez-vous qu'il serait possible de faire la même chose avec les événements bluetooth?

Merci pour votre aide!

+0

Je trouve le fichier /Library/Preferences/com.apple.Bluetooth.plist. Mais ce fichier change un peu trop – iero

Répondre

0

Créez un fichier dans ~/Library/LaunchAgents contenant:

<key>Label</key> 
<string>Smartcard reader</string> 

<key>ProgramArguments</key> 
<array> 
    <string>/Users/USERNAME/Library/Scripts/script.bash</string> 
</array> 

<key>WatchPaths</key> 
<array> 
    <string>/Library/Preferences/com.apple.Bluetooth.plist</string> 
</array> 

Et nous pouvons /Users/USERNAME/Library/Scripts/script.bash tester si l'appareil connecté est le bon:

if [ $(system_profiler SPBluetoothDataType | grep "Smart Reader 2501" | wc -l) -eq 1 ] ; then 
    echo "Smartcard connected" 
fi 

Lancement du service à l'aide

launchctl load ~/Library/LaunchAgents/yourscript