Jump to content

How to simplify choosing Network Locations with AppleScript

+ 1
  macnlos's Photo
Posted Feb 22 2011 08:58 PM

My wife just got a MacBook Pro and I went in and created a network location that is specific to our home and another for when she's outside of the home. She keeps calling me and asking where to go to change it. I'm sure she will remember but it got me wondering if I could make it a little simpler for her. AppleScript fits the bill and allows me bundle a little applet that she can put on her dock and run when needed.

The "scselect" command will display all the registered locations from the command prompt. If you pass the scselect command a location name then it will change the location to that name. For example if I wanted to set the network location to "Home", I would do the following at the command prompt:

scselect 'Home'


So with that being known I then approached the applet so that it would pull a current list of locations, present them to the user, and then allow the user to change it. The following code can be compiled and run with AppleScript. If you want to make it into an applet then do a "Save As" and change it's type from "Script" to "Application".

UPDATE
I didn't like the fact that a user could choose to switch to their current location so I modify the script to remove it as a choice. I also added credits to where I found examples of this work.

[font="Courier New"]
-- Network Switch
-- Carlos Randolph
-- February 22, 2011
-- OS X 10.6.6

-- I wrote this script to provide an easy way for my wife
-- to switch network locations without having to remember
-- where to do it in System Preferences.

-- Copyright: there is none, do what you want with it, use at your own risk

-- version 1.0 - 10/22/2011 - initial release
-- version 1.5 - 10/23/2011 - doesn't display current location as a choice

-- macscripter.net - remove item from list logic
-- http://macscripter.net/viewtopic.php?id=24525
--
-- www.geekology.co.za - examples of scselect command and how to shell out
-- Willem van Zyl
-- http://www.geekology.co.za/blog/2009/02/change-os-x-network-location-from-command-line-or-applescript-application/

tell application "System Events"
	tell network preferences
		
		-- get the current network lcoation
		set activeLocation to the name of current location
		
		-- get all network locations and add Quit to the list
		set allLocations to the name of every location
		copy "Quit" to the end of allLocations
		
		-- remove the current location from the list
		set deleteFromList to {activeLocation}
		set changeLocations to {}
		repeat with i from 1 to count allLocations
			if {allLocations's item i} is not in deleteFromList then
				set changeLocations's end to allLocations's item i
			end if
		end repeat
		
		-- display a dialog for the user to choose which network to switch to
		set choiceLocation to the button returned of (display dialog "Choose a Network Location" buttons changeLocations default button "Quit")
		
		-- if they quit then skip
		if (text of choiceLocation) = "Quit" then
			display dialog "You chose to Quit" buttons {"OK"} default button 1
		else
			do shell script "scselect '" & (text of choiceLocation) & "'"
			display dialog "Changed network to " & (text of choiceLocation) & "." buttons {"OK"} default button 1
		end if
	end tell
end tell[/font]


5 Replies

 : Feb 23 2011 06:17 AM
I'm looking at the code and the one thing that bothers me is how the current network location is available as an option. The applet should say what the current location is but not have it as a switch choice. This will eliminate the need for the test to skip if someone chooses the current location. I will update it tonight.
=========================
Sent to you from my
iPad, iPhone, BlackBerry,
Laptop, Desktop, or
Kitchen Toaster
+ 1
  johlke's Photo
Posted Apr 14 2011 05:23 AM

Or even more simple from the Apple menu choosing menu Location and then your preferred location. See also http://discussions.a...787489&tstart=0
0
  macnlos's Photo
Posted Apr 14 2011 07:16 AM

Now what fun would that be? I didn't even see/notice that, thanks for the tip!
=========================
Sent to you from my
iPad, iPhone, BlackBerry,
Laptop, Desktop, or
Kitchen Toaster
 : Apr 14 2011 08:15 PM
The same happened to me: I was looking for a script to automize that and found your post and the other one that tells to use the Apple menu. Happy to have found both.
0
  CFMaurer's Photo
Posted May 18 2012 04:41 PM

O.k I forgot about the apple menu locations too, but with this post as inspiration I've got an even quicker solution. I create a script with do shell script "scselect 'manual entered location'" for each of my common network locations "home", "office_man_ip", "dev_server" etc. Then I just store them all in a folder that spotlight indexes. Now all I have to do is hit the spotlight command (apple-space by default) type in the first few letter of the location and bam it's done. It's an extra fast and easy location change with no mousing around. The downside is that you do have to manually create your quick location change scripts for each new location you add (which I don't do a lot) and you have to actually remember what your location is called (which might only be hard if you don't know where you are, in which case changing your network location may not be a top priority).

Thanks for sharing it helped me learn more about applescript and build something practical!