macotron.disks
List the volumes that Finder shows, with their size and free space, and eject a disk.
// Put an eject item in the menu bar for each external disk.
const menu = macotron.disks.list()
.filter((v) => v.ejectable)
.map((v) => ({
title: "Eject " + v.name,
onClick: async () => {
const r = await macotron.disks.eject(v.path);
if (!r.ok) macotron.notify.toast("Could not eject " + v.name, r.error, { color: "error" });
},
}));
macotron.menubar.status("eject", { sfSymbol: "eject.fill", menu });
Listing
list(): Array<{ name, path, device, fsType, total, free, available, internal, removable, ejectable, network, readOnly, root }>
The mounted volumes that Finder shows. System volumes that Finder hides, such as /System/Volumes/Data, are not in the list. The startup disk is the row with root: true and path: "/".
nameis the name Finder shows, for example"Macintosh HD".pathis the mount point, for example"/Volumes/Backup".deviceis the BSD name, for example"disk3s1s1". For a network share it is the server path.fsTypeis the file system, for example"apfs","exfat", or"smbfs".total,free, andavailableare bytes.freeis the space the file system can give now.availablealso counts purgeable space, such as caches and local snapshots, that macOS removes when it needs room. Finder showsavailable.internal,removable, andejectabledescribe the device. They arefalsefor a network share.networkistruefor a server volume.readOnlyistruewhen the volume is mounted read-only. The startup volume is read-only because macOS seals it.
The call does not wait for a server. Macotron reads the sizes of a network share from the kernel's last reading, so they can be out of date, and the call cannot stop when a server is gone.
Ejecting
eject(path): Promise<{ ok, error? }>
Unmounts every volume on the disk that holds path, then ejects the disk. When an app keeps a file open on the volume, ok is false and error tells why.
See also
- macotron.fs to read and write files.
- The
eject.jsanddisk-usage.jsplugins.