Posted at

Cocoaの機能を使ってWi-Fiに接続するAppleScript

More than 3 years have passed since last update.


  • CoreWLANフレームワークを使ってWi-Fiに接続するサンプル


  • listWiFi: Wi-FiネットワークをスキャンしてSSIDのリストを返す


  • connectWiFi: SSIDとパスワードを使ってWi-Fiに接続


  • disconnectWiFi: 接続中のWi-Fiを切断


connectToWifi.scpt

use scripting additions

use framework "CoreWLAN"
use framework "Foundation"

--SSID選択
set chooseResult to choose from list my listWiFi()
if chooseResult = false then
error number -128
end if
set networkSSID to item 1 of chooseResult

--パスワード入力
set {text returned:pass} to display dialog "Password of network " & quoted form of networkSSID & ":" default answer "" buttons {"Cancel", "Connect"} cancel button 1 default button 2 with hidden answer

--Wi-Fiに接続
my connectWifi(networkSSID, pass)

--Wi-Fiの接続を切る
my disconnectWiFi()

on listWiFi()
--require framework: CoreWLAN, Foundation
set currentInterface to current application's CWInterface's interface()
currentInterface's setPower:true |error|:(missing value)
set networkSet to currentInterface's scanForNetworksWithName:(missing value) |error|:(missing value)
return ((networkSet's allObjects()'s valueForKeyPath:"ssid.@distinctUnionOfObjects.self")'s sortedArrayUsingSelector:"localizedCaseInsensitiveCompare:") as list
end listWiFi

on connectWifi(networkSSID as text, pass as text)
--require framework: CoreWLAN
set currentInterface to current application's CWInterface's interface()
currentInterface's setPower:true |error|:(missing value)
set network to (currentInterface's scanForNetworksWithName:networkSSID |error|:(missing value))'s anyObject()
currentInterface's associateToNetwork:network |password|:pass |error|:(missing value)
end connectWifi

on disconnectWiFi()
--require framework: CoreWLAN
current application's CWInterface's interface()'s disassociate()
end disconnectWiFi



更新履歴


  • 2016-02-13: CoreWLANフレームワークを使ってWi-Fiに接続するconnectWiFiハンドラを作成

  • 2016-02-15: listWiFiハンドラとdisconnectWiFiハンドラを作成