Help Needed With Older AppleScript to Mount / Unmount External Drives

Hi all,

I found this older AppleScript on the Internet somewhere a while ago - can't remember where.

By just running the script, it will either mount or unmount all external drives. This is great for laptops on the go when you want to make sure to unmount all externals before you travel.

The script has run fine for years, however now I am on Mohave, and it shows a problem when trying to unmount. I am thinking this may have something to do with APFS, but not sure.

When trying to unmount it throws a dialogue window saying that something could not be unmounted, (possibly APFS related), and then it continues unmounting. BTW: even though it says "re-mount" it actually continues to unmount as you would want...

Q: I am wondering if anybody can help update this script so that it will not have a problem when attempting to unmount external drives?


set alldisks to paragraphs of (do shell script ¬
	"diskutil list|grep -v 'EFI EFI'|grep -v 'Recovery HD'|grep -o 'disk[0-9][a-z][0-9]*'")
set all_non_boot_disks to paragraphs of (do shell script "df -hlg | awk -F/ '/disk*/ {print $5}'")
set nonbootnumber to (count of alldisks)
if (count of all_non_boot_disks) > 0 then
	try
		set all_non_boot_disks to items 2 thru nonbootnumber of alldisks
	on error
		set all_non_boot_disks to {}
	end try
	activate
	repeat with the_Item in alldisks
		try
			set the_ID to (do shell script "df -hlg | grep -m 1" & space & ¬
				quoted form of the_Item & space & "| grep -o 'disk[0-9][a-z][0-9]*'")
			do shell script "diskutil unmount" & space & the_ID
		on error the error_message number the error_number
			if the error_message does not contain ":" then
			else
				if button returned of (display dialog "A disk couldn't be unmounted.
                                        Would you like to re-mount those already unmounted?" buttons ¬
					{"Cancel", "Re-mount"} default button 2) is "Re-mount" then
					my mountAll(alldisks)
				end if
			end if
		end try
	end repeat
else
	my mountAll(alldisks)
end if

on mountAll(alldisks)
	set actiondisks to {}
	repeat with i in alldisks
		if i is not in actiondisks then
			set actiondisks to actiondisks & i
		end if
	end repeat
	repeat with myitem in actiondisks
		try
			do shell script "diskutil mount" & space & myitem
		end try
	end repeat
end mountAll

You should not need a mounting script.

Here's my unmount script -- works well in Mojave:

tell application "Finder"
  
  set diskList to every disk whose ejectable is true
  
  if (diskList ≠ {}) then
    
    repeat with oDisk in diskList
      
      set diskName to name of oDisk
      display notification "Now Ejecting Disk: " & diskName
      eject diskName
      
    end repeat
    
  else
    display notification "NO Disks Found to Eject"
  end if
  
end tell

I have this saved from somewhere:

tell application "Finder" to eject (every disk whose ejectable is true)'

and I do seem to recall that it got hung up on… APFS snapshots, I think? I ended up writing my own

diskutil list -plist \
| awk '/MountPoint/{getline; print}'  \
| egrep -v '<string>/</string>|<string>/private/var/vm</string>' \
| sed 's#.*<string>##g ; s#</string>##g' \
| while read line
do

diskutil unmount "$line"

done

Then I had to add exclusions for com.apple.TimeMachine.localsnapshots and firmwaresyncd. and Preboot and Recovery and came up with this:

diskutil list -plist \
| awk '/MountPoint/{getline; print}'  \
| egrep -v '<string>/</string>|(<string>/private/var/vm|<string>com.apple.TimeMachine.localsnapshots|<string>firmwaresyncd.|<string>Preboot|<string>Recovery)' \
| sed 's#.*<string>##g ; s#</string>##g' \
| while read line
do
	diskutil unmount "$line"
done

For what it's worth, you could use this with Keyboard Maestro on a sleep macro.