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

MacBook Pro

Posted on Sep 3, 2019 9:37 AM

Reply
Question marked as Top-ranking reply

Posted on Sep 3, 2019 11:41 PM

It's almost certainly APFS, which massively complicates dealing with disks.


However, from your description of what you're trying to do, I also think your script is massively overcomplicated, too.


For example, to unmount all ejectable volumes, consider that your script could simply say:


tell application "Finder"

eject (every disk whose ejectable = true)

end tell


I'm not so sure about logic behind the 'remount', though - the OS will usually automatically mount volumes when they're attached, so how often do you need to remount them?

Similar questions

4 replies
Question marked as Top-ranking reply

Sep 3, 2019 11:41 PM in response to revDAVE

It's almost certainly APFS, which massively complicates dealing with disks.


However, from your description of what you're trying to do, I also think your script is massively overcomplicated, too.


For example, to unmount all ejectable volumes, consider that your script could simply say:


tell application "Finder"

eject (every disk whose ejectable = true)

end tell


I'm not so sure about logic behind the 'remount', though - the OS will usually automatically mount volumes when they're attached, so how often do you need to remount them?

Sep 5, 2019 9:33 AM in response to revDAVE

However later I may want to remount same drives that were still attached from before.


From before what?


As I said in my earlier post, the OS will generally mount any drive when it's attached, so I'm unsure as to when you'd need this. Given your original example of a laptop user wanting to disconnect drives before heading out, those drives would automatically remount when you return and reconnect the drives.


Otherwise, to do what you ask you need to somehow maintain state - i.e. keep track of which disks were unmounted and then remount then - unless you want to mount any disks whether they were previously mounted or not - which seems to be what the original script does.


If you do want to mount all available disks, just:


do shell script "mount -a"



Sep 6, 2019 8:17 AM in response to Camelot

Hi Camelot,


Sorry if it was not more descriptive earlier. I have a few different related scenarios:


- Sometimes I want to eject all external drives before traveling. In this case yes I would be disconnecting the cables. Then later when I return, I would reconnected the cables and they would auto mount at that time, as you mentioned.


- Some other times I just want to dismount all external drives, however they would all stay attached with cables, and then later it would be nice to have an applescript able to remount them all.


And yes - also just mount all available disks - without tracking previous would be fine.


To that end –I tried your shell script:



do shell script "mount -a"


- however I got an error - I think related to APFS:


error "mount_apfs: mount: Operation not permitted" number 1


- if so, is there a way to just mount the externals which are non afps drives?


Thanks a lot for your help,

Dave

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Help needed with older Applescript to mount - unmount external drives

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.