Monthly Archives: June 2010

Mac CLI Hacks

or: Just for the shell of it

These are probably not of any interest unless you’re the odd sort of mutant who is simultaneously a Mac fan yet relishes the power of the Bourne shell and its relatives. But since I am, and have a number of friends with similar tendencies, I thought I’d share.

First off, I have an external CD/DVD drive for ripping media, just because it tends to be both faster and less finicky than the MacBook Pro’s internal drive. Unlike the built-in slot-loading drive, it has a tray, and some combination of its design and the way it’s sitting on my desk is such that when the tray is open, the button is pretty much impossible to reach. So while it’s easy to open the closed drive, it’s not quite so easy to close it when open, regardless of whether or not it has a disc in the tray.

Yes, I know I could just shove the tray shut. But doing so’s always made me feel vaguely Neanderthal. I also know that It’s possible to use the built-in Disk Utility to this end, and that’s what I’ve done in the past. However, that means firing up Disk Utility, waiting a second or two while it surveys the attached-volume landscape, selecting the volume of interest from the list, and finally clicking the close button. A few more steps than I’d like.

Fortunately, as with most minor annoyances, I’m not the first one to encounter it. One David Morse came up with a solution to more-or-less the same problem. However, his solution actually involves more work, and is more configuration-specific, than necessary. The current version of drutil supports “Drive Selection Criteria”, meaning that if your target drive has a unique contribution of attributes, you can select it without having to obtain and then grep a device-list first. In my case, the drive I care about is the only external CD/DVD drive, so specifying “external” suffices.

function close () {
    drutil -drive external tray close
}

Still on my personal To-Do list: find a way to make this work within OnMyCommand, so that I can invoke it from within the Finder itself. Still, for now the one-liner is good enough.

Moving on, the Mac OS Finder lets you toggle the visibility of a file’s extension in various ways. (Cleverly, as is typical — if you try to rename a file within the Finder so as to remove its extension, the Finder simply hides the extension instead, so as to refrain from needlessly altering the system’s understanding of the file’s type.)

You can of course also toggle extension visibility using the Get Info/Show Inspector Options. Even so, there are times when you want to tweak the extension-visibility of a slew of items at once, and selecting the set from the Finder under such circumstances can be more than a bit exasperating. Fortunately, there’s a command-line tool that lets you manipulate a file’s extension-visibility, among other things: SetFile. This leads to two functions, one to hide an extension, the other to show it:

function hidex () {
    SetFile -a E "$@"
}
function showex () {
    SetFile -a e "$@"
}

("$@", complete with quotes, is your friend. Really. Failure to use it, particularly when attempting to manipulate files which have spaces in their names, is known to the State of California to cause cancer and reproductive harm.)