Archive for the 'NSPropertyListFormat' Category

2017/08/06 Bluetoothに接続中のデバイスをメーカー名とマイナー種別で抽出

Bluetoothに接続中のデバイスをメーカー名とマイナー種別で抽出するAppleScriptです。

IOBluetooth経由ではなく、system_profiler経由でアクセスしてみました。メーカー名とデバイス種別で抽出できるので、自分がやりたい処理はできるようになったのですが、できればできたで衝撃の事実が発覚!

Apple製の周辺機器として流通しているものの中にも、メーカー名に”Apple”を返さないものがありました。事実、Magic Keyboard 2やMagic Mouse 2は製造元として「Broadcom」を返してきました。

仕様上、これでいいのか疑問が残る仕上がりです。ハードウェアについては十二分に満足していますが、こんなところでミソがつくとは、、、、

BroadcomはBluetoothの通信チップのメーカーであり、周辺機器そのものを製造しているわけではない、と認識していたのですが、気のせいでしょうか?(たぶん、ただしい)

参考までに、身の回りのBluetoothデバイスの種類のメジャー(Major)とマイナー(Minor)の名称をリストアップしておきます。詳細はユーティリティーフォルダ内の「システム情報」アプリケーションでご確認ください。

Magic Keyboard
Major: Peripheral, Minor:Keyboard

Magic Mouse 2
Major: Peripheral, Minor:Mouse

Mac mini
Major: Computer, Minor:Desktop

Bluetooth Speaker
Major: Audio, Minor:Loudspeaker

AirPods
Major: Audio, Minor:Headphones

iPad mini
Major: Computer, Minor:Handheld

iPhone
Major: Phone, Minor:Smartphone

本ルーチンについては、「意味なし予約語」を用いて英単語でそれっぽく表記できるようにしてみましたが、一般的なサブルーチンのハンドラの表記形式を用いても問題はありません。たまたまです。

AppleScript名:Bluetoothに接続中のデバイスをメーカー名とマイナー種別で抽出
– Created 2017-08-06 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/4764

property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSMutableArray : a reference to current application’s NSMutableArray
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
property NSPropertyListSerialization : a reference to current application’s NSPropertyListSerialization
property NSPropertyListImmutable : a reference to current application’s NSPropertyListImmutable
property NSPropertyListFormat : a reference to current application’s NSPropertyListFormat
property NSPredicate : a reference to current application’s NSPredicate

–製造者が”Apple”のBluetoothデバイスをリストアップ
set qRes to returnBTPeripheral from “Apple”
–>  {{device_supportsESCO:”attrib_Yes”, device_role:”attrib_master”, device_manufacturer:”Apple (0×6, 0×03)”, 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:”0×2002″, device_supportsSSP:”attrib_Yes”, device_classOfDevice:”0×04 0×06 0×240418″, device_vendorID:”0×004C”, device_fw_version:”0×0372″, device_ispaired:”attrib_Yes”, device_supportsEDR:”attrib_Yes”}, {device_supportsESCO:”attrib_No”, device_manufacturer:”Apple (0×3, 0×31C)”, 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:”0×030D”, device_supportsSSP:”attrib_No”, device_vendorID:”0×05AC”, device_classOfDevice:”0×05 0×20 0×2580″, device_minorClassOfDevice_string:”Mouse”, device_fw_version:”0×0084″, 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 (0×6, 0×03)”, 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:”0×2002″, device_supportsSSP:”attrib_Yes”, device_classOfDevice:”0×04 0×06 0×240418″, device_vendorID:”0×004C”, device_fw_version:”0×0372″, 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 (0×6, 0×03)”, 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:”0×2002″, device_supportsSSP:”attrib_Yes”, device_classOfDevice:”0×04 0×06 0×240418″, device_vendorID:”0×004C”, device_fw_version:”0×0372″, 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

★Click Here to Open This Script