How can one get the current Network Location in a script?
I want to write a script that will mount a Samba share depending on the network location (i.e. mount share#1 at home but mount share#2 at work). I’ve seen lots of references to changing the location via UI scripting but not for getting the location.
Here’s what it looks like thus far (I realise there’s no such thing as machine location):
if machine location = "Home" then
tell application "Finder"
mount volume "smb://user@serverathome/share/"
end tell
else if machine location "Work" then
tell application "Finder"
mount volume "smb://user@serveratwork/share/"
end tell
end if
tell application "System Preferences"
activate
end tell
tell application "System Events"
tell application process "System Preferences"
set frontmost to true
click menu item "Network" of menu "View" of menu bar 1
--you may need a delay here, adjust to suit
delay 1
set machineLocation to value of pop up button 1 of window "Network"
end tell
end tell
tell application "System Preferences" to quit
Then use your if statement to select which server gets mounted.
Thanks for your help, it works great! The code below is my finished product (for others use).
As a question, is there a none GUI way of doing this sort of thing? I’m surprised that there isn’t some sort of system value for the network location.
tell application "System Preferences"
activate
end tell
tell application "System Events"
tell application process "System Preferences"
set frontmost to true
click menu item "Network" of menu "View" of menu bar 1
delay 2 --you may need a delay here, adjust to suit
set machineLocation to value of pop up button 1 of window "Network"
end tell
end tell
tell application "System Preferences" to quit
if machineLocation is "Home" then
tell application "Finder"
if not (exists "Shared") then
mount volume "smb://login@homeServer/Shared/"
end if
end tell
else if machineLocation is "Work" then
tell application "Finder"
if not (exists "serverShared") then
mount volume "smb://login@workServer/serverShared/"
end if
end tell
end if
Hey, I’m new to the whole scripting thing, and I want to learn more, but for now I was wondering how I could create an apple script that would change my IP from a DHCP to a DHCP with manual IP adress i am using the built in ethernet. If I could get a little help that would be great.
thanks
Chapstick,
Welcome to AppleScript. You’ll wonder why it took you so long.
You have to use GUI scripting and set up however many Locations as needed in the Network preferences. Once you set up the locations, you can use GUI scripting to select the location you want. You can use the test in scratch’s script to toggle the location, or set up an interactive list of locations.
I like this better
"why use the GUI scripting when you can just have
do shell script “scselect xxx”
where xxx is the location names. I’m actually trying to write an applescript studio application that gets the list of locations and lets you chose from them, but that also allows you to set what times to switch to which locations, with a manual override. :)"