LoginSignup
1
1

More than 5 years have passed since last update.

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

Posted at
  • 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ハンドラを作成
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1