Macos Mac:设备通过蓝牙连接时启动命令

Macos Mac:设备通过蓝牙连接时启动命令,macos,bluetooth,macos-sierra,launchd,Macos,Bluetooth,Macos Sierra,Launchd,我想在特定的外部蓝牙设备连接到我的Mac电脑时启动shell命令 一个很好的方法(不安装第三方软件)是在~/Library/LaunchAgents中添加一个plist文件 在上,有一个当wifi连接到特定位置时启动事件的示例。通过查看特定文件完成此操作: <key>WatchPaths</key> <array> <string>/Library/Preferences/SystemConfiguration/com.apple.airpo

我想在特定的外部蓝牙设备连接到我的Mac电脑时启动shell命令

一个很好的方法(不安装第三方软件)是在~/Library/LaunchAgents中添加一个plist文件

在上,有一个当wifi连接到特定位置时启动事件的示例。通过查看特定文件完成此操作:

<key>WatchPaths</key>
<array>
   <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
</array>
监视路径
/库/Preferences/SystemConfiguration/com.apple.airport.Preferences.plist
你认为蓝牙事件也可以这样做吗


谢谢你的帮助

在~/Library/LaunchAgents中创建包含以下内容的文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

    <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>

</dict>
</plist>
使用启动服务

launchctl load ~/Library/LaunchAgents/yourscript

我找到了/Library/Preferences/com.apple.Bluetooth.plist文件。但是这个文件更改太多了,我不相信“测试”代码就足够了。即使未连接,system_profiler输出也将包含设备的名称。您需要解析输出或运行一些AppleScript。看见
launchctl load ~/Library/LaunchAgents/yourscript