Change "Location" with a script?

Dear all,
I am working in two different (network) environments and need to change both network settings (proxy, ...) and also computer settings (CUPS server) and some environment variables depending on my physical location. For the latter two I have a little bash script that does that conveniently.

But changing the network always requires a few clicks extra... I was wondering if it was possible to change the network environment ("Location" in the Apple menu) by a script (I tried with AppleScript and Automator but didn't find anything) or to run a script automatically whenever I change the Location. This way I would only have to run either the script or change the Location to adjust all settings.

Can anyone help me here?

Thanks a lot,
Leonard

MacBook Pro, Mac OS X (10.6.2)

Posted on Feb 9, 2010 12:36 AM

Reply
8 replies

Feb 9, 2010 6:49 AM in response to Leonard Burtscher

Hi Leonard,

Here's a script that uses [GUI scripting|http://www.macosxautomation.com/applescript/uiscripting/index.html] to select a location from the Apple menu:

*tell me to activate*
*tell application "System Events" to set theList to name of locations of network preferences*
*set R to choose from list theList*
*if R is false then return*
*tell application "System Events" to tell (process 1 whose frontmost is true)*
*click menu item (R as text) of menu 1 of menu item "Location" of menu 1 of menu bar item 1 of menu bar 1*
*end tell*

Hope it can help.

Feb 10, 2010 8:07 AM in response to Pierre L.

Hi Pierre,
Thanks for pointing me to this excellent page. I think will have to visit that more often if I want to learn AppleScript-ing!

I tried both your script and the script on their system prefs page (under "Network") but always get an error message:

+System Events got an error: Can’t get menu item "Location" of menu 1 of menu bar item 1 of menu bar 1 of process 1 whose frontmost = true.+
(when I run your script)

+System Events got an error: Can’t get display name of network preferences.+
(when I run the above mentioned script, see link)

The location has not changed. I have activated "Enable access for assistive devices" in the Universal Access System Preference.

Do you know why this doesn't work?

Thank you!
Leonard

Feb 10, 2010 10:07 AM in response to Leonard Burtscher

I can see at least three explanations for the error “Can’t get menu item "Location" of menu 1 of menu bar item 1 of menu bar 1 of process 1 whose frontmost = true”:

1. There's no menu item “Location” in your Apple menu, which occurs when you have got just one location (“Automatic”). Then you should add at least one other location in the “Network” pane of the System Preferences.

2. It might be that the script runs faster on your computer than on mine. Then just add a “delay” statement (like “delay 1”, “delay 0.5” or “delay 0.25”) before any problematic line of code.

3. If you are using a foreign version of Mac OS X, the menu item "Location" might have another name (such as “Configuration réseau” with the French version of Mac OS X). You should then change the menu item name in the script.

Hope it can help. Please let me know if any of the suggestions above fixes the issue.

Message was edited by: Pierre L.

Feb 16, 2010 1:50 AM in response to Pierre L.

Hi Pierre,

Thank you. Your point number 3 was the solution. (I actually wanted to mark your message as "solved"... Now I have marked the question as "answered").

While this works, ideally I would like to have one script that does both the changes to the shell (for this I already have a shell script) and the changes to Mac OS.

On http://developer.apple.com/Mac/library/documentation/AppleScript/Conceptual/Appl eScriptX/Concepts/workwithas.html I found a way to call an AppleScript from the shell (using the #!/usr/bin/osascript interpreter) -- but if I run the script you sent I get

142:166: execution error: No user interaction allowed. (-1713)

In fact I would prefer the script to run without user interaction, i.e. to just put in the name of the location that I want to switch to and not display a selection box. I would then just create two or three scripts for my most frequently used locations. I tried a few variants but I didn't manage to set 'R' which is probably the variable containing the new location to a fixed value. Could I ask you again for help with this?

Thanks again,
Leonard

Feb 16, 2010 4:31 AM in response to Leonard Burtscher

Hi Leonard,

If I understand correctly what you are asking for, you would like to have one AppleScript for each location, which you could run from a shell script, with no other user interaction than launching the shell script. The following line of code should do exactly that.

*tell application "System Events" to tell (process 1 whose frontmost is true) to click menu item "Office" of menu 1 of menu item "Location" of menu 1 of menu bar item 1 of menu bar 1*

Just replace “Office” with “Home” (for example) or anything else in your other scripts.

Hope it can help.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Change "Location" with a script?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.