iTranslated by AI
macOS Shortcut to mount specific external storage
Overview
In environments where a Mac is used with external storage (USB, Thunderbolt, etc.), there was a need to mount the storage quickly when needed and unmount it appropriately otherwise. Unmounting isn't much of a hassle since you can just "Eject" in Finder, but mounting requires a slightly more tedious method.
Using Disk Utility is a common way to mount storage, but operating the GUI every time is a chore. Therefore, I decided to automate the mounting process using Shortcuts.
Core Process
The core is a Shell Script, and the approach is to execute this through a Shortcut. As an interface, by pinning the Shortcut to the menu bar, you can mount the target storage at any time in about two clicks.
The script is as follows:
volname="StorageName"
device=`diskutil list | grep $volname | sed -r "s/.*(disk[0-9]+s[0-9])$/\\1/"`
for dev in `echo $device`
do
diskutil mount /dev/"$dev"
done
This script uses the diskutil list command to identify the target device name from the specified volume name and performs the mount with the diskutil mount command.
It assumes the path will be in a format like /dev/disk1s1.
Please specify the volume name you want to mount (the name of the storage as seen in Finder) in StorageName.
Implementing as a Shortcut
Create a new shortcut in the Shortcuts app, add a "Run Shell Script" action, and paste the script above.
By enabling "Pin in Menu Bar" in the details settings, it becomes executable from the menu bar.

I also added a "Show Notification" action to display the results because I wanted feedback.

By setting up shortcuts in this way, you can individually mount multiple external storage devices.
In Case of SMB
When you want to mount a file share from another Mac or a shared disk from a Windows PC locally.
Basically, you can mount the specified volume using the open command. Simply use the script as follows:
open "smb://USER:PASS@COMPUTER/VOLUME"
- USER… SMB share username
- PASS… SMB share password
- COMPUTER… SMB share computer name or IP address
- VOLUME… SMB share target share point
However, this method is not very secure because the username and password are included in plain text within the command.
Discussion