Applescripted airport connection

Alec Jacobson

August 31, 2009

weblog/

My university uses a closed network connection available in all its buildings. To connect on my Mac I have to set up a a network each time. This only entails
  1. Clicking on the airport symbol
  2. Selecting Other...
  3. Typing in the network name
  4. Selecting LEAP
  5. Entering my username and password
The wireless on my older mac laptop is some what weak so repeating these few steps is tedious and worth script (only my username is ever remembered even after repeated connections). Here's a script to connect to a hard-coded network with a hard-coded username (the password of course is not hard-coded):
property passwd : ""
set network_name to "NYU-ROAM2"
set user_name to "abc123"
set results to do shell script "airport -s " & network_name
if results does not start with "No scan results for network" and results is not equal to "No networks found" then
	set passwd to text returned of (display dialog "Username:
" & user_name & "
Password:" default answer passwd default button 2 with icon 2 with title "Airport Roam-o-matic" with hidden answer)
        set results to do shell script "airport -A" & network_name & " --password=\"<" & user_name & "/" & passwd & ">\" 2>&1"
        if results starts with "Error" then
                display dialog "Could not connect to " & network_name & ".

Probably the password you entered did not match your username: " & user_name & "." buttons {"OK"} default button 1 with icon 0 with title "Airport Roam-o-matic"
        end if
else
        display dialog network_name & " network not found." buttons {"OK"} default button 1 with icon 0 with title "Airport Roam-o-matic"
end if
Note: My network lets me fake a LEAP username and password using this <username/password> WEP password. Note: For more general use just replace the first two lines with:
set network_name to text returned of (display dialog "Enter the network name:" default answer "" default button 2 with icon 2 with title "Airport Roam-o-matic")
set user_name to text returned of (display dialog "Enter your username:" default answer "" default button 2 with icon 2 with title "Airport Roam-o-matic")
Update: I added passwd as a property so that it is remembered. Also, I made the prompt contain the username and look a little prettier. Note: If you get an error like sh: airport: command not found, then you'll need to run this in Terminal.app:
sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport