Benches Networking

9

changing network location with applescript

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I'm trying to change network location with applescript. I used the instructions from this page
http://www.macosxautomation.com/applescript/features/system-prefs.html

I have two locations, called Automatic and testlocation. when I run the following script nothing happens. no errors, but the location does not change

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #ADD8E6;
overflow: auto;"
title="this text can be pasted into the Script Editor">
tell application "System Events"
tell network preferences

set current location to location "testlocation"

end tell
end tell</pre>

am I missing something here? I tried it booted in 10.5.8 and 10.6.1. changing the location manually works just fine.

9 replies

Sign in or join Beachball Help to reply.

  1. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    What you can do is run "scselect" in Terminal. This will give you a unique ID for each of your networks. The following script is one that can be used for two networks named "Airport #1" and "Ethernet #1":

    tell application "System Events"
    tell network preferences
    get the name of current location as text
    set a to the result
    end tell
    end tell
    tell application "Finder"
    activate
    display dialog "You are using network " & a with icon 1
    display dialog "Do you want to change networks?" with icon 1 buttons {"Airport #1", "Ethernet #1", "No"} default button 3
    set userChoice to button returned of the result
    if userChoice is "Airport #1" then
    do shell script "/usr/sbin/networksetup -setairportpower airport on"
    delay 2.5
    do shell script "scselect XXX12345-111X-4E68-AA35-CA6D04D5A2BF" <<example string<<
    else
    if userChoice is "Ethernet #1" then
    do shell script "/usr/sbin/networksetup -setairportpower airport off"
    delay 0.5
    do shell script "scselect 1824XX49-ADE6-496F-A107-B2F7F57903F5" <<example string<<
    end if
    end if
    end tell

  2. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thanks a lot. this is even more than i wanted . I should have mentioned that I knew one could do it with scselect but I really wanted to know why the native apple script method described in that apple tutorial is not working. either I'm missing something really obvious there (can't think what though) or it's a bug.

  3. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    tht page is a Leopard page (and in fact that code works perfectly on my 10.5.8). Snow Leopard may have changed things, but I don't have access to the System Preferences dictionary to see if there's any differences. I'd guess there are, though, judging by results...

  4. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    You're quite welcome. From what I understand "/usr/sbin/networksetup -setairportpower airport on" used to be "/usr/sbin/networksetup -setairportpower on". That was what I had to change when I went to Snow Leopard.

  5. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    twtwtw wrote:
    tht page is a Leopard page (and in fact that code works perfectly on my 10.5.8).

    really? It does nothing on my 10.5.8 powerbook as I mentioned in my first post. does it really change the location for you when you look in network system preferences?

    Snow Leopard may have changed things, but I don't have access to the System Preferences dictionary to see if there's any differences. I'd guess there are, though, judging by results...

  6. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    V.K. wrote:
    really? It does nothing on my 10.5.8 powerbook as I mentioned in my first post. does it really change the location for you when you look in network system preferences?

    yeah, it does. if it doesn't on yours the only thing I can imagine is that you have a typo in the location name you're using (a space or non-printing character, maybe?)

  7. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    nope, no typos. in fact, if I put a wrong location the script returns an error when it runs.

  8. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    fascinating. are you seeing any errors in Console?

  9. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    no, absolutely nothing at all in the console.