Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,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 3.43 / 5
  You rated: 4 / 5 (7 votes cast)
 
[35,824 views]  

Turn AirPort on or off with AppleScript | 18 comments | Create New Account
Click here to return to the 'Turn AirPort on or off with AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Turn AirPort on or off with AppleScript
Authored by: Zil0g on Jul 31, '07 08:21:20AM

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 Jul 31, '07 08:25:06AM

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 Jul 31, '07 08:45:45AM

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 Jul 31, '07 09:27:17AM

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 Jul 31, '07 10:18:05AM

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 Jul 31, '07 10:21:30AM
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 Aug 10, '07 08:18:06AM
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 Jul 31, '07 11:46:16AM

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 Jul 31, '07 11:55:21AM
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 Aug 01, '07 07:14:44AM

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 Jul 31, '07 01:53:09PM
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 Aug 01, '07 02:16:34PM

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 Aug 01, '07 03:44:59PM
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 Mar 26, '08 04:19:12AM
Turn AirPort on or off with AppleScript
Authored by: Wim37øµ on Sep 17, '08 02:42:55PM

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 Nov 07, '08 11:27:03AM

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 | # ]
Turn AirPort on or off with AppleScript
Authored by: dzg on Nov 02, '10 03:56:11PM
Nice! But you need device name, and I did not need admin priveleges:

do shell script "networksetup -setairportpower en1 off"
do shell script "networksetup -setairportpower en1 on"


[ Reply to This | # ]
Quicksilver
Authored by: norz on Nov 21, '08 03:02:15PM
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 | # ]