I'm trying to do something I thought would be simple, but after researching it I feel like I'm half way towards shaving a yak, so I'm asking for advice. I want to create a script that lets me and my colleagues do normal spolight searches on a shared drive, which from research seems to not be easy and is best solved with a script that:

The bolded parts are the parts I'm having trouble with.

Particular problems I'm having:

  • I've found an AppleScript way to mount a drive if it's not already mounted - check name of every disk then mount volume "path://to/volume" - but it doesn't allow me to specify the name, and I'm cautious about how consistent the Mac will auto-name a path on a non-Mac shared drive. I want to set the volume name so I can refer to it when telling Spotlight which volume to index, which seems to be impossible in AppleScript?
  • I've found a shell script way to mount a drive to a specific volume, but I read (can't find the source now, am looking for it) that the volume has to already exist, which confuses me (do I need to create an empty volume? I don't quite understand what an empty volume would be); and I can't see how to check that it hasn't already auto-mounted.

My level of knowledge of Mac volumes etc is quite low, so I'm aware I may have misunderstood any of the above.


3 Answers 3


OS X follows these steps when mounting a drive: It creates a raw device in /dev/ (for local disks only), it creates a folder in /Volumes/, then it mounts the drive to that folder it just created. That folder creation step is probably what you read. The shell script you linked is missing the following command:

mkdir /Volumes/somenetworkdir

Then the command you linked will work, with the proper parameters

mount -t smbfs -o username=RemoteUser //REMOTEHOST/directory /Volumes/somenetworkdir
  • So can I check if the volume already exists by checking if the directory already exists in Volumes? Trusting that when it is not mounted it'll be removed? Or is it not that simple? Commented Jul 6, 2015 at 18:05
  • OS X will remove the directory when it unmounts the drive, yes. The only time where it may not is if the system shuts down improperly, preventing it from removing the directory as normal. Commented Jul 6, 2015 at 18:06
  • you can try running "rmdir /Volumes/somenetworkdir" first if you want to be certain the directory isn't there when the script runs, although check first so it doesn't fail! Or just check for the existence of the directory before running mkdir. That's probably ideal. Commented Jul 6, 2015 at 18:08
set mountedVolumes to do shell script "ls /Volumes/"

if (mountedVolumes contains "thisDriveName") is false then

        mount volume "afp://thisUsername:thisPassword@thisIPorHostname/thisDriveName"

end if

You can check if a volume is mounted by checking the output of the mountcommand:

mount | grep '/Volumes/SomeVolumeName'

And you can mount it with open smb://.... So your script could look something like this



# ...

mount | grep -q "/Volumes/$share " || open smb://$user:$pass@$server/$share

# ...

The open smb://... command will mount it under the name of the share.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .