Stupid Mac::Glue Tricks: new terminal window in Finder window

pudge on 2007-01-19T07:06:31

Ovid had a code snippet for opening a new terminal window in the current Terminal directory. It was in AppleScript, and because it's what I do, I rewrote it in Mac::Glue, and also rewrote it to work in iTerm, whch is what I use.

I have for awhile wanted something similar for the Finder, but never got around to it. That is, open a terminal for whatever Finder window I am in. So ... here it is.

#!/usr/bin/perl
#
# Open a new terminal in the Finder cwd
#

use Mac::Files; use Mac::Glue ':all';

my $finder = new Mac::Glue 'Finder'; my $cwd = $finder->prop(target => window => 1)->get(as => 'alias'); $cwd ||= FindFolder(kUserDomain, kDesktopFolderType); # default to Desktop $cwd =~ s/'/'\\''/g;

my $iterm = new Mac::Glue 'iTerm'; $iterm->activate; my $term = $iterm->make(new => 'terminal'); $term->Launch(session => 'default'); $term->obj(session => 1)->write(text => "cd '$cwd'");


If you want to use Terminal, replace the lines about iTerm with these:

my $term = new Mac::Glue 'Terminal';
$term->do_script(with_command => "cd '$cwd'");