I think I can get you started, but using terminal commands, which you said you'd prefer to avoid. Apologies, but it might give you a starting point. All of the following you could put in a bash script and run as a login item.
You'll need to first share out the target drive(s) (MyBook
in the examples below) using file sharing (System Preferences > Sharing > check the File Sharing box, add the drives you need and set appropriate permissions - I just use Everyone to keep it simple).
You can mount a remote machine and/or its associated drives using mount like so:
Make a directory as the mount point (obviously call test
whatever you want):
mkdir /Volumes/test
Now mount the remote drive. I'm assuming this is already connected to your iMac and appears when you connect to your iMac using Finder:
mount -t afp afp://<your mac's name>/<drive name to mount> /Volumes/test
On my remote Mac Mini, to connect to my MyBook attached to it via USB, that would be:
mount -t afp afp://bobs-mac-mini/MyBook /Volumes/test
If you need to authenticate (I got error -5000
when trying to mount my home folder), you can also do this using
mount -t afp afp://<username>:<password>@<your mac's name>/<drive name to mount> /Volumes/test
However, the password would unfortunately have to be in the clear. So again, this might be:
mount -t afp afp://binarybob:password123@bobs-mac-mini/MyBook /Volumes/test
You can also connect to your home folder using the above method, just by replacing MyBook
with the name of your home folder.
The drive you mounted should now appear in the finder and you should be able to use it like any other locally mounted drive. When you're finished, you can do:
umount /Volumes/test
to remove it. BTW, if you're not an administrator, you might need to add sudo
in front of each command and type an administrator password.
I have a similar situation, and for me it simply works if I drag the network folder in finder with Cmd + Alt held down to create an alias. If the network share is not mounted, it will automatically mount when the link is accessed either by Finder or with most native GUI applications (e.g., my iTunes folder is linked this way and starting iTunes will mount the share).
However, if you meant a symlink instead of an alias, you can certainly create the link (e.g., on the command line with ln -s /Volumes/dest /path/to/target
) but it will not cause the automount to occur, but rather the directory will just seem to not exist if accessed without the share mounted.
Best Answer
Yes, a script can be triggered when the iPhone is connected...
When the iPhone is connected to the Mac, the following file gets modified:
Using a Launch Agent with a
WatchPaths
key it can be determined if the iPhone is connected to the Mac when the NetworkInterfaces.plist file is modified.For testing purposes, I created the launch agent as:
Contents of the com.me.iphone.connected.plist file:
In the XML PLIST file, /usr/local/bin/iPhone is a shell script that gets executed when there is a change to the NetworkInterfaces.plist file and the contents of the shell script is:
When the example shell script is executed it assumes I'm already connected to the Network Share, in this example it's "Data", but it first sets the value of the
iPhone
variable by using thedefaults
command to read the.plist
file and pipes its output togrep
to return the first occurrence of "iPhone" if found. If the iPhone is connected, theniPhone="iPhone"
and the test in the firstif
statement succeeds.The next test succeeds if I am connected to Data, and the subsequent test succeeds if I'm not already connected to my iPhone Backup sparsebundle. It's coded in this manner so as not to try opening the sparsebundle if it's already mounted if the NetworkInterfaces.plist file is modified again while the sparsebundle is already mounted. In other words, the sparsebundle is only opened if it's not already mounted.
If you also want the mounting of the SMB share to occur in the, e.g.
iPhone
, shell script, you can add the following example command directly after the opening of the firstif
statement. In other words, literally between the first twoif
statements:This uses AppleScript code to mount the target share and assumes the credentials are already stored in your Keychain. It also is not problematic if the SMB share is already mounted, then the command just fails silently.
Notes:
Had you included the script or code you are already using, I would have created a working example that would have incorporated it, however what I've submitted is an example that works for me tested under macOS Catalina.
I recommend you read the manual pages for
launchctl
,launchd.plist
andlaunchd
. You can read the manual page forcommand
in Terminal by typingcommand
and then right-click on it and select: Open man PageAfter creating the
$HOME/Library/LaunchAgents/com.me.iphone.connected.plist
file I loaded it using thelaunchctl
command, e.g.:Once loaded, it will automatically load when you login to your account, so you shouldn't need to do it manually again under normal circumstances. To unload it, use the
unload
subcommand with thelaunchctl
command.After unloading it, you can use the
disable
subcommand so it doesn't load again or you can delete the .plist
file, if/when you want to undo changes you've made to your system.