Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TheScavenger

macrumors 6502
Original poster
Jul 14, 2007
259
11
Kansas City, MO
I am trying to figure out how to automatically connect a MacBook to a network drive after connecting to the wireless network that the drive is connected to.

Here is the situation:

I back-up my wife's MacBook via Time Machine to an external hard drive that is connected to my Apple desktop. It works great! I even have her system setup to automatically mount the drive upon log-in. Unfortunately, after leaving our wireless signal and then coming back the time machine drive isn't automatically mounted since she isn't "logging back-in". Is there an automator script that can be built to automatically connect to drive X after connecting to wireless network Z?

Thanks
 

calderone

Cancelled
Aug 28, 2009
3,743
352
You can't do this with Automator.


The simplest solution would be a shell script running at a set interval, say 5 minutes, it would be set via launchd.

When the shell script runs, it check the SSID you are currently connected to, if it is network Z then it mounts that drive, if not then it doesn't. Essentially you would just have a daemon running in the background checking for that SSID.

I already wrote the shell script, let me know if this interests you and I can move forward with the rest.

It would be very similar to this: https://forums.macrumors.com/threads/162636/, except kicker.bundle no longer exists but there are solutions to that.
 

calderone

Cancelled
Aug 28, 2009
3,743
352
You know, I could actually use launchd's WatchPaths option to make it happen when networks are transitioned, this thought led me to this.

This is a pretty robust solution, http://tech.inhelsinki.nl/locationchanger/

This would be pretty much the same implementation I had in mind. Give it a shot.
 

TheScavenger

macrumors 6502
Original poster
Jul 14, 2007
259
11
Kansas City, MO
You know, I could actually use launchd's WatchPaths option to make it happen when networks are transitioned, this thought led me to this.

This is a pretty robust solution, http://tech.inhelsinki.nl/locationchanger/

This would be pretty much the same implementation I had in mind. Give it a shot.

I read through the link but I have no idea how to use that data to accomplish what I need. Have any hints? :eek:
 

maxl

macrumors newbie
Feb 2, 2013
7
0
I am responding to this even though it is a couple years old because it shows up in a Google search. Here is what I did to mount disks automatically when I change networks.

First I created an applescript to mount the drive:

Code:
tell application "Finder"
	try
		mount volume "[B]addressOfYourVolumeGoesHere[/B]"
	end try
end tell

Make sure you update the appleScript file to reflect the address of your drive. You can find this by mounting your drive manually then right-click > get info.

Next I created LocationChanger.plist (thank you for the idea calderone) file to listen and act on any changes in the /Library/Preferences/SystemConfiguration folder:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>my.locationchanger</string>
	<key>ProgramArguments</key>
	<array>
		<string>/bin/locationchanger</string>
	</array>
	<key>WatchPaths</key>
	<array>
		<string>/Library/Preferences/SystemConfiguration</string>
	</array>
</dict>
</plist>

Finally I created locationchanger, which is a bit different from what the link that calderone posted was trying to do:


Code:
#!/bin/bash

# automatically mount network volumes on network change

# redirect all IO to /dev/null
exec 1>/dev/null 2>/dev/null

# wait 2 seconds for connection to be established
sleep 2

