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.)

6 thoughts on “Mac CLI Hacks

  1. OS X has a built-in Eject menubar widget that also opens & closes drive doors – double-click the following file to enable it:

    /System/Library/CoreServices/Menu Extras/Eject.menu

    (Command+drag the icon to change its menubar position or to remove it)

  2. Um, holy crap. You’ve just tipped me off to three immensely useful things (Menu Extras, Eject, and the Command-drag trick) in as many lines. Thanks, Josh!

    I’m now off to play around with the other items in Menu Extras.

    Incidentally, it’s great to hear from you, irrespective of the cool and handy info. How are you? We should catch up. A quick look at Twilight Edge suggests that you may have a few eye-opening things to tell me about Arduinos, too.

  3. I’d create a quicksilver trigger to run the script with a keystroke.

    I also just show extensions for all files (Finder Preferences).

  4. I am one of those sad cases who knows that Quicksilver holds an incredible reserve of power under its skin, but never really learned anything beyond Control-Space as a shortcut to launching applications.

    If only someone, anyone, had taken the time to write a comprehensive Quicksilver manual!

    (Ahem. Queueing up a printout of the PDF now. The irony of this is that I was actually looking for Castro’s Favorite Color, to see what other goodies I might learn from you, and found that the first Google hit for “howard melman mac” was something subtly yet gratifyingly different.)

  5. Sadly QS is getting a little old and broken down, particularly on snow leopard. On SL setting prefs or configuring triggers can be frustrating. I haven’t updated the manual for the issues. The google user group has some info. The preferences part of the manual needs a rewrite to clean up the language but that’s mostly done for the main part. Just stick to the basic plugins and only add new ones as you need them.

    Butler and LaunchBar have improved but I haven’t used them so can’t compare them. Google Search Box’s progress is slow and doesn’t have triggers yet.

    The point of my comment above. You can active QS with control space, type to bring up your script in the first pane and choose the run action the in the second and hit return to run that command. That’s the easy way to run anything via QS (launching apps or files). For stuff you do all the time, create a trigger, that is a global key binding that you can hit at any time to run the command.

  6. Oh my ǥoodness! Impressive article dude! Thank you
    so much, However I am having problems with your RSS. Ⅰ don’t кnow why I cannot subscribe to it.
    Is there anyone else getting identical ᏒSS issues? Anyone who knoᴡs the answer wiⅼl ʏou kindly respond?

    Thanx!!

Leave a Reply

Your email address will not be published. Required fields are marked *