# Ejecting all network drives via Applescript/Automator/Shell?

I use the following script to attempt to eject all disks that aren't internal:

tell application "Finder"
eject (disks whose ejectable is true)
end tell


This has the advantage of skipping volumes like MobileBackups as well as several others.

Unfortunately, it doesn't eject network shares.

Is there any way to get connected network shares in applescript?

As a +1, ideally this would skip things like optical disks as well.

EDIT: Both answers so far have shared ways to eject specific network disks. This, of course, is easy and I know how to do it. What I'm specifically asking for is a way to attempt to eject every disk other than system internal disks, regardless of their name.

• Interestingly, I've been using something almost identical to this to eject my external drives, and it has worked like a charm until after the last time I restarted. Now I get a popup that says, The Disk "32-digit UUID" wasn't ejected because one or more programs may be using it. but I can't figure out what that UUID corresponds to. It is not listed in diskutil info -all for example. Also nothing is listed in lsof. This is on OSX High Sierra. Any suggestions? – Victor Engel Dec 3 '18 at 15:45
• It turns out the UUID was from Clusters (the background compression tool). It doesn't actually appear as a drive anywhere that I can see, but I finally found it in lsof. – Victor Engel Dec 3 '18 at 21:42

I use Automator. I inserted 'Get Specified Finder Items', and named the target disks that I wanted to 'Get'. Only those named get chosen, but network drives do work. I then pipe this to 'Eject Disk'.

That's it.

Easily done by using the -t option of the umount command. From the man page which you can read using man umount:

-t type
Is used to indicate the actions should only be taken on filesys-
tems of the specified type.  More than one type may be specified
in a comma separated list.  The list of filesystem types can be
prefixed with no'' to specify the filesystem types for which
action should not be taken.  For example, the umount command:

umount -a -t nfs,hfs


So,

do shell script "/sbin/umount -a -t nfs,smbfs"


should umount all NFS and Windows/Samba shares mounted.

EDIT: You may also want to use the -f option to force the action. Read the man page for details.

EDIT2: Apologies, umount isn't behaving as advertised, at least on my Snow Leopard. So I'd do it like this remembering to change msdos to your desired filesystem type:

set mounts to {}
set mounts to paragraphs of (do shell script "mount | grep msdos | cut -d ' ' -f3")

# Umount non busy filesystems
repeat with mount in mounts
set pid to (do shell script "fuser -c " & mount)
if pid is equal to "" then
# We use diskutil since classic unix umount cmd needs sudo/password
do shell script "diskutil umount " & mount
end if
end repeat


HTH

• How can I find out what type of filesystem a disk was mounted with? – Tim Visher May 13 '14 at 12:42
• The mount command will display all the mounted filesystems. In the output, the first word after the bracket is the filesystem type. – Vic May 15 '14 at 8:35

Or you could just call the shell command..

do shell script "/usr/sbin/diskutil unmount /Volumes/some_network_share"


Note that this might fail if you have open files so you can add force as such

do shell script "/usr/sbin/diskutil unmount force /Volumes/some_network_share"


The command line utility /sbin/umount may be another solution to what you're looking for. The Mac OS keeps an alias to each of your mounted disks in a folder /Volumes. You can use this to your advantage and amount a drive listed there:

$umount /Volumes/Video  So you might want to create a shell script that lists the contents of /Volumes and attempts to eject them all, like this one: for var in ls /Volumes ; do echo Ejecting /Volumes/$var
umount /Volumes/$var done  It will, however, fail to eject drives that cannot be ejected, like the current startup disk. This will only eject all network volumes: $ find /Volumes -maxdepth 1 -not -user root -print0 | xargs -0 umount


If you want an AppleScript, I adapted the script posted at: Shortcut to eject all external hard drives but not MobileBackups

to this, and it works great. I've placed the applet for this in my dock to use right before I unplug.

set exceptionsList to {"MobileBackups", "startup disk", "home", "net"}
tell application "Finder"
set diskList to the disks
repeat with mountedDisk in diskList
if name of mountedDisk is not in exceptionsList then
eject mountedDisk
end if
end repeat
end tell