Running UNIX commands from OS X’s Finder

Mac OS X is a rather schizophrenic beast. Part ultra-modern GUI, part hard-core command line system. Yup, they’re both in there. So, whichever way you care to work, you can get things done. Unless, of course, you want to do something using a little bit of each. Then OS X is a pain in the butt. All the icky UNIX stuff is mysteriously invisible to the Finder and all the slick Finder stuff is largely useless from a UNIX “shell”.

You can sort of work around it by using “Go to Folder”, and open up, say, “/etc”. Ooh, looky all the weird UNIX stuff. But, then, it won’t let you create aliases to that stuff. Dang, so close.

My initial goal: To be able to edit my “hosts” file (/etc/hosts) by clicking on an icon in the Finder. I paid plenty of money for this fancy UI, I should be able to do that, dammit.

My extended goal: To create a directory called “UNIX Commands”, into which I could add click-to-run icons for all those down-and-dirty UNIX things I need to do now and then.

Now, mind you, I have a bona fide Computer Science degree from back when “Windows” and “Mac” were purely extra-curricular “novelty” topics, so I’m not afraid of a little UNIX now and then. I’m going to explain this in a manner assuming you know a tiny bit of UNIX.

OK, here’s what I’ve done to get things working the way I wanted.

I put my “UNIX Commands” folder in my user folder (because I have that in my dock and can just click-and-hold all the way to launching the commands). I’ll assume you’re fine with this idea.

So,

Step 1: Create a folder called “UNIX Commands” in your Home (username) directory.
Step 2: Launch the Terminal from Applications/Utilities
Step 3: Go to that directory… OK, I’ll help you out:

cd /Users/yourusername/Desktop/Unix Commands/

Step 4: Create a text file using your favorite UNIX editor (I use emacs). I’m calling mine “edithosts”

emacs edithosts

Step 5: I can’t teach you how to use emacs, or any other editor, but however you manage to do it, edit the file so it looks like this:

!# /bin/bash
sudo emacs /etc/hosts

That second line should be whatever UNIX command you want to run. As I said before, I’m using this to edit my “hosts” file. The first line is the same no matter what. It tells UNIX (Darwin) - hey, execute this file with “bash”.

OK, save your file and exit the editor.

Step 6: You must tell Darwin “officially” that this is an executable file. You do that by typing this at the terminal prompt:

chmod +x edithosts

OK. Cool. Now exit the Terminal. We’re done with all that for now.

Step 7: Back in the happy, colorful world of Finder, open up your “UNIX Commands” folder. You should see your file there, looking like a boring piece of paper. Go ahead and double-click it. Finder will whine that there’s no default application to open the document. Click “Choose Application…”. Switch “Enable” from “Recommended Applications” to “All Applications”. Choose “Terminal” and click Open. Here’s the gerat part… Finder will tell you: “It is not known if this application can open Unix Executable File files”. Say “Duh” (optional) and Click Open. Finder will pop up ANOTHER weird dialog saying “an error occured while changing the application that opens “edithosts” because not enough information is available. Do you want to open “edithosts” with “Terminal”. Say “Duh” again (optional) and Click “Update”. Say “Duh” again because the dialog asked you a yes or no question, and you’re given the options of “Don’t Update” and “Update”.

Step 8: You did it! Right now, your UNIX command should have executed.

Hey, that was pretty cool. Here’s the neat part though… When you need another command, don’t bother creating it from the Terminal. You can duplicate your existing command right from the Finder. Conveniently, OS X will remember the “executable” attribute you set in Step 6. Inconveniently, the first time you run your new command, you’ll have to go through all that silliness in Step 7. Eh, no biggie.

One last tip… To make your life WAY easier, make an Alias for TextEdit in your “Unix Commands folder. That way, when you create a new command by duplicating an old one, you can just drag-and-drop it into TextEdit to change the actual command. Super cool!

[Administrative note: This blog entry was written late at night with little regard to coherency or grammar. I just wanted to get my little victory online as quickly as possible. It might be edited for clarity in the near future.]

Leave a Reply