# get various system information
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\ | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' '`

# SSID will not include spaces so leave them out when entering the name of your network

case $SSID in 
	
	# Home network SSID
	'your network SSID goes here. keep the single quotes' )
	       # do whatever you want to do. I am mounting network drives
	       /usr/local/bin/growlnotify -m "Joined home network and mounting Airport drives..."
	       osascript ~/appleScripts/mountAirportMedia.scpt
	       osascript ~/appleScripts/mountAirportBackup.scpt
	       /usr/local/bin/growlnotify -m "Airport drives mounted!"
	
	;;

	# Network 2
	'network2' )
	       # do whatever you want to do.
	       /usr/local/bin/growlnotify -m "Joined network 2. Doing nothing."

	;;

	# any unspecified network
	* )
	       /usr/local/bin/growlnotify -m "Joined unknown network. Doing nothing."
	       # do whatever you want

esac

exit 0


That's it and it is working great. I found it easier to just call applescript files then do everything in the shell. But it is up to you.

LocationChanger.plist goes in ~/Library/LaunchAgents/LocationChanger.plist (note: ~/Library/ is in your user home directory and hidden by default.)

locationchanger goes in /bin/locationchanger (note: /bin/ is in your systems root directory and hidden by default.)

If you wrote your own locationchanger make sure it is executable. You can change it with the terminal command:

chmod 755 pathToYourFile

Once you have put all the files in place and are ready to go type:

launchctl load /Library/LaunchAgents/LocationChanger.plist

into the terminal or restart your computer to start it up.

I also used growlnotify to let you know what it is doing.

I have attached the files I used so you can use them instead of creating your own if you want.
 

Attachments

  • autoMountVolumes.zip
    4.2 KB · Views: 552

kristiaanpipijn

macrumors newbie
Feb 7, 2013
3
0
Hey maxl,

I was very pleased to see a recent reply to this post just now, since I want to do exactly the same over here.

Although I can't seem to get it to work.
The applescript works, I tested it, it mounts the drives everytime.

I followed the other instructions very closely, but no luck.
No growl notifications some I'm wondering if it is able to launch the script.
Is there any way for me to check that?

when I try to load the launchctl thing, terminal responds that it's already loaded. I have no clue!

Care to help me out a bit?
I'm running Mountain Lion, does that matter?
My wifi is called "Gebakken lucht". Does that mean I have to put 'Gebakkenlucht' in the locationchanger?

BTW, in your instructions you say LocationChanger.plist goes in ~/Library/LaunchAgents/LocationChanger.plist. I guess you meant inside ~/Library/LaunchAgents/

And you probably mean 'launchctl load ~/Library/LaunchAgents/LocationChanger.plist' instead of 'launchctl load /Library/LaunchAgents/LocationChanger.plist'

Hope you could help me out!
 

maxl

macrumors newbie
Feb 2, 2013
7
0
hey, you might not have growlnotify installed. You can download it here: http://growl.info/downloads just get the Growl 1.2.2 - For 10.6 and 10.5. growlnotify is in the Extras folder.

To test and see if it is your LocationChanger.plist file or the locationchanger script just double click on locationchanger, if it launches a terminal window and mounts the drives then the script is working fine and issue is with the .plist file.

once the launch agent is loaded if you make changes to the plist file you have to unload it via launchctl unload before you can load it again.

you can find out the name of your network by pasting:

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\ | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' '

into a terminal window. it will just display the network name as it will be stored in the variable.

also if the file path starts with a "~/" that is your home library. If it starts with a "/" then that is the root of the system. with the exception of the last one you pointed out. My terminal window prompt starts in my home directory so i left out the "~" for that launchctl command.

also, if you want you can add in a line like:

echo -e "Airport drives mounted!" >> ~/Desktop/debug.txt

anywhere in the locationchanger script. anything you type between the quotations will be written to a file on your desktop called debug.txt when the script is run. this helps a lot in locating any issues in the script file but this cannot be placed in the .plist file.

good luck. hope this helps, if you are still suck let me know and I'll do what i can to help.
 

kristiaanpipijn

macrumors newbie
Feb 7, 2013
3
0
Thanks for the help, maxl!

I'm not there yet...

* growl is working.

* probably it's the locationchanger file. double-clicking it does not mount the drives.

* found the ssid with /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\ | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' ' in terminal. I had the ssid correct in the locationchanger, so thats not the issue.
Although: when I was putting this string in terminal, before giving the ssid, terminal says "airport: invalid option --".
Since this string is also in the locationchanger, might this be a problem?

* so by moving the debug string around in the locationchanger, thats how I'll might be able to locate the string where the locationchanger doesn't work? I'll start testing now. I'll keep you informed!

UPDATE: the debug.txt gets updated on every run until the debug string is behind the following line: "case $SSID in". So when the debug string is behind "case $SSID in", the debug.txt doesn't get changed.

Thanks a lot already, help is greatly appreciated!
 
Last edited:

maxl

macrumors newbie
Feb 2, 2013
7
0
"airport: invalid option --" isn't an issue, I get that too on mine and it still works fine.

First thing I would check is that the path to your applescript is correct. Maybe you forgot the '~' at the start to indicate your home directory?

If you place echo -e "case 1" >> ~/Desktop/debug.txt in the first statement and echo -e "case 2" >> ~/Desktop/debug.txt in the second statement etc. You will be able to figure out which case it is choosing.
 

kristiaanpipijn

macrumors newbie
Feb 7, 2013
3
0
Ha! Found it!

I did some searching around on the osascript, because everything else seemed to work fine. That's when I discovered the following.
I had placed the mountscript inside a folder inside the Application Support folder.
I think somehow it wasn't able to handle the space character in the pathname, because when I put the script in a different location (no spaces in pathname) the script is running how it should, growlnotifications and everything.

Any ideas on that?

Anyway, thanks again for the great script and your help!
 

maxl

macrumors newbie
Feb 2, 2013
7
0
A space is a special character. If you want to use it in a path you have to escape it. In your case you would have needed to type ".../Application\ Support/..." the '\' tells the computer to treat the space as a space instead of the end of the command.

Glad you got it working in the end.
 

blovell

macrumors newbie
Jun 11, 2013
2
0
can't get it

Hey maxl. Thanks so much for posting these instructions. I'm trying, but I can't seem to get it to work for me. I've downloaded the .zip file that you posted and made changes to locationchanger, and placed it in the /bin/ directory in the root folder. I've also made the necessary changes to the script file and pointed locationchanger to it. When I double-click on locationchanger, terminal opens and the network drive mounts as expected. The difficulty I'm having is that, even with the .plist file in LaunchAgents in my home directory, the drive doesn't mount automatically on startup or login. Any suggestions? Is there something I need to change in the .plist file?

Thanks for your help.

UPDATE: Never mind. It started working all of a sudden. Not sure what the glitch was but it's working great now. Thanks for a great tip!
 
Last edited:

maxl

macrumors newbie
Feb 2, 2013
7
0
Once the .plist file is loaded it just listens for a change in your network configuration file then does whatever you have asked it to, in this case check the network name and mount a drive. A network change has to occur to trigger the mounting of the drives. If you did launchctl load with the network connection already active nothing would happen because nothing in your network configuration file changes so the program would not be not triggered.

Glad you found this useful and that you got it working.
 

liquefry

macrumors newbie
Oct 25, 2013
6
0
Thanks for this tip, it works a treat. One thing I've found is that on waking my Macbook from sleep, the script appears to runs twice. I'm running Mavericks, get the following messages when waking up:
- a system notice that my share drive has been disconnected (this always happened before because I gather the wi-fi turns off when in sleep).
- growl notifications that the drives are being connected - both the connecting and connected notice
- a few seconds later, the first "connecting" notice happens again. The connected notice doesnt appear (presumably because the drive is already connected).

Any thoughts, or ideas of how I can diagnose? It is a minor irritation rather than any real problem. I did a launchctl list and it only shows up once, the script is pretty much as posted above.
 

DeJung

macrumors newbie
Dec 17, 2013
3
0
Can't get it to work

Hi Maxl,

your solution is just what i searched for. Did everything just like explained in your "instructions.rtf" but i can't get it working.
I'm trying to explain the circumstances, so maybe you or someone else can figure out what went wrong:

1. My OS is OS X 10.9

2. I did all the necessary changes in the scripts and locationchanger - not sure at that point if the SSID has to be entered just like shown in the Network-Settings-Panel e.g. "Office" or like its stored in /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist with the cryptical name?

3. When i change networks just nothing happens.

4. To figure out where the problem is i tried to start locationchanger manually, the terminal appears and shows:
Code:
Nicolass-MacBook-Pro:~ Nicolas$ /bin/locationchanger ; exit;
logout

[Prozess beendet]
and nothing further happens (logout appears 5 Sek. after starting - just the time i set the sleep for).

5. I tried to load the *.plist manually - that leads to a message telling me it's already loaded. So this part seems to work.

6. I tested my scripts manually - they work fine.

7. I used your terminal command to make locationchanger executable.

Now i'm really at the end of my knowledge and hope someone can help to get this working.

Regards Nic

_________________________________________________________________

Update:

8. Installed growlnotify to get some feedback - and that shows where the problem is: location changer seems to recognize all networks as unspecified networks. So i played around a bit with the SSID in location changer and tested both the screen name e.g. 'office' and the cryptical one e.g. ‘RmlybWE=' stored in /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist
Both ways didn't bring success.
So my thought now is: Did Mavericks change anything that could affect the first part of locationchanger?

Code:
#!/bin/bash

# automatically mount network volumes on network change

# redirect all IO to /dev/null
exec 1>/dev/null 2>/dev/null

# wait 2 seconds for connection to be established
sleep 2

# get various system information
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A
/Resources/airport -I\ | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' '`

