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