Useful Mac OS X-specific command line utilities

One of the greatest strengths of Mac OS X, for developers in particular, is that it has a very elegant and consistent graphical user interface as well as an excellent command line interface. I’m not going to cover the basics like “ls” and “cd”, but rather point out some Mac OS X specific tools that are less well known than they should be.

Many of these are the command line equivalents for the GUI versions available in OS X. Combining them with other command line tools can be very powerful and huge time savers. See the “man” pages for more details of each.

open – Opens a file, application, or directory in GUI-land. Very useful.

screencapture – Take a screenshot. Exactly the same as 3 or 4 (or even 4 then ) plus more.

say – Text to speech. Give it a file name, string of text, or pipe the output of another program to it. Options for difference voices,
saving the result to a file, etc. Fun hobby: ssh into a computer being used by someone else and start speaking to them using “say”.

pbcopy and pbpaste – Copy and paste to/from the OS X pasteboard.

srm – Secure “rm”. Like “rm” but overwrites deleted data. “-m” gives you DoD compliant erasing!

osascript – Run AppleScripts (or other OSA languages) from the command line. (I use this in the “term” script)

hdiutil – create and open disk images (.dmg)

defaults – view and set various hidden settings. Check out macosxhints.com for some of these.

These are just a few Mac OS X specific commands I found most useful. Amit Singh has a fairly comprehensive list over at his excellent kernelthread.com website.

ANSI escape sequences in JavaScript (color on the command line!)

Recently I’ve been doing a lot of work in Rhino, Mozilla’s JavaScript interpreter written in Java. While it’s a bit slower than some of the other JavaScript interpreters out there it has the advantage of being able to call Java libraries. This is immensely useful since JavaScript is pretty bare-bones in terms of built-in libraries.

Anyway, I had a simple logging system with the usual “error”, “warn”, “info”, etc levels, which was great except the important errors and warnings would get lost in the hundreds of info and debug messages. I didn’t know much about ANSI escape codes except that it allows for neat things like coloring text in your terminal, so I wrote a little code to help with this:

