Stupid Mac::Glue Tricks: iTerm Scripting
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. :-)
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__
Leave a comment