Any suggestions?
 
Last edited:

maxl

macrumors newbie
Feb 2, 2013
7
0
I did change something when I switched to Mavericks but I think it was just to use the notification center instead of growl. Does your SSID have a space or other special character in it?

And about the script running twice, I have a fix for that. I can post it later tonight. It is basically just an if statement with a delay but I can't remember exactly how I implemented it. When I get home I'll have a look. Sorry I forgot to repond to your question earlier.
 

DeJung

macrumors newbie
Dec 17, 2013
3
0
No my SSID are just simple words like 'office' and 'home'.

If it's ok for you i can attach my personalized locationchanger and i'd beg you to have a quick look at it if i messed something up.

Could you provide me the code snippet for using Notification Center instead of Growl?
 

maxl

macrumors newbie
Feb 2, 2013
7
0
Sure, upload it and i'll have a look.

To use the notification center add the following line to locationchanger:

notify='/bin/terminal-notifier.app/Contents/MacOS/terminal-notifier'

Place the attached application in your /bin folder (where locationchanger is)
change the growl notify lines to look like this:

$notify -message "mounting Airport drives..." -title "Joined home network" -sender com.apple.Terminal

I didn't write terminal-notifier. It is from here: https://github.com/alloy/terminal-notifier

I don't seem to get the issue with it running the scripts twice anymore and I don't have the version with the delay in it either (got a new computer, must have missed it when i copied my files over.) Sorry.
 

