Hello all. I need help with a script to create a network between my MBP (running latest Lion) and my iOS devices. I’ve searched everywhere and even here on the forums and can’t seem to find anything up to date enough to work properly.
Some help or a point in the right direction would be greatly appreciated. My intention is to learn about applescripting of course, but right now I want to get into Automator a bit and I can’t start my workflow without this script if I want to follow the tutorial. Thanks in advance.
Well, feel free to try mine. I’ll post it below for your information. (make sure to use the English names of the menu item and its entries. Don’t know them right know. UI properties in the script below)
It works nicely on Mac OS Snow Leopard using UI Scripting. Only some slight changes were necessary to update it from Leopard to Snow Leopard. I used some code that I found somewhere on the internet as well (don’t remember where).
If the airport can still be activated through a menu item, I am sure that it’s not that hard to update the old script to Lion.
Which script(s) did you try so far? Which error messages did you have and at which point did the script fail?
-- Creation date: Montag, 18. Januar 2010, 20:57:54
-- Created by: ljr (http://applescript.bratis-lover.net)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c- SETTINGS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
property myName : "Create ad-hoc network"
property myVersion : "0.3"
--c-- network properties (128-Bit WEP: 13 ASCII or 26 hex chars)
property NetworkName : "**MerkurOnWheels**"
--property NetworkPassword : ""
--c-- ui properties
property airportMenuItemName : " AirPort-Menüerweiterung" -- name of airport menu item in "menu bar items"
property airportNotActivated : "AirPort ist nicht aktiviert." -- Airport is not activated
property activateMenuName : "AirPort aktivieren" -- activate Airport
property CreateMenuName : "Netzwerk anlegen ." -- create network
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c- MAIN
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
set {tmp, NetworkPassword} to ¬
getLoginAndPasswordForKey("iPhone Ad-Hoc", "Applescript")
-- the password is retrieved from a keychain called "Applescript" under the name "iPhone Ad-Hoc"
-- (has to be created manually in advance)
-- the tmp variable is just a dummy since no login exists (only password)
try
tell application "System Events"
tell process "SystemUIServer"
tell menu bar 1
--c-- get index of airport menu item
set menuItems to description of menu bar items
set len to count menuItems
repeat with i from 1 to len
if item i of menuItems is airportMenuItemName then ¬
exit repeat
end repeat
--c-- check if airport is activated
set activateAirport to false
if value of menu bar item i is airportNotActivated then ¬
set activateAirport to true
--c-- click airport menu items (activate first if necessary)
tell menu bar item i
if activateAirport then
click
delay 0.2
click menu item activateMenuName of menu 1
end if
click
delay 0.2
click menu item CreateMenuName of menu 1
end tell
end tell
repeat until exists window 1
delay 0.2
end repeat
--c-- fill out create airport dialog
tell window 1
keystroke NetworkName
click checkbox 1
keystroke tab
keystroke tab
keystroke tab
keystroke NetworkPassword
keystroke tab
keystroke NetworkPassword
click pop up button 2
click menu item 2 of menu 1 of pop up button 2
delay 0.2
click button 1
end tell
end tell
end tell
on error eMsg number eNum
my errorHandler("An error occured during script execution!", eMsg, eNum)
end try
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c- FUNCTIONS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
on errorHandler(customMsg, eMsg, eNum)
tell me
activate
display dialog customMsg & "\r\rError: " & eNum default answer eMsg ¬
buttons {"Cancel"} default button 1 cancel button 1 with icon 0 ¬
with title myName giving up after 15
end tell
error number -128
end errorHandler
on getLoginAndPasswordForKey(keyName, keychainName)
-- http://applescript.bratis-lover.net/library/keychain/
local keyName, keychainName, thisKey, thisChain
local thisAccount, thisPassword
try
-- correct German name of login keychain
if keychainName starts with "Anmeldung." or ¬
keychainName is "Anmeldung" then
set keychainName to "login.keychain"
end if
-- add '.keychain' if missing
if keychainName does not end with ".keychain" then
set keychainName to keychainName & ".keychain"
end if
-- get account and password
tell application "Keychain Scripting"
set thisChain to keychain keychainName
set thisKey to first key of thisChain whose name is keyName
set thisAccount to thisKey's account
set thisPassword to thisKey's password
end tell
return {thisAccount, thisPassword}
on error eMsg number eNum
error "Can't getLoginAndPasswordForKey: " & eMsg number eNum
end try
end getLoginAndPasswordForKey
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- EOF
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Wow, thanks for the quick reply. I had posted in the Automator thread a few days back and haven’t gotten any reply.
I haven’t tried any other scripts or gotten any errors yet because all the scripts I’ve found were for older versions of the OS and while reading the comments I could tell right away that I wouldn’t be able to fix any of the problems that they were apparently causing for others.
And now that I look at the script you posted, I realize I don’t have any idea what to do with it, lol.
I assume I just open the editor and copy/paste the entire contents of the script in? What about the lighter grey text? are those comments?
Again, sorry for being a total noob. I’m trying to read the documentation on Scripting and Automation but haven’t gotten so far yet. Thanks again.
One word in advance: I am still on Snow Leopard, so I am not sure if all I am gonna say is still the same in Lion.
Let’s use a little easier version (though also less secure) of the script (see below). All lines starting with two dahes (–) are comments. Don’t mind the --c-- stuff. That’s just something I use…
Click the “Open this Scriplet in your Editor:” link above the script. It should open in AppleScript Editor.
Choose a name for your network and put it in this line (changing the Merku…): property NetworkName : “MerkurOnWheels”
Choose a password (26 chars) and put it in the line: property NetworkPassword : “8ee3147d025b40907514a754f9”
Find out what the menu item for Airport is called. You can use the following script. It will give you a list of the menu items, one of them will be the one for Airport. Then put it in this line: property airportMenuItemName : " AirPort-Menüerweiterung"
tell application "System Events"
tell process "SystemUIServer"
tell menu bar 1
set menuItems to description of menu bar items
end tell
end tell
end tell
Change the properties of the next three lines to the names of the menu items. Make sure to get everything exactly right (e.g. the three dots in “property CreateMenuName” are not three dots but a single character “.”).
Now, in the main part, the following things are happening (almost everything happens via UI scripting):
7a. --c-- get index of airport menu item
This part will get a list of all the menu items and return the index number (i) of the Airport menu item.
7b. --c-- check if airport is activated
This part will check whether or not Airport is activated.
7c. --c-- click airport menu items (activate first if necessary)
This part will activate airport if necessary and then click on the “Create Network .” menu item. This will open a little dialog for which we are waiting with the repeat loop.
7d. --c-- fill out create airport dialog
This was the part that needed to be updated from Leopard to SL.
“keystroke NetworkName” will type the network name you chose into the designated field.
“click checkbox 1” will check the password needed checkbox.
The three tabs that follow will move the cursor down into the password field.
“keystroke NetworkPassword” will type the password into the text field.
One tab and again typing the password.
The click pop menu 2 and the line that follows will change the security from 40-bit WEP to 128-bit WEP.
And finally “click button 1” will hit OK.
Give it a shot and let me know how it went
Don’t put all the code in at once if it doesn’t work. Copy line by line into Script Editor and see what it does. Be aware that you always have to end a tell block with end tell.
-- Creation date: Montag, 18. Januar 2010, 20:57:54
-- Created by: ljr (http://applescript.bratis-lover.net)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c- SETTINGS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
property myName : "Create ad-hoc network"
property myVersion : "0.3"
--c-- network properties (128-Bit WEP: 13 ASCII or 26 hex chars)
property NetworkName : "**MerkurOnWheels**"
property NetworkPassword : "8ee3147d025b40907514a754f9"
--c-- ui properties
property airportMenuItemName : " AirPort-Menüerweiterung" -- name of airport menu item in "menu bar items"
property airportNotActivated : "AirPort ist nicht aktiviert." -- Airport is not activated
property activateMenuName : "AirPort aktivieren" -- activate Airport
property CreateMenuName : "Netzwerk anlegen ." -- create network
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c- MAIN
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
try
tell application "System Events"
tell process "SystemUIServer"
tell menu bar 1
--c-- get index of airport menu item
set menuItems to description of menu bar items
set len to count menuItems
repeat with i from 1 to len
if item i of menuItems is airportMenuItemName then ¬
exit repeat
end repeat
--c-- check if airport is activated
set activateAirport to false
if value of menu bar item i is airportNotActivated then ¬
set activateAirport to true
--c-- click airport menu items (activate first if necessary)
tell menu bar item i
if activateAirport then
click
delay 0.2
click menu item activateMenuName of menu 1
end if
click
delay 0.2
click menu item CreateMenuName of menu 1
end tell
end tell
repeat until exists window 1
delay 0.2
end repeat
--c-- fill out create airport dialog
tell window 1
keystroke NetworkName
click checkbox 1
keystroke tab
keystroke tab
keystroke tab
keystroke NetworkPassword
keystroke tab
keystroke NetworkPassword
click pop up button 2
click menu item 2 of menu 1 of pop up button 2
delay 0.2
click button 1
end tell
end tell
end tell
on error eMsg number eNum
my errorHandler("An error occured during script execution!", eMsg, eNum)
end try
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c- FUNCTIONS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
on errorHandler(customMsg, eMsg, eNum)
tell me
activate
display dialog customMsg & "\r\rError: " & eNum default answer eMsg ¬
buttons {"Cancel"} default button 1 cancel button 1 with icon 0 ¬
with title myName giving up after 15
end tell
error number -128
end errorHandler
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- EOF
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
According to the comments on that site and the screenshots you posted, only one line needed to be changed in order to make it work on Lion (I marked the line).
The script basically does the same as the other ones posted before.
Let me know if it worked…
property CreateMenuName : "Create Network."
property NetworkName : "Net"
property NetworkPassword : "paswd"
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
-- ### THE FOLLOWING LINE WAS MODIFIED ### --
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.5
end repeat
tell window 1
click checkbox 1
click pop up button 2
click menu item 1 of menu 1 of pop up button 2
set value of text field 2 to NetworkPassword
set value of text field 3 to NetworkPassword
set value of text field 1 to NetworkName
click button 1
end tell
end tell
end tell
Fantastic, thanks again. I’ll give it a try when I get home tonight. You’ve helped immensely already, I really appreciate you taking the time. I’m gonna learn to script and hopefully I can contribute to the forum sometime. Take care and I’ll let you know how it goes.
But now I’ve got enough raw material to work it out myself eventually. I don’t want you to put any more time into this. I want to figure out how to make it create a network (possibly with WEP security) without me having to interact with it at all. Right now, it first asks for my master pass and then it opens the network window and asks me to confirm. Thanks again for all your help. Take care for now…
Hello again, just wanted to update you on my progress. I’ve been going through the tutorials here and have gotten far enough that I was able to puzzle together a few different scripts to achieve what I wanted. This script will also turn Wi-Fi on if it’s off and creates 40-Bit WEP security. Only problem is that I haven’t been able to figure out how to create 128-Bit WEP. Plus, I don’t know what the “BringupScript” command is. Let me know if you have any tips. Thanks in advance.
property NetworkName : "Your Network Name"
property NetworkPassword : "abcde"
property BringupScript : ""
tell application "System Events"
tell process "SystemUIServer"
tell menu bar 1
-- Find Airport Menu
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
-- Turn on airport and start create network
tell menu bar item the_menu
perform action "AXPress"
-- If Wi-Fi is off, turn it on
if title of menu item 2 of menu 1 is "Turn Wi-Fi On" then
perform action "AXPress" of menu item "Turn Wi-Fi On" of menu 1
perform action "AXPress"
end if
perform action "AXPress" of menu item "Create Network." of menu 1
end tell
end tell
-- Enter information into Create Network Dialog
tell window 1
click pop up button 2
click menu item 3 of menu 1 of pop up button 2
set value of text field 3 to NetworkPassword
set value of text field 2 to NetworkPassword
set value of text field 1 to NetworkName
click button "Create"
end tell
end tell
end tell
One more thing, now and then when I run this script, I’m asked for my administrative password to make changes. The script fails when this happens, but if I enter it and run it again, it’s no problem. I haven’t been able to figure out exactly why, but im sure it’s probably a common problem. If you know of any fix for that I’d appreciate it. Thank you.
good to hear that you are making progress with your script. Let’s see if I can help…
It should be easy to fix that (as long as 10.7 still offers that option). The solution to this problem should be found in this line:
click menu item 3 of menu 1 of pop up button 2
You have to figure out, which menu item (number) the 128-Bit WEP is in the pop up and then change the above line accordingly. Either by logic or by trial and error. Of course, you need to supply a proper password then.
I am not quite sure, what you mean by that. If you are looking for a quick way to execute your script, you have several options. The built-in options are:
Save your script as an App-Bundle (via the Save As…-dialog) and execute it by double clicking it.
Then there are various third party tools that can be used. I usually recommend FastScripts (http://www.red-sweater.com/fastscripts/) which does basically the same as the AppleScript menu. But it also let’s you assign keyboard shortcuts. There is a free version that let’s you create up to 10 shortcuts.
Other options would be e.g. Quicksilver, Butler,…
That is indeed quite strange. Honestly, I have never seen this dialog when running my script (as I said before, I am still on 10.6). I do not have any idea what it could be. Sorry
Ok! Fixed it. I, of course, did try to change that menu item, but I guess I never tried the number “four” lol, now it works.
The BringUpScript command was already a part of the scripts I used to make mine. So I didn’t put it there for any reason, I just don’t know what it does. I’ll ask somewhere on the forum though. This script is intended to be part of an Automator workflow so I won’t necessarily need any short commands for it.
Thank you again for all you help through this process. Take care.
property NetworkName : "Your Network Name"
property NetworkPassword : "thirteenchara"
property BringupScript : ""
tell application "System Events"
tell process "SystemUIServer"
tell menu bar 1
-- Find Airport Menu
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
-- Turn on airport and start create network
tell menu bar item the_menu
perform action "AXPress"
-- If Wi-Fi is off, turn it on
if title of menu item 2 of menu 1 is "Turn Wi-Fi On" then
perform action "AXPress" of menu item "Turn Wi-Fi On" of menu 1
perform action "AXPress"
end if
perform action "AXPress" of menu item "Create Network." of menu 1
end tell
end tell
-- Enter information into Create Network Dialog
tell window 1
click pop up button 2
click menu item 4 of menu 1 of pop up button 2
set value of text field 3 to NetworkPassword
set value of text field 2 to NetworkPassword
set value of text field 1 to NetworkName
click button "Create"
end tell
end tell
end tell