/
wifi.rb
31 lines (25 loc) · 975 Bytes
/
wifi.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require 'mac-network'
OSX.require_framework('CoreWLAN')
class Mac::Network::WiFi
def self.preferred_network=(network_name)
raise "Sorry #{network_name} is not in the network list" unless self.network_names.include? network_name
config_copy = self.configuration
config_copy.networkProfiles = OSX::NSOrderedSet.orderedSetWithArray(config_copy.networkProfiles.allObjects.sort_by {|item| item.ssid != network_name })
self.interface.commitConfiguration_authorization_error(config_copy,nil,nil)
end
def self.preferred_network
self.networks.allObjects.first.ssid
end
def self.interface
OSX::CWInterface.interfaceWithName(OSX::CWInterface.supportedInterfaces.first)
end
def self.configuration
OSX::CWMutableConfiguration.configurationWithConfiguration(self.interface.configuration)
end
def self.networks
self.configuration.networkProfiles
end
def self.network_names
self.networks.allObjects.map {|n| n.ssid }
end
end