Attachments

  • terminal-notifier.app.zip
    352.3 KB · Views: 248
Last edited:

DeJung

macrumors newbie
Dec 17, 2013
3
0
Thanks for helping Maxl!

Here's my personalized version of 'locationchanger'. Notification-Center support already implemented.

But it doesn't work at all - i change networks and nothing happens.

If you have any questions, feel free to contact me.

Regards Nic
 

Attachments

  • locationchanger.zip
    1.2 KB · Views: 216

liquefry

macrumors newbie
Oct 25, 2013
6
0
Hey, I've been using this script for a while, thanks :)

Since updating to Yosemite, the script is triggered every 1 minute or so, I guess the /Library/Preferences/SystemConfiguration is changing a lot based on more than just wifi network changes. Any ideas on another file to watch?

I use the notification centre rather than growl, it's an easy script command. Instead of the growl commands you can use:

osascript -e 'display notification "text of notification" with title "Title"'

Any thoughts on the trigger would be appreciated, I have had to turn off the notifications as they were getting annoying!
 

blovell

macrumors newbie
Jun 11, 2013
2
0
First, maxl, thank you so much for posting your instructions on how to do this. I've been using this information to auto mount my network drive for quite a while without a hitch.

That being said, I'm wondering if you could help me do the opposite of what's going on here. How would I go about automatically unmounting the network drive when it detects that it is no longer on my home wifi network?

Thanks for any help you can give. I'm learning stuff as I go.
 

alexk403

macrumors member
Jul 24, 2012
59
1
Can't get working on Yosemite

I am trying to get this to work on Yosemite. I know my applescript is in the right place and is working correctly, and the command I put in the shell script works. The problem is in the shell script. I have put the echo commands in each case as directed to figure out which case it is choosing, but no debug file appears on the desktop, so I guess it is not choosing any of them? If I put an echo debug command before the case $SSID line then i get a file, normal with the same thing printed several times. Any ideas? Thanks in advance.

BTW, the only thing I need to do after editing my locationchanger script file is replace the current one in the /bin folder, then unload and load the plist in terminal correct? I am using a modified version of the original, not my own.

Code:
#!/bin/bash

# automatically mount network volumes on network change

# redirect all IO to /dev/null
exec 1>/dev/null 2>/dev/null

# wait 2 seconds for connection to be established
sleep 5

# get various system information
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\ | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' ‘`

# SSID will not include spaces so leave them out when entering the name of your network
case $SSID in 
	
	# Home network SSID
	‘Alex’sNetwork’ )
	echo -e "case 1" >> ~/Desktop/debug.txt
	# do whatever you want to do. I am mounting network drives
	#change the path to your applescripts
	osascript ~/prog/AppleScript/mountVolume.scpt
	osascript -e ‘display notification “Your media can now be accessed.” with title “Entertainment Drive Mounted”’
	
	;;

	# Network 2
	'network2' )
	echo -e "case 2” >> ~/Desktop/debug.txt
	# do whatever you want to do.
	osascript -e ‘display notification “unknown network 2.” with title “Entertainment Drive Mounted”’

	;;

	# any unspecified network
	* )
	echo -e "case 3” >> ~/Desktop/debug.txt
	osascript -e ‘display notification “unknown network.” with title “Entertainment Drive Mounted”’
	# do whatever you want

esac

exit 0
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.