4/08/2007

Acting on Current Airport Network


I was roaming around the web this morning, and saw an application to do what I've been doing using an AppleScript that I patched together from someone else's shell script example. If you paste the script below into Script Editor, you can change the relevant portions (volume path, username, SSID of your network) to get a script that will launch your home file server if you’re connected to your home work. To do anything else, you only really need the AirportName variable that is produced by the first try block. You might for example run this script periodically using Lingon or Cronnix to set a directories name from “Foo” to “.Foo” to hide it from prying eyes. Or you can use it to set whether or not your password-requiring screen saver activates or not. Anyway, if you have some AppleScript at all, you should be able to make something out of this after a little Googling for any other parts you need.

try
        set AirportName to do shell script ¬
                "system_profiler SPAirPortDataType|awk -F\": \" '/Current Wireless Network/{print $2}'"
on error
        display dialog "I could not determine current wireless network." buttons {"Okay"} default button 1
        
if button returned of result is "Okay" then
                quit me
        end if
end try

if AirportName is "YourSSID" then
        tell application "Finder"
                
tell application "Finder"
                        
activate
                        try
                                mount volume "afp://your.server.address/yourUserName" as user name "yourUserName"
                        
on error
                                display dialog "There was an error mounting the Volume." buttons {"Okay"} default button 1
                        
end try
                end tell
        end tell
else
        display dialog "You are connected to " & AirportName & ", not YourSSID. Do you wish to continue?" buttons {"Yes", "No"} default button "No"
        
if button returned of result is "Yes" then
                tell application "Finder"
                        
activate
                        try
                                mount volume "afp://your.server.address/yourUserName" as user name "yourUserName"
                        
on error
                                display dialog "There was an error mounting the Volume." buttons {"Okay"} default button 1
                        
end try
                end tell
        else if button returned of result is "No" then
                quit me
        end if
end if

1 comments:

jpc said...

If you have NO AppleScript, you still might get some mileage out of this by plugging it into Automator, which is in your Applications folder and allows you to make little programs by dragging actions from a list. Its help files will give you a good start. Don't be afraid to try this out, as it's pretty obvious what actions might cause you to do some Bad Thing You Don't Understand.