This is a website by Willem van Zyl

I'm a project manager, software developer, Apple evangelist and geek from South Africa. I'm passionate about web and mobile application development, usability, productivity, physics, astronomy, science fiction and fantasy.

If you would like to contact me, message me on Twitter or send me an email.

How to change the OS X network location from the command line or an AppleScript application

12 Feb 2009

In Mac OS X Leopard, Network Locations are defined in System Preferences->Network, and allow users to assign their DHCP, IP, Subnet, Router, DNS, WINS, etc. settings to preconfigured sets. These sets can be activated by simply selecting a different Network Location (e.g. "Home", "Office", "Coffee Shop") on the Network Preferences panel.

The scselect command line utility can be used to list all Network Locations on an OS X system by opening Terminal (from your "/Applications/Utilities" folder), and entering:

scselect

It's also possible to switch your Network Location to a different preconfigured set by entering:

scselect "{location_name}"

Using AppleScript and the scselect command line utility (and optionally adding the free Growl user notification utility), it's possible to easily build an application that will toggle the system between Network Locations when run:

#
# Switch Network Location.scpt
#
#   This script retrieves a list of all available network
#   locations, compares each to the current network location,
#   and then switches the current network location to the
#   next available one.
#
# Last updated: 2009/02/12
#
# Willem van Zyl
# willem [ - a t - ] geekology [ - d o t - ] co [ - d o t - ] za
#

tell application "System Events"

    tell network preferences
        get the name of every location

        --mark that the next location  in the
        --list of available locations will not
        --become the new location:
        set switchOnNextLocation to false

        --create a list of available location names:
        set allLocations to the name of every location

        --store the name of the active location:
        set activeLocation to the name of current location

        --set the name of the new location to the first
        --available location by default:
        set newLocation to the first item of allLocations

        --loop through all available location names:
        repeat with thisLocation in allLocations
            if switchOnNextLocation is true then
                --store the name of the new location:
                set newLocation to thisLocation

                --mark that the next location  in the
                --list of available locations will not
                --become the new location:
                set switchOnNextLocation to false

            end if

            --check if the current location in the list of
            --available locations is equal to the active location.
            --if it is, the next location in the list of
            --available locations will become the new location.
            if (text of thisLocation) = (text of activeLocation) then

                --mark that the next location will become the new location:
                set switchOnNextLocation to true
            end if
        end repeat

        --use the scselect utility to switch the
        --network location:
        do shell script "scselect '" & newLocation & "'"

    end tell

end tell

-- Check if Growl is running:
tell application "System Events"
    set growlIsRunning to ¬
        (count of ¬
            (every process whose name is "GrowlHelperApp")) > 0
end tell

--Only display notifications if Growl is running:
if growlIsRunning = true then

    tell application "GrowlHelperApp"
        -- Make a list of all notification types:
        set the allNotificationsList to ¬
            {"Network Location Switched"}

        -- Make a list of the default enabled notifications:
        set the enabledNotificationsList to ¬
            {"Network Location Switched"}

        -- Register the script with Growl
        -- using either "icon of application"
        -- or "icon of file":
        register as application ¬
            "Switch Network Location" all notifications allNotificationsList ¬
            default notifications enabledNotificationsList ¬
            icon of application "Switch Network Location"

        -- Send a notification:
        notify with name ¬
            "Network Location Switched" title ¬
            "Network Location Switched" description ¬
            "The network location was switched to '" & newLocation & ¬
            "'." application name "Switch Network Location"
    end tell

end if

Geekology's example of this Switch Network Location script can be downloaded here, with a compiled version of the application available here.

Do you like this? Share it:

Copyright © Geekology 2011. All Rights Reserved.

Hosted by Code. Like. Clockwork.