Determine current network location

Hi there!

Is there any scriptable way to determine what my current network preference “Location” is?

I’d like to be able to adjust what drives I auto-mount via a login script based on what my current location is set to at login time.

Thanks in advance!

-John

Your network IP Address?

do shell script "ifconfig -a | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}'"

That would work for my IP address, which I suppose I could turn around and mish-mash back into a location based on addressing rules. But that could be a bit iffy with DHCP and a common addressing scheme (10.x)

What I was actually referring to is the System Preferences - Network - “Location” (or Apple Menu - Location) setting that controls which ports, protocols and network configurations are active, etc.

I have a login script now that automatically mounts up some NFS and SMB shares for work, but it bombs at home. I’d like to add some smarts to it, so it can say:

“If I’m at ‘~Work~’, I should mount all this stuff. If I’m at ‘~Home~’ I should mount my personal G4’s local and external HDs via AFP and not even attempt to mount the work stuff”

From the looks of Goggling, there was a “Location Manager” that could provide this info in OS9, but there dosent’ appear to an equivalent for OSX.

Clear as mud, right?

:wink:

This is my first dip into AppleScript, so you’ll have to walk me through the solution I’m afraid.

Thanks!

-John

Oh, I see… You wish the name of the “location” in the popup menu…
Hmmm… No idea. It must be stored somewhere in a plist file, but I can’t help you.

Maybe this sample code from Apple can help you:

tell application "System Events"
	tell process "Finder"
		tell menu "Apple" of menu bar 1
			tell menu "Location" of menu item "Location"
				get properties of every menu item
			end tell
		end tell
	end tell
end tell

You must have installed latest beta “System Events”, so you can check in the “get properties” statement its “selected” property.

Why not just wrap your script in a try block? If mounting the work related volumes fails, try mounting the home related volumes. If that fails, there may be a network issue and then raise a notification that the script couldn’t mount anything.

Jon

I’ve been trying for the past several hours to do exactly the same thing. The way to do it seems to be to write a shell script using the command “scselect” and running it as a LoginHook. I can’t seem to get it to work though. Try Googling those terms and give it a try. Let me know if you get it to work.

This works for me because I have two different accounts: one for work and one for home. I created the following Applescript do shell script “scselect n” (where “n” is the location, in my case 1 for home 2 for work, you can find this by running “scselect” from a terminal window) Then from the Applescript editor I chose “Save as Run Only”. For format choose “Application” and make sure “Never Show Startup Screen” is selected. Save the file and then put it in your Login items from System Preferences. Do the same for you other location. The app runs, sets your location, and quits. Hope this helps.