My current main volume (where the OS is installed) is called Macintosh HD, but in the future if I change that name, I want my macros to still work wherever I'm using the volume's name.
Is there a way to get the name of that volume so I can save it to a variable?
I could save the name to a global variable, but I would rather have KM set this automatically for the local variables I use.
diskutil info / | grep Volume\ Name
Volume Name: UltraSSD
Edit: To clarify, the basic diskutil command will spit out a ton of stuff, but I only want the volume name. So I pass the output to grep to get just that line. But that's still too much…
So then I use the pipe (|) again to pass that output to the cut command, and the -c option on cut means to cut based on columns. The name of the drive starts in column 31, and I just put 9999 to make sure I capture all the text from 31 to the end of the name.
But I do know I started using it after I changed the name of my start up disk and some of my macros stopped working properly. I've now changed the name two more times with no further issues, so it definitely works for me!
I was a bit surprised as well. Both incur the overhead of launching an engine, but it seems the AppleScript text parser is a bit quicker. I think this is probably due to the time required to run and grep through the diskutil output, which is substantial. I don't know of another spot to see the boot drive name, though.
This method depends on the text output of diskutil info being printed in a particular way in order for the correct portion of text to be captured. A safer way to process the output of diskutil info is to have the information provided as an XML-formatted property list:
diskutil info -plist /
This can be piped through to the plutil command, which is the macOS command-line utility for processing property list and JSON data. Among its other capabilities, it can parse the input as key-value entries, allowing values to be extracted by specifying its associated keypath. The VolumeName key is top-level, which makes this easy:
diskutil info -plist / |
plutil -extract VolumeName raw - -o -
I'd personally opt for the AppleScript method, for much the same reason as above: it retrieves the information stored in an intrinsically-valued AppleScript constant that will always correctly point to the boot volume, and the only text manipulation needed is to strip the trailing path delimiter that is firmly defined. It's therefore robust, and apparently more performant (possibly because the aforementioned constant gets created as part of the AppleScript environment upon instantiation, so its value is already available, and doesn't need grepping or cutting).