Archive for the 'CWInterface' Category

2015/08/18 ASOCで無線LANの各種情報を取得する v2

Cocoaの機能を用いて無線LANの各種情報を取得するAppleScriptです。Shane Stanleyから「こんなやり方もあるよ」と教えてもらったものです。

CoreWLANまわりをいろいろ探ってみたものの、どーもCFSTR(ASOCで扱えない)を作成できないと呼べない機能などに阻まれ、あんまり凝ったことはできなさそうです。

[Update] WiFiがオフになっている場合への対処、およびv1のテストコードを統合しました。基本的にv1を短く書いたもので、機能は同じです。

AppleScript名:ASOCで無線LANの各種情報を取得する v2.1
– Created 2015-08-18 by Shane Stanley
– Modified 2015-08-18 by Takaaki Naganoya
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “CoreWLAN”

set allNames to current application’s CWInterface’s interfaceNames()’s allObjects()
–>  (NSArray) {”en0″} –@MacBook Pro Retina 2012

set hitF to false
repeat with i from 1 to allNames’s |count|()
  set aInterface to (current application’s CWInterface’s interfaceWithName:(allNames’s objectAtIndex:(i - 1)))
  
if aInterface’s serviceActive() as boolean then
    set hitF to true
    
exit repeat
  end if
end repeat

if hitF = false then return false –ActiveなWLANがない場合(WiFiをオフにしている)にはfalseをリターン

set aChan to aInterface’s wlanChannel()
–> (CWChannel) [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS]

set chanNumber to aChan’s channelNumber()
–> 52
set chanBand to aChan’s channelBand() – 0 = unknown, 1 = 2GHz, 2 = 5GHz
–> 2
set chanWidth to aChan’s channelWidth() – 0 = unknown, 1 = 20MHz, 2 = 40MHz
–> 2
set aTransRate to aInterface’s transmitRate()
–>  162.0
set aPower to aInterface’s transmitPower()
–>  0

set aChannnels to aInterface’s supportedWLANChannels()
–>  (NSSet) {(CWChannel) [channelNumber=1(2GHz), channelWidth={20MHz}, active], (CWChannel) [channelNumber=2(2GHz), channelWidth={20MHz}, active], ……………(CWChannel) [channelNumber=64(5GHz), channelWidth={20MHz}, DFS], (CWChannel) [channelNumber=136(5GHz), channelWidth={20MHz}, DFS]}

set aSSIDdata to aInterface’s ssidData()
–>  (NSData) <45787472 xxxxxxxx xxxxxxxx xxxx>

set aSSID to aInterface’s ssid() as text
–>  ”Extreme net_5G”

set aActive to aInterface’s serviceActive()
–>  true

set aSecurity to aInterface’s security()
–>  4

set aRSSIval to aInterface’s rssiValue()
–>  -72

set aPower to aInterface’s powerOn()
–>  true

set aNoise to aInterface’s noiseMeasurement()
–>  -91

set aIFname to aInterface’s interfaceName() as text
–>  ”en0″

set aMode to aInterface’s interfaceMode()
–>  1

set aHWAddress to aInterface’s hardwareAddress() as text
–>  ”b8:xx:xx:xx:xx:xx”–MAC Address

set aCountry to aInterface’s countryCode() as text
–>  ”JP”

set aConfig to aInterface’s configuration()
–>  (CWConfiguration)

set aCacheRes to aInterface’s cachedScanResults()
–>  (NSSet) {(CWNetwork) ……………… [channelNumber=11(2GHz), channelWidth={20MHz}], ibss=0]}

set aBSSID to aInterface’s bssid() as text
–>  ”10:XX:XX:XX:XX:XX”

set aPhyMode to aInterface’s activePHYMode()
–>  4

★Click Here to Open This Script 

2015/08/18 ASOCで無線LANの各種情報を取得する

Cocoaの機能を用いて無線LANの各種情報を取得するAppleScriptです。CoreWLAN.frameworkの機能(CWInterface)を使っています。

実行結果をみると、 transmitPower() だけ不思議な(アクティブになっているのに0が返ってくる)感じです。

AppleScript名:ASOCで無線LANの各種情報を取得する
– Created 2015-08-18 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “CoreWLAN”

set dName to getWiFiDeviceName()
–> “en0″ –(example on MacBook Pro Retina 2012)

