AppleScript名:Bluetoothに接続中のデバイス名を取得、AirPodsを切断する |
— Created 2017-08-05 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" –macOS 10.11 or later use scripting additions use framework "Foundation" use framework "IOBluetooth" –参照 https://github.com/lapfelix/BluetoothConnector property IOBluetoothHostController : a reference to current application’s IOBluetoothHostController property IOBluetoothDevice : a reference to current application’s IOBluetoothDevice set dList to getActiveBluetoothDevices() of me –> {{deviceName:"Piyomaru AirPods", deviceAddress:"Xx-XX-xX-Xx-xx-xx"}, {deviceName:"Takaaki Naganoya のマウス", deviceAddress:"xx-xx-XX-xx-XX-Xx"}, {deviceName:"Takaaki Naganoya のキーボード #1", deviceAddress:"XX-XX-xX-xx-Xx-xX"}} set dRes to filterRecListByLabel(dList, "deviceName contains ’AirPods’") of me if dRes = {} then return false –Case: No match set dAddr to dRes’s first item’s deviceAddress set cnRes1 to disconnectBluetoothDeviceByAddress(dAddr) of me –> true (Successfully Disconnected) delay 10 set cnRes2 to connectBluetoothDeviceByAddress(dAddr) of me –> true (Successfully Connected) –指定アドレスのBluetooth Deviceを接続する on connectBluetoothDeviceByAddress(addressStr as string) if getBluetoothPowerState() = false then error "Bluetooth Power is not active with your Mac" set aBTList to IOBluetoothDevice’s pairedDevices() as list repeat with i in aBTList set aClass to (current application’s NSStringFromClass(i’s |class|())) as string if aClass is equal to "IOBluetoothDevice" then set anAddress to i’s addressString() as string if anAddress = addressStr then i’s openConnection() return true end if end if end repeat return false end connectBluetoothDeviceByAddress –指定アドレスのBluetooth Deviceの接続を切る on disconnectBluetoothDeviceByAddress(addressStr as string) if getBluetoothPowerState() = false then error "Bluetooth Power is not active with your Mac" set aBTList to IOBluetoothDevice’s pairedDevices() as list repeat with i in aBTList set aClass to (current application’s NSStringFromClass(i’s |class|())) as string if aClass is equal to "IOBluetoothDevice" then set anAddress to i’s addressString() as string if anAddress = addressStr then i’s closeConnection() return true end if end if end repeat return false end disconnectBluetoothDeviceByAddress –ペアリング済みのBluetooth Deviceの情報を取得 on getActiveBluetoothDevices() if getBluetoothPowerState() = false then error "Bluetooth Power is not active with your Mac" set aBTList to IOBluetoothDevice’s pairedDevices() as list set devList to {} repeat with i in aBTList set aClass to (current application’s NSStringFromClass(i’s |class|())) as string if aClass = "IOBluetoothDevice" then set aName to i’s |name|() as string set anAddress to i’s addressString() as string set aPaired to i’s isPaired() as boolean –set aConnect to i’s isConnected() as boolean if aPaired = true then set the end of devList to {deviceName:aName, deviceAddress:anAddress} end if end if end repeat return devList end getActiveBluetoothDevices –Mac本体のBluetoothのパワー状態を取得 on getBluetoothPowerState() set aCon to IOBluetoothHostController’s alloc()’s init() set pRes to (aCon’s powerState()) as boolean end getBluetoothPowerState –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel(aRecList as list, aPredicate as string) set aArray to current application’s NSArray’s arrayWithArray:aRecList set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate set bList to filteredArray as list return bList end filterRecListByLabel |
More from my site
(Visited 105 times, 1 visits today)