Creating a Network

Hi all,

I have a script for creating an ad-hoc network which works (High Sierra), but I’d greatly prefer to have a simpler terminal command, as I know that in Linux, for instance, can be as easy as this:

iwconfig wlan0 mode Ad-hoc
iwconfig wlan0 essid MyWifi

osX does not have iwconfig, and I’m struggling to have an equivalent to that.
Any ideas on how to simplify the process?
Below my actual working code:


on CreateNetwork() —NEW-M needs to be enabled from Pref>Security and Privacy etc.

set CreateMenuName to "Create Network…"
set NetworkName to "MyAdHocNetwork"
writeReport for "| | | CREATING AD-HOC NETWORK..." given timeStamp:"nothing"

tell application "System Events"
	tell process "SystemUIServer"
		tell menu bar 1
			set menu_extras to value of attribute "AXDescription" of menu bar items
			repeat with the_menu from 1 to the count of menu_extras
				if item the_menu of menu_extras contains "Wi-Fi" then exit repeat
			end repeat
			tell menu bar item the_menu
				perform action "AXPress"
				delay 0.2
				perform action "AXPress" of menu item CreateMenuName of menu 1
			end tell
		end tell
		repeat until exists window 1
			delay 0.2
		end repeat
		tell window 1
			set value of text field 1 to NetworkName
			delay 0.2
			click pop up button 1
			delay 0.2
			click menu item 11 of menu 1 of pop up button 1
			delay 0.2
			click button 1
		end tell
	end tell
end tell

end CreateNetwork


I’m not sure of the equivalent shell command in macOS off the top of my head, but I wonder if this AppleScript might help instead:

use framework "Foundation"
use framework "CoreWLAN"

property this : a reference to current application
property CWWiFiClient : a reference to CWWiFiClient of this
property NSString : a reference to NSString of this

property NSUTF8StringEncoding : a reference to 4
property kCWIBSSModeSecurityWEP104 : a reference to 2

property channels : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 36, 40, 44, 48}

-- USER-DEFINED PROPERTIES
property SSID : "My Network Name"
property pw : "mypassword"
property channel : missing value -- set to a specific channel (optional)


on run settings -- {SSID:⟨string⟩, password:⟨string⟩, channel:⟨integer⟩}
		-- Determines whether the run handler was passed any parameters
		if class of settings = script or settings = {} then set ¬
				settings to {SSID:SSID, password:pw, channel:channel}
		
		-- Obtain settings for network or use defaults
		set [SSID, pw, channel] to [SSID, password, channel] of settings
		set ch to channel
		if ch is not in channels then set ch to some item of channels
		
		-- Create the ad-hoc network and establish a connection
		set defaultInterface to CWWiFiClient's sharedWiFiClient()'s interface()
		defaultInterface's startIBSSModeWithSSID:((NSString's ¬
				stringWithString:SSID)'s ¬
				dataUsingEncoding:NSUTF8StringEncoding) ¬
				security:kCWIBSSModeSecurityWEP104 channel:ch password:pw ¬
				|error|:(reference)
		set [success, E] to the result
		if E ≠ missing value then return E's localizedDescription() as text
		success
end run

AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.13