Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

Turn AirPort on or off with AppleScript Network
If your laptop is running unplugged and you need to conserve battery life, one way of saving power is to turn off AirPort. (You may also run into situations where you'd want to turn AirPort on or off as part of a longer script, such as to keep it from dying completely when putting a MacBook to sleep while Internet Sharing is enabled.) Although there seems to be no reliable way of turning AirPort on or off directly via the shell, it is possible to get around that by manipulating OS X's Locations with the scselect command.

First, create a new location called AirPort-Off in the Network preferences pane. With this new location selected, select Network Port Configurations under the Show pop-up menu, and deselect AirPort. Then select your previous location, which for most people would probably be Automatic.

Now, you can turn AirPort off by running scselect AirPort-Off and back on by running the last command, replacing AirPort-Off with the name of your normal location name. To assign a hot key to the command using a utility like Butler, create a simple AppleScript containing this one line:
do shell script "/usr/sbin/scselect AirPort-Off"
Then have Butler (or your app of choice) run that APpleScript. (With Butler, running a shell script containing only the bare command doesn't seem to work.)
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[19,395 views]  

Turn AirPort on or off with AppleScript | 17 comments | Create New Account
Click here to return to the 'Turn AirPort on or off with AppleScript' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Turn AirPort on or off with AppleScript
Authored by: Zil0g on Tue, Jul 31 2007 at 8:21AM PDT
There is a very useful application to control airport via Terminal. Named "airport", this command can be found and installed via MacPorts.

[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: keyslapper on Tue, Jul 31 2007 at 8:25AM PDT
Hmm ... No, it doesn't work like that for me:

$ scselect AirPort-Off
Set "AirPort-Off" not available.

Defined sets include: (* == current set)
0 (Automatic)
* AB4F8C43-A465-4DFA-80BC-BA93FDBD51FB (Work)
8FF3D441-21F0-432A-B216-3A7B2D3F3456 (Home)
(Hex strings and Location names modified to protect the guilty)

The manpage suggests that scselect only provides access to different location configurations. No mention of "AirPort-Off" or "AirPort-On".

[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: Eric3 on Tue, Jul 31 2007 at 8:45AM PDT

The scselect command is for switching between locations you've defined in System Preferences > Network. The command I listed wouldn't work for you because you don't have a Location named "AirPort-Off". You can create a new location with the "New Location..." command in the Location: popup menu; in my case I created one named "AirPort-Off" and disabled AirPort for it.

Now in your case, you have three locations defined: Automatic, Home, and Work. To switch between them, you would run one of "scselect Automatic", "scselect Work", or "scselect Home".

---
---
Eric3

[ Reply to This | # ]

Turn AirPort on or off with AppleScript
Authored by: rgspb on Tue, Jul 31 2007 at 9:27AM PDT
Maybe I'm missing something here, but I find it quite simple and very quick and easy to go to the AirPort menu on the menu bar and pull down to Turn AirPort Off. It's a simple two click process.

[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: keyslapper on Tue, Jul 31 2007 at 10:18AM PDT
I think the article stated the hint was for the command line oriented user. Not that it's "hard" or terribly "inconvenient" to mouse it, but having a script that can be called from QS is pretty handy.

I'm pretty sure the article didn't say it was better, just another way to do it. Assuming it works for you.

[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: Stef@nK on Tue, Jul 31 2007 at 10:21AM PDT
I prefer the XNetwork OSAX by Jean-Baptiste Le Stang with the commands
start airport
and
stop airport



[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: hofbauer on Fri, Aug 10 2007 at 8:18AM PDT
Oh man, thanks so much for this! This is the real hint!

[ Reply to This | # ]
Only tangentially related to AirPort, AppleScript
Authored by: ClassicUser on Tue, Jul 31 2007 at 11:46AM PDT
OK, MacOSXHints folks, PLEASE rename this hint.

It's a great hint for command-line manipulation of network locations, but doesn't really have anything specifically to do with AirPort. Also, the "AppleScript" portion is merely a wrapper for a command-line utility call.

Plus, from the other comments here, it seems others are confused by the need to establish a location with the given name, to get this to work (yes, this is clearly indicated in the hint, but still)

Can we rename this "Change network location via script" or the like, to make it more understandable?

[ Reply to This | # ]
It LIES!
Authored by: ubi on Tue, Jul 31 2007 at 11:55AM PDT
Unchecking Airport under Network Preferences / Network Port Configurations doesn't turn off power to the Airport card. This is very important to understand. Even though the menu icon appears as if the Airport power is off, it's still running. I wish that unchecking a port (Airport / Bluetooth) would actually turn off the power to these interfaces.

[ Reply to This | # ]
Re: It LIES!
Authored by: Eric3 on Wed, Aug 1 2007 at 7:14AM PDT
Aw geez, you're right. Well, that pretty much defeats the whole purpose of this hint.

---
Eric3

[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: eisbaer on Tue, Jul 31 2007 at 1:53PM PDT
there is an older hint from 2004
http://www.macosxhints.com/article.php?story=20041215002018178&query=networksetup
The solution seems still to be valid:
Apple provides for the server version two useful tools which does the job quite well: systemsetup and networksetup

[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: Mac Berry on Wed, Aug 1 2007 at 2:16PM PDT
I didn't see anything there saying it was for those who don't like to use the GUI, and in fact the hint said "Although there seems to be no reliable way of turning AirPort on or off directly via the shell,", which is simply not true.

I'm afraid I see this one as a solution to a problem that doesn't exist!

[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: Mac Berry on Wed, Aug 1 2007 at 3:44PM PDT
Umm, that was meant to be a reply to this. Sorry, I hit the wrong "reply" button I think.

Mark

[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: guych on Wed, Mar 26 2008 at 4:19AM PDT
Turn AirPort on or off with AppleScript
Authored by: Wim37øµ on Wed, Sep 17 2008 at 2:42PM PDT
This worked perfectly for me using 10.5.5 on a powerbook.

[ Reply to This | # ]
Turn AirPort on or off with AppleScript
Authored by: ljr_nbg on Fri, Nov 7 2008 at 11:27AM PST

Why not:

do shell script "networksetup -setairportpower on" with administrator privileges

respectively

do shell script "networksetup -setairportpower off" with administrator privileges

(okay, you need to enter an admin password)



[ Reply to This | # ]
Quicksilver
Authored by: norz on Fri, Nov 21 2008 at 3:02PM PST
Another fast way to switch quickly between network locations, is to use Quicksilver with the <a href=http://docs.blacktree.com/quicksilver/plug-ins/network_locations?DokuWiki=>network location module</a>.

[ Reply to This | # ]