ANSI_ESC            = String.fromCharCode(0x1B);
ANSI_CSI            = ANSI_ESC + ‘[‘;
ANSI_TEXT_PROP      = ‘m’;
ANSI_RESET          = ‘0’;
ANSI_BOLD           = ‘1’;
ANSI_FAINT          = ‘2’; // unsupported?
ANSI_NORMAL         = ’22’;
ANSI_ITALIC         = ‘3’; // unsupported?
ANSI_UNDER          = ‘4’;
ANSI_UNDER_DBL      = ’21’; // unsupported?
ANSI_UNDER_OFF      = ’24’;
ANSI_BLINK          = ‘5’;
ANSI_BLINK_FAST     = ‘6’; // unsupported?
ANSI_BLINK_OFF      = ’25’;
ANSI_REVERSE        = ‘7’;
ANSI_POSITIVE       = ’27’;
ANSI_CONCEAL        = ‘8’;
ANSI_REVEAL         = ’28’;
ANSI_FG             = ‘3’;
ANSI_BG             = ‘4’;
ANSI_FG_INTENSE     = ‘9’;
ANSI_BG_INTENSE     = ’10’;
ANSI_BLACK          = ‘0’;
ANSI_RED            = ‘1’;
ANSI_GREEN          = ‘2’;
ANSI_YELLOW         = ‘3’;
ANSI_BLUE           = ‘4’;
ANSI_MAGENTA        = ‘5’;
ANSI_CYAN           = ‘6’;
ANSI_WHITE          = ‘7’;

ANSIControlCode = function(code, parameters)
{
    if (parameters == undefined)
        parameters = "";
    else if (typeof(parameters) == ‘object’ && (parameters instanceof Array))
        parameters = parameters.join(‘;’);
    return ANSI_CSI + String(parameters) + String(code);
}

// simple text helpers:

ANSITextApplyProperties = function(string, properties)
{
    return ANSIControlCode(ANSI_TEXT_PROP, properties) + String(string) + ANSIControlCode(ANSI_TEXT_PROP);
}

var colorCodeMap = {
    "black"   : ANSI_BLACK,
    "red"     : ANSI_RED,
    "green"   : ANSI_GREEN,
    "yellow"  : ANSI_YELLOW,
    "blue"    : ANSI_BLUE,
    "magenta" : ANSI_MAGENTA,
    "cyan"    : ANSI_CYAN,
    "white"   : ANSI_WHITE
}

ANSITextColorize = function(string, color)
{
    if (colorCodeMap[color] == undefined)
        return string;
    return ANSITextApplyProperties(string, ANSI_FG + colorCodeMap[color]);
}

Download here: ansi_sequences.js

ANSIControlCode(code, parameters) takes in a control code as a string (a single alphabetic character, see above Wikipedia article), and zero or more parameters, either as a single string/number or array of strings/numbers (again, see above Wikipedia article). It returns a string with the control sequence.

There are also two helpers functions for setting text properties:

ANSITextApplyProperties(string, properties) wraps “string” in a pair of control sequences. First the sequence to set text properties (“m” along with the given parameters), second the reset text properties sequence (“m” with no parameters). It returns the wrapped string. This lets you easily apply a number of properties to a string.

ANSITextColorize(string, color) simply sets the foreground color of a string, and returns the wrapped string.

Here are examples of the helper functions:

Note that color control codes must be combined with either ANSI_FG or ANSI_BG.

Also note that this is completely useless in the browser. Only shells can understand these control codes, so use it in Rhino and other command line JavaScript interpreters (while this code is written in JavaScript, the same concepts be applied to any other programming language that can run in a shell: C, Python, Ruby, etc).

A new kind of music video: Radiohead's "House of Cards"

Radiohead’s latest music video, “House of Cards”, was shot using LIDAR (Light Detection and Ranging) rather than cameras:

Learn more about how it was made at the Google Code Radiohead page. The coolest part is that Radiohead is making the data available to anyone who wants to play with it.

They provide some sample Processing code to view the data, but for whatever reason I couldn’t get it to work, so I wrote a simple little iPhone OpenGL ES application:

I’m unable to test it on a real iPhone since I don’t have a paid developer account, but presumably it should work.

View other people’s visualizations at the YouTube House of Cards group.

Multitouch JavaScript "Virtual Light Table" on iPhone v2.0

Now that iPhone 2.0 is out I started playing around with some of the new web features, and soon found that I had created the prototypical virtual light table that’s an essential demo for any new multitouch technology.

It’s about 100 lines of JavaScript. It grabs the 10 latest photos from Flickr’s “interesting photos” API and randomly places them on the screen for you to play with:

This is great if you have an iPhone with the 2.0 software, but desktop browsers should get some multitouch love too. So I started writing a little bridge that fakes multitouch events in desktop browsers. It’s far from complete, but it’s just good enough to get the virtual light table demo working.

So go ahead and load it up in the new iPhone MobileSafari or Safari 3.1+ / WebKit nightly (requires CSS transforms):

http://tlrobinson.net/iphone/lighttable/

In desktop browsers it uses the previous clicked location as a second “touch”, so you can click a photo then click and drag another spot on the photo to resize and rotate (notice the yellow dot).

For a good overview of touch events and gestures, check out this SitePen blog post and Apple’s documentation.

Here’s the source for the fake multitouch bridge:

http://tlrobinson.net/iphone/lighttable/multitouch-fake.js.

Clearly the reverse of this bridge would be even more useful, since iPhone only sends mouse events under specific conditions. The mousedown, mouseup, and mousemove events could be emulated using the touch equivalents to make certain web apps work on the iPhone without much additional work. Of course you would need to either cancel the default actions (i.e. panning and zooming) on touch events, or have some way to manage the interactions between them.