set aInterface to current application’s CWInterface’s alloc()’s initWithInterfaceName:dName
–>(CWInterface) [interfaceName=en0]

set aChan to aInterface’s wlanChannel()
–>  (CWChannel) [channelNumber=52(5GHz), channelWidth={40MHz(+1)}, DFS]

set aTransRate to aInterface’s transmitRate()
–>  162.0

set aPower to aInterface’s transmitPower()
–>  0

set aChannnels to aInterface’s supportedWLANChannels()
–>  (NSSet) {(CWChannel) [channelNumber=1(2GHz), channelWidth={20MHz}, active], (CWChannel) [channelNumber=2(2GHz), channelWidth={20MHz}, active], ……………(CWChannel) [channelNumber=64(5GHz), channelWidth={20MHz}, DFS], (CWChannel) [channelNumber=136(5GHz), channelWidth={20MHz}, DFS]}

set aSSIDdata to aInterface’s ssidData()
–>  (NSData) <45787472 xxxxxxxx xxxxxxxx xxxx>

set aSSID to aInterface’s ssid() as text
–>  ”Extreme net_5G”

set aActive to aInterface’s serviceActive()
–>  true

set aSecurity to aInterface’s security()
–>  4

set aRSSIval to aInterface’s rssiValue()
–>  -72

set aPower to aInterface’s powerOn()
–>  true

set aNoise to aInterface’s noiseMeasurement()
–>  -91

set aIFname to aInterface’s interfaceName() as text
–>  ”en0″

set aMode to aInterface’s interfaceMode()
–>  1

set aHWAddress to aInterface’s hardwareAddress() as text
–>  ”b8:xx:xx:xx:xx:xx”–MAC Address

set aCountry to aInterface’s countryCode() as text
–>  ”JP”

set aConfig to aInterface’s configuration()
–>  (CWConfiguration)

set aCacheRes to aInterface’s cachedScanResults()
–>  (NSSet) {(CWNetwork) ……………… [channelNumber=11(2GHz), channelWidth={20MHz}], ibss=0]}

set aBSSID to aInterface’s bssid() as text
–>  ”10:XX:XX:XX:XX:XX”

set aPhyMode to aInterface’s activePHYMode()
–>  4

–指定ハードウェアポートのデバイス名を取得する
on getWiFiDeviceName()
  set v2 to system attribute “sys2″ –> 4
  
if v2 6 then
    set hardWareName to “AirPort” –Under Mac OS X 10.6.8
    
set aMesStr to “Current AirPort Network: “
  else if v2 7 then
    set hardWareName to “Wi-Fi” –Mac OS X 10.7 or later
    
set aMesStr to “Current Wi-Fi Network: “
  else
    display dialog “error”
  end if
  
  
set dName to getHardwareDeviceName(hardWareName) of me
  
return dName
end getWiFiDeviceName

–指定ハードウェアポートのデバイス名を取得する
on getHardwareDeviceName(targName)
  set sRes to do shell script “/usr/sbin/networksetup -listallhardwareports”
  
log sRes
  
set sList to paragraphs of sRes
  
set s1List to items 2 thru -1 of sList
  
  
set s2List to {}
  
repeat with i in s1List
    set j to contents of i
    
if j is equal to “VLAN Configurations” then
      exit repeat
    end if
    
set the end of s2List to j
  end repeat
  
  
–ネットワークポート関連のレコードを作成
  
set s3List to {}
  
set aLen to length of s2List
  
repeat with i from 1 to aLen by 4
    set a1Item to contents of item i of s2List
    
set a1Item to repChar(a1Item, “Hardware Port: “, “”) of me
    
    
set a2Item to contents of item (i + 1) of s2List
    
set a2Item to repChar(a2Item, “Device: “, “”) of me
    
    
set a3Item to contents of item (i + 2) of s2List
    
set a3Item to repChar(a3Item, “Ethernet Address: “, “”) of me
    
    
set the end of s3List to {hardwarePort:a1Item, device:a2Item, ethernetAddress:a3Item}
  end repeat
  
  
repeat with i in s3List
    set j1 to hardwarePort of i
    
set j2 to device of i
    
if j1 is equal to targName then
      return j2
    end if
  end repeat
  
  
return “”
  
end getHardwareDeviceName

–文字置換ルーチン
on repChar(origText, targStr, repStr)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChar

★Click Here to Open This Script