creating ad-hoc airport network from script

Hi, I have been trying to create an applescript to automatically create a computer to computer (ad-hoc) network, I have it mostly working in leopard (except I can’t get it to set the name or click the ok button), but it fails in tiger… can anyone help me with this?

here is the script I have, perhaps there is a better way? I would like it to work in both 10.4 and 10.5… It would also be nice if the user did not have to click the ok button, and even nicer if the script could set the name of the network…

tell application “System Events” to 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 is “Airport Menu Extra” then exit repeat
end repeat
tell menu bar item the_menu
click
delay 1
click menu item “Create Network.” of front menu
delay 1
end tell
end tell
end tell

Thanks,
Rich Simpson

Hi,

try this


property CreateMenuName : "Create Network."
property NetworkName : "testNet"

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 is "Airport Menu Extra" 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.5
		end repeat
		tell window 1
			keystroke NetworkName
			click button 1
		end tell
	end tell
end tell

wow, thanks…
Just what I needed, works great.

Hi,

Just discovered this post. I have zero experience with applescript but would like to use your solution here. How would I add password protection (40-bit WEP) to this script?

Thanks!

Very easy… just use the mac’s built-in Keychain Access program to hold the password. Then in your script you ask the user to enter a password and then match that password against the one stored in Keychain Access. I put the directions in the script. Hope this helps.

-- If you need to use a password in a script you can use the keychain to store the password and have the script retrieve it. This way your password is protected because you don't need to store passwords in clear text in a script.
-- Create the password item - Open Keychain Access application and select the keychain in the left column. Then click File>New Password Item..., give it a name, put your account shortname in account, and enter the password. Highlight it in the password list and get information on it. Under the Attributes button enter its kind as generic key. This is chosen because there aren't many of them and the search is much faster.
-- Make sure to enter your keychainFileName and passwordItemName in this script

set keychainFileName to "myKeychain.keychain" -- must be the name of the keychain file in the Finder, not the name of the keychain
set passwordItemName to "My Script Password"
set createMenuName to "Create Network."
set networkName to "testNet"

set passCheck to text returned of (display dialog "Enter your password..." default answer "" with icon 2 with hidden answer)
if passCheck is equal to getPW(keychainFileName, passwordItemName) then
	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 is "Airport Menu Extra" 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.5
			end repeat
			tell window 1
				keystroke networkName
				click button 1
			end tell
		end tell
	end tell
end if

on getPW(keychainFileName, passwordItemName)
	set passwd to missing value
	try
		tell application "Keychain Scripting"
			tell keychain keychainFileName
				tell (some generic key whose name is passwordItemName) to set passwd to password
			end tell
		end tell
	on error theError number errorNumber
		tell me
			activate
			display dialog "Error retrieving the password:" & return & return & theError & return & "Error Number: " & (errorNumber as text) buttons {"OK"} default button 1 with icon stop
		end tell
	end try
	return passwd
end getPW

Has anyone got this working in Lion? When I try it, it just opens the rightmost item (to the L of Spotlight) on the menu bar. Presumably identifiers changed with the shift to “Wi-Fi” from “Airport” in Lion but I don’t know how to locate the new ones.
thanks
Derick