無線LANネットワーク名称を取得する v1.1

現在接続中の無線LANネットワークの名称を取得するAppleScriptです。

無線LAN接続中の場合には名称文字列を、未接続の場合にはヌルを返します。

air_port_names.png

▲実行時には「Extreme net」が取得される

スクリプト名:無線LANネットワーク名称を取得する v1.1
set aRes to getAirportNetworkName() of me

–無線LANで接続中のネットワーク名称を取得
on getAirportNetworkName()
  
  
set v2 to system attribute “sys2″ –> 4
  
if v2 6 then
    set hardWareName to “AirPort” –Mac OS X 10.6
    
set aMesStr to “Current AirPort Network: “
  else if v2 7 then
    set hardWareName to “Wi-Fi” –Mac OS X 10.7
    
set aMesStr to “Current Wi-Fi Network: “
  end if
  
  
set dName to getHardwareDeviceName(hardWareName) of me
  
set sText to “/usr/sbin/networksetup -getairportnetwork “ & dName
  
  
set sRes to do shell script sText
  
  
if sRes does not start with “You are not associated with “ then
    set sRes2 to repChar(sRes, aMesStr, “”) of me
  else
    set sRes2 to “”
  end if
  
  
return sRes2
  
end getAirportNetworkName

–指定ハードウェアポートのデバイス名を取得する
on getHardwareDeviceName(targName)
  set sRes to do shell script “/usr/sbin/networksetup -listallhardwareports”
  
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

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

One Response to “無線LANネットワーク名称を取得する v1.1”

  1. Mac起動時に環境によって違うNASを自動マウントGolbitz | Golbitz Says:

    […] 無線LANネットワーク名称を取得する v1.1 […]

Leave a Reply