Using system dialogs in textmate commands / shell scripts (on a mac)

Sometimes you need to provide user input to your textmate scripts. The usual cocoa inputs as described in the textmate manual don’t work in a bash environment, therefor a wrapper is needed. One way to do this, is to use the library CocoaDialog. After the initial setup, as described here in the documentation we are able to call a dialog from the command shell which can now be exploited in our scripts.

An example command to replace a placeholder in your textmate file with iterating numbers (more dialog examples can be found on the cocoadialog examples page):

#!/usr/bin/perl -w
use strict;

our $CD = "$ENV{HOME}/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog";

my $rv = `$CD standard-inputbox --title "Enter increment start number" --no-newline \\
    --informative-text "Where to begin with incrementation?"`;

my ($button_rv, $retval) = split /\n/, $rv, 2;
my $inc = 1;

if ($button_rv == 1) {
     while(<>) { 
        if(m/^\\i\((\d+)\s*,\s*(\d+)\)$/) { 
                $retval=$1; 
                $inc=$2; 
                next; 
        } 
        while(s/\\i/$retval/) {$retval+=$inc;} 
        print; 
	} 
}

Now you can insert the placeholder \i whereever you like and apply your textmate command to replace every instance of the placeholder with an increasing number, starting by your provided number.

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.