How to mount / unmount external drives in Monterey?

Hi all,

I am able to unmount an external drive like this:

try
do shell script “diskutil unmount /Volumes/MyDrive/”
end try

  • but after unmount - the opposite (while still connected) does not work :

try
do shell script “diskutil mount /Volumes/MyDrive/”
end try

in terminal I get this error:
this says - Unable to find disk for /Volumes/MyDrive1/

How can I get the drive to mount again after unmount?:

Model: MacBook
AppleScript: 2.8
Browser: Safari 605.1.15
Operating System: macOS 12

Mounting the volume this way doesn’t work because the (unmounted) volume doesn’t exist at the given path.
You need the device node.

There is an expression which works since the first version of OS X

do shell script "diskutil mount `diskutil list | awk '/ MyDrive / {print $NF}'`"

Please note the different quotations »"«,»'« and »`«. The differences are crucial.

Thanks Very much for the info.

I have a very similar question. I made some typos when I posted last night.

What is the syntax for mounting a partition named Data 6 (with a space between Data and 6)? For example,

set myDirive to “Data 6”
do shell script “diskutil mount diskutil list | awk '/ MyDrive / {print $NF}'

gave a Script Error when I ran it. I just updated from Mojave to Big Sur. In Mojave, I had a similar script that mounted a group of external drives, but it no longer works in Big Sur. I based the above example using the suggestion here. If I could understand why this doesn’t work, I might be able to fix my longer script.

This cannot work because myDirive is not equal to MyDrive and apart from the (double) typo you are going to mount a volume with name MyDrive.

Try this instead

set myDrive to "Data 6"
do shell script "diskutil mount `diskutil list | awk '/ " & myDrive & " / {print $NF}'`"

Thanks, Stefan. I appreciate the reply.

I copied and pasted your two lines but I still get the script error:

Usage: diskutil mount [readOnly] [nobrowse] [-mountOptions Opt[,Opt]*]
[-mountPoint Path] DiskIdentifier|DeviceNode
Mount the volume in the “standard” place (/Volumes), unless an optional
“custom” mount point is specified.

In case this helps, from Terminal, I ran diskutil list and among my results are

4: Apple_HFS ⁨Data 6⁩ 1.0 TB disk4s4
and
6: APFS Volume ⁨Data 7⁩ 92.4 GB disk8s6

I tried your script with both Data 6 and Data 7 but received the same error message.

So… , now that I have run “diskutil list”, I tried your script again, but using “Apple_HFS Data 6” and then “APFS Volume Data 7”, and both worked!

This was NOT required in Mojave. Also, using the long names like “Apple_HFS ⁨Data 6” will make it much more complicated to modify my own script since that script also requires the short names. Is there not a way to mount just using the short names?

My bad, remove the space characters between the slashes and the double quotes

set myDrive to "Data 6"
do shell script "diskutil mount `diskutil list | awk '/" & myDrive & "/ {print $NF}'`"

Stefan,

I am getting used to a new keyboard so I know all about typing errors, as I initially made quite a few.

I tried the revised script and I got an interesting result.

First, I tried it as posted, with data 6, which does not have an APFS container. And… it worked! (hurray!)

Then I tried it with data 7, which is in an APFS container. And … I got the usual error message:

	--> e[b]rror[/b] "Usage:   diskutil mount [readOnly] [nobrowse] [-mountOptions Opt[,Opt]*]
     [-mountPoint Path] DiskIdentifier|DeviceNode

Then I tried it with two other partitions in that APFS container. Their names are MWF 7 and Parallels 7. Both worked!

I’ll try renaming and otherwise fooling around with Data 7 to identify the problem

I am using the script from this thread inside of a loop to mount disks. The disks are grabbed from a list. I have been using this script since, maybe, Mountain Lion, so nothing has changed since I upgraded my computer to Big Sur. But…

7 of the disks always successfully mount, 4 of them never mount (using the script).

I can find no attribute that distinguishes those that mount from those that don’t.

3 of the ones that don’t mount are APFS Sys volumes and the other is an APFS physical volume.

1 of the successesful ones is an APFS Sys volume, 4 are APFS physical volumes, and 2 are USB External volumes.

I also looked at “owners”, but some of the successful mounts have them enabled, others have them disabled. Same for the unsuccessful ones: some enabled, some disabled.

Is there anything else I can do to troubleshoot this?

Postscript. I just replaced the unsuccessful devices in the list with their device node names: disk6s3, disk6s2, disk6s1, and disk 9s4.

Three of these four now mount along with the others, but the 4th one, TTSS 8 = disk6s1, still won’t mount.

In Terminal, diskutil mount TTSS\ 8 also fails to mount

but, strangely, diskutil mount disk6s1 succeeds (but not the AppleScript version.)

Thanks for all this good info.

I know this is an older thread, but I found it when looking for a script to eject/mount external drives. Learned a few things so I thought I’d share the solution I ended up using. Somewhere here it suggests to run “diskutil list” in Terminal to get the disk identifiers. Which I did. The reason is that although you can eject external disks by just using their name, see below, getting them mounted again tends to be an issue. Not so once you have the disk identifiers. I was having an issue trying to mount the SuperDuper! Clone drive as it also has a SuperDuper! Clone - Data volume associated with it. But, using the disk identifier for just the SuperDuper! Clone drive ends up mounting both the SuperDuper! Clone and the SuperDuper! Clone - Data volumes. Takes a few seconds, but both get mounted.

set volumes_ to {"MacBook Pro Local Backups", "CCC MacBook Pro Local", "Time Machine", "SuperDuper! Clone"} -- volume names
tell application "Finder"
	repeat with vol_ in volumes_
		eject disk vol_
	end repeat
end tell
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

