I made a script that connects to a network drive when iTunes is running. You can download it from this gist page (look for the "raw" download link).
#!/bin/sh
HOST="hostname" # AFP server, e.g. server.local or 192.168.1.94 or…
USER="your_user_name" # for connection to AFP server
PASS="your_password" # for connection to AFP server
VOLUMENAME="your_volume_name" # name of volume to be mounted, typically username on AFP server.
while :
do
if [[ $(ps axo pid,command | grep "/Applications/[i]Tunes.app/Contents/MacOS/iTunes ") ]]; then
if [ ! -d "/Volumes/${VOLUMENAME}" ]; then # iTunes runs, Server not mounted
echo "iTunes runs, now mounting ${VOLUMENAME}..."
mkdir "/Volumes/${VOLUMENAME}"
mount_afp afp://${USER}:${PASS}@${HOST}/${VOLUMENAME} "/Volumes/${VOLUMENAME}"
fi
fi
sleep 2
done # start over
Basically, it runs in an infinite loop and checks every two seconds if iTunes.app is running. If so, it checks whether your AFP volume is mounted already, and does so if not.
You’ll have to configure the script before running it for the first time, see the variables on top of the script. VOLUMENAME is the name from the normal AFP connection dialog (ssd in this case):

To check if the thing works in principle: Let’s say the script is on the Desktop
, in Terminal type cd ~/Desktop/
, then ./mount_afp_when_itunes_running.sh
.
In case of Permission denied
, set the file permissions so that you can run the script with chmod 755 mount_afp_when_itunes_running.sh
, then try again.
Finally, if everything works, you could then launch this script automatically via a Launch Agent.