Stupid Mac::Glue Tricks: iTerm Scripting

| | Comments (0)
I wanted to create a bunch of windows, tabs, etc. and do things in them. So, I wrote this script. I run it when I am doing maintenance and refresh on useperl, and for some other sites.

It opens one window (terminal) in iTerm, then five separate tabs (sessions), preparing each one to do various things (ssh to a host, preparing to tail a log, etc.) and setting the name of the tab.

I normally set my session title with PROMPT_COMMAND in my environment, so I unset that env var and then set the name of the session with Mac::Glue.

It sleeps because it sometimes takes a few seconds for the unset to take, and if I set the name of the session too quickly, then it will get overwritten again. Another possibility would be to go through everything, then go back and set the session names on a separate pass.

waitforit() prints out a command and then waits for me to hit return, to execute it. I couldn't find a good way to tell iTerm to print the text and not execute it, so I execute perl on the host to do it for me. :-)

#!/usr/bin/perl
use warnings;
use strict;
 
use Mac::Glue ':all';
 
my $iterm = new Mac::Glue 'iTerm';
$iterm->activate;
 
my $prj = 'useperl' || shift;  # different for each project
my $nfs = "$prj-nfs-1";
my $db  = "$prj-db-1";
 
my $watchneg = ' | egrep -v "File does not exist"';
 
# first arrayref happens before the unset, second after
my %sessions = my @sessions = (
    Main    => [["ssh $nfs"], []],
    DB    => [["ssh $db"], ['mysql']],
    Ctrl    => [[], [waitforit(qq'$prj-control -d; $prj-control -p 5')]],
    HTTP    => [[], [waitforit(qq'$prj-watch $watchneg')]],
    slashd    => [["ssh $nfs"], [
        'cd /usr/local/slash/site/*/logs',
        waitforit('tail -f slashd.log')
    ]],
);
@sessions = grep !ref, @sessions;
 
my $term = $iterm->make(new => 'terminal');
 
for my $num (1 .. @sessions) {
    my $name = $sessions[$num - 1];
    my $sess = $term->obj(session => $num);
 
    $term->Launch(session => 'Default Session');
    $sess->write(text => 'ssh osdn');
 
    for my $cmd (@{$sessions{$name}[0]}) {
        $sess->write(text => $cmd);
    }
 
    $sess->write(text => "unset PROMPT_COMMAND");
 
    for my $cmd (@{$sessions{$name}[1]}) {
        $sess->write(text => $cmd);
    }
 
    sleep 5;
 
    $sess->prop('name')->set(to => $name);
}
 
sub waitforit {
    my($cmd) = shift;
    $cmd =~ s/'/'\\''/g;
    return "perl -e '\$c = shift; print qq(\$c: ); <>; system(\$c)' '$cmd'";
}
 
__END__

use.perl.org

Leave a comment

<pudge/*> (pronounced "PudgeGlob") is thousands of posts over many years by Pudge.

"It is the common fate of the indolent to see their rights become a prey to the active. The condition upon which God hath given liberty to man is eternal vigilance; which condition if he break, servitude is at once the consequence of his crime and the punishment of his guilt."

About this Entry

This page contains a single entry by pudge published on April 23, 2004 3:50 PM.

Spam for Politicians was the previous entry in this site.

Another Caucus, Another Delegate is the next entry in this site.

Find recent content on the main index or look in the archives to find all content.