do shell script "diskutil mount disk5s1" -- CCC MacBook Pro Local
do shell script "diskutil mount disk5s2" -- MacBook Pro Local Backups
do shell script "diskutil mount disk7s1" -- Time Machine
do shell script "diskutil mount disk9s1" -- SuperDuper! Clone

Possibly there’s an easier/cleaner way to do this, but it works for me.

The disk nodes aren’t necessarily persistent across rebooting the machine.

In post #7 I described a way to mount a volume by name. Just use your repeat loop (without the Finder tell block) and replace the eject line with the do shell script line and myDrive with vol_

set volumes_ to {"MacBook Pro Local Backups", "CCC MacBook Pro Local", "Time Machine", "SuperDuper! Clone"} -- volume names
repeat with vol_ in volumes_
	do shell script "diskutil mount `diskutil list | awk '/" & vol_ & "/ {print $NF}'`"
end repeat

I had absolutely no idea that this was the case. Thank you for pointing it out, and for the script with a solution.

I get an error when trying to mount the SuperDuper! Clone volume when using your script (posted below).

set volumes_ to {"SuperDuper! Clone"} -- volume names
repeat with vol_ in volumes_
	do shell script "diskutil mount `diskutil list | awk '/" & vol_ & "/ {print $NF}'`"
end repeat

It ejects just fine with the following script.

set volumes_ to {"SuperDuper! Clone"} -- volume names
tell application "Finder"
	repeat with vol_ in volumes_
		eject disk vol_
	end repeat
end tell

I’ve included screenshots of the error window and the error location in the script.
Error

Error Location

It’s caused by the exclamation mark in the name. I recommend to rename the volume by deleting the character.

You are of course correct. I must have looked at the error message for at least 10 minutes, and after that time, it still meant nothing to me. I used Disk Utility to erase the original clone, assigned a new name, without the “!”, modified the scripts, and now both the eject and mount scripts work perfectly. Thank you!

I suspect that what I’m seeing is just the MacOS system doing what its meant to do, but thought I’d ask. The below script does the eject of all mounted drives.

set volumes_ to {"Mac Mini Local Backups", "CCC Mac Mini Local", "Time Machine", "SuperDuper Clone"} -- volume names
tell application "Finder"
	repeat with vol_ in volumes_
		eject disk vol_
	end repeat
end tell

But when used I get a window that pops up asking if I want to eject both or just one. I need to always eject both. Is there any way to incorporate this into the script itself? Mac Mini Local Backups and CCC Mac Mini are both on one 500GB SSD.
Screenshot

Try this…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

local tid, vol_, volumes_, aDisk
set tid to text item delimiters
set text item delimiters to "s"
set volumes_ to {"Mac Mini Local Backups", "CCC Mac Mini Local", "Time Machine", "SuperDuper Clone"} -- volume names
repeat with vol_ in volumes_
	set aDisk to paragraph 1 of (do shell script "diskutil list | awk '/" & vol_ & "/ {print $NF}'")
	set aDisk to (text items 1 thru -2 of aDisk) as text
	do shell script "diskutil unmountDisk " & aDisk
end repeat
set text item delimiters to tid

Robert, that works. Takes a bit of time. The first two drives to get ejected are Mac Mini Local Backups and CCC Mac MIni Local, which takes about 11 to 12 seconds. Then you get the beach ball thing spinning and it takes a total of just under 33 seconds to eject Time Machine and SuperDuper Clone, which I can live with. Thanks for the solution.

Screenshot

Here is a fancier version

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property physicalDisks : {}
property synthesizedDisks : {}

local tid, diskList, vol_, volumes_, aDisk, d
set tid to text item delimiters
set text item delimiters to "/dev/disk"
set diskList to rest of text items of (do shell script "diskutil list")
set text item delimiters to "s"
repeat with d in diskList
	set d to paragraphs of contents of d
	if item 1 of d contains "physical):" then
		set end of physicalDisks to {trimWhitespace(text 7 thru 32 of item 5 of d), trimWhitespace(text 34 thru 56 of item 5 of d), text 69 thru -1 of item 3 of d}
	else if item 1 of d contains "synthesized):" then
		set end of synthesizedDisks to {(text items 1 thru -2 of (text 49 thru -1 of item 4 of d)) as text, trimWhitespace(text 34 thru 56 of item 5 of d)}
	end if
end repeat
set volumes_ to {"Extra HD", "G-Speed"} -- volume names
repeat with vol_ in volumes_
	set vol_ to contents of vol_
	set flag to false
	repeat with i from 1 to count synthesizedDisks -- search for APFS+ disk
		if (item 2 of item i of synthesizedDisks) = vol_ then
			set vol_ to item 1 of item i of synthesizedDisks
			set flag to true
			exit repeat
		end if
	end repeat
	if not flag then -- not an APFS disk
		set flag to false
		repeat with j from 1 to count physicalDisks -- search for HFS+ disk
			if (item 2 of item j of physicalDisks) = vol_ then
				set vol_ to item 3 of item j of physicalDisks
				set flag to true
				exit repeat
			end if
		end repeat
	end if
	if flag then do shell script "diskutil unmountDisk " & vol_
end repeat
set text item delimiters to tid

on trimWhitespace(aString)
	local s, e, ls, ws
	set ws to "  " & tab & linefeed & return
	set ls to length of aString
	set e to ls
	set s to 1
	repeat while s ≤ ls
		if text s of aString is not in ws then exit repeat
		set s to s + 1
	end repeat
	repeat while e ≥ s
		if text e of aString is not in ws then exit repeat
		set e to e - 1
	end repeat
	if e - s < 0 then
		return ""
	else
		return text s thru e of aString
	end if
end trimWhitespace