— Created 2017-08-06 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation"
property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property NSPredicate : a reference to current application’s NSPredicate property NSMutableArray : a reference to current application’s NSMutableArray property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSPropertyListFormat : a reference to current application’s NSPropertyListFormat property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property NSPropertyListImmutable : a reference to current application’s NSPropertyListImmutable property NSPropertyListSerialization : a reference to current application’s NSPropertyListSerialization
–製造者が"Apple"のBluetoothデバイスをリストアップ set qRes to returnBTPeripheral from "Apple" –> {{device_supportsESCO:"attrib_Yes", device_role:"attrib_master", device_manufacturer:"Apple (0x6, 0x03)", device_services:"Handsfree, Wireless iAP, AVRCP Controller, Audio Sink, AVRCP Target, AAP Server", device_isconnected:"attrib_Yes", device_RSSI:-51, device_majorClassOfDevice_string:"Audio", device_isconfigured:"attrib_Yes", device_minorClassOfDevice_string:"Headphones", device_interval:"441.25 ms", device_addr:"XX-XX-XX-XX-XX-XX", device_ConnectionMode:"attrib_sniff_mode", device_productID:"0x2002", device_supportsSSP:"attrib_Yes", device_classOfDevice:"0x04 0x06 0x240418", device_vendorID:"0x004C", device_fw_version:"0x0372", device_ispaired:"attrib_Yes", device_supportsEDR:"attrib_Yes"}, {device_supportsESCO:"attrib_No", device_manufacturer:"Apple (0x3, 0x31C)", device_ispaired:"attrib_Yes", device_services:"Apple Wireless Mouse", device_isconnected:"attrib_No", device_majorClassOfDevice_string:"Peripheral", device_isNormallyConnectable:"attrib_Yes", device_isconfigured:"attrib_Yes", device_addr:"XX-XX-XX-XX-XX-XX", device_productID:"0x030D", device_supportsSSP:"attrib_No", device_vendorID:"0x05AC", device_classOfDevice:"0x05 0x20 0x2580", device_minorClassOfDevice_string:"Mouse", device_fw_version:"0x0084", device_supportsEDR:"attrib_No"}} –Appleのデバイスでも製造者がAppleになっていないものもある。Magic Keyboard 2とか
–種類(マイナー)が "Headphones"のBluetoothデバイスをリストアップ set qRes to returnBTPeripheral about "Headphones" –> {{device_supportsESCO:"attrib_Yes", device_role:"attrib_master", device_manufacturer:"Apple (0x6, 0x03)", device_services:"Handsfree, Wireless iAP, AVRCP Controller, Audio Sink, AVRCP Target, AAP Server", device_isconnected:"attrib_Yes", device_RSSI:-51, device_majorClassOfDevice_string:"Audio", device_isconfigured:"attrib_Yes", device_minorClassOfDevice_string:"Headphones", device_interval:"441.25 ms", device_addr:"XX-XX-XX-XX-XX-XX", device_ConnectionMode:"attrib_sniff_mode", device_productID:"0x2002", device_supportsSSP:"attrib_Yes", device_classOfDevice:"0x04 0x06 0x240418", device_vendorID:"0x004C", device_fw_version:"0x0372", device_ispaired:"attrib_Yes", device_supportsEDR:"attrib_Yes"}}
–製造者が"Apple"で、種類(マイナー)が "Headphones"のBluetoothデバイスをリストアップ set qRes to returnBTPeripheral from "Apple" about "Headphones" –> {{device_supportsESCO:"attrib_Yes", device_role:"attrib_master", device_manufacturer:"Apple (0x6, 0x03)", device_services:"Handsfree, Wireless iAP, AVRCP Controller, Audio Sink, AVRCP Target, AAP Server", device_isconnected:"attrib_Yes", device_RSSI:-52, device_majorClassOfDevice_string:"Audio", device_isconfigured:"attrib_Yes", device_minorClassOfDevice_string:"Headphones", device_interval:"441.25 ms", device_addr:"XX-XX-XX-XX-XX-XX", device_ConnectionMode:"attrib_sniff_mode", device_productID:"0x2002", device_supportsSSP:"attrib_Yes", device_classOfDevice:"0x04 0x06 0x240418", device_vendorID:"0x004C", device_fw_version:"0x0372", device_ispaired:"attrib_Yes", device_supportsEDR:"attrib_Yes"}}
on returnBTPeripheral from devMaker as string : "" about kindName as string : "" set sRes to do shell script "/usr/sbin/system_profiler SPBluetoothDataType -detailLevel full -xml" set aSource to (readPlistFromStr(sRes) of me) as list set aaList to contents of first item of aSource set resArray to NSMutableArray’s new() set aList to _items of aaList repeat with i in aList set aDict to (NSMutableDictionary’s dictionaryWithDictionary:(contents of i)) set aKeyList to (aDict’s allKeys()) as list set dResList to (aDict’s valueForKeyPath:"device_title") repeat with ii in dResList set dKeyList to ii’s allKeys() set dKey to first item of dKeyList set dDic to (ii’s valueForKeyPath:dKey) if devMaker is not equal to "" and kindName is not equal to "" then set qText to "device_manufacturer contains ’" & devMaker & "’ && device_minorClassOfDevice_string ==’" & kindName & "’" else if devMaker is not equal to "" then set qText to "device_manufacturer contains ’" & devMaker & "’" else if kindName is not equal to "" then set qText to "device_minorClassOfDevice_string ==’" & kindName & "’" end if set dRes to filterRecListByLabel(dDic, qText) of me if (dRes as list) is not equal to {} then (resArray’s addObject:(first item of dRes)) end if end repeat end repeat return resArray as list end returnBTPeripheral
–stringのplistを読み込んでRecordに on readPlistFromStr(theString) set aSource to NSString’s stringWithString:theString set pListData to aSource’s dataUsingEncoding:(NSUTF8StringEncoding) set aPlist to NSPropertyListSerialization’s propertyListFromData:pListData mutabilityOption:(NSPropertyListImmutable) |format|:(NSPropertyListFormat) errorDescription:(missing value) return aPlist end readPlistFromStr
–リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel(aRecList as list, aPredicate as string) set aArray to NSArray’s arrayWithArray:aRecList set aPredicate to NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate set bList to filteredArray as list return bList end filterRecListByLabel
|