Monday, July 26, 2021

#1 2008-05-15 06:23:14 pm

richcreations
Member
Registered: 2008-05-16
Posts: 2

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


Filed under: System

Offline

 

#2 2008-05-16 02:14:02 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11749
Website

Re: creating ad-hoc airport network from script

Hi,

try this

Applescript:


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


regards

Stefan


Filed under: System

Offline

 

#3 2008-05-16 02:38:09 am

richcreations
Member
Registered: 2008-05-16
Posts: 2

Re: creating ad-hoc airport network from script

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

Offline

 

#4 2009-06-04 04:22:25 am

wisemonkey
Member
Registered: 2009-06-04
Posts: 1

Re: creating ad-hoc airport network from script

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!

Offline

 

#5 2009-06-04 07:51:39 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: creating ad-hoc airport network from script

wisemonkey wrote:

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?


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.

Applescript:

-- 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


Filed under: System, Keychain

Offline

 

#6 2011-09-21 02:14:52 pm

Derick
Member
Registered: 2007-05-26
Posts: 18

Re: creating ad-hoc airport network from script

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


Filed under: lion wifi

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)