OS X: new terminal window in current directory

Ovid on 2007-01-05T18:54:16

Jut thought this might prove useful for some folks. I know I borrowed this code from somewhere but, uncharacteristically, I didn't paste the URL in with the code, so I can't credit the source (I recall hacking the code, but I still can't claim credit).

If you're running OS X and you open a new terminal window, it's handy if it opens in the same directory as the one you're currently working in. The following script allows this (if you know of a better way, please let me know).

#!/bin/sh
#
# Open a new terminal in the cwd
#

CWD=`pwd`
osascript<


X.org

chromatic on 2007-01-05T19:49:53

For X.org users, it's simply:

xterm &

Just how old is this mistake now?

Aristotle on 2007-01-06T04:26:01

do script with command "cd \"" & thePath & "\""

Pray that the current directory path never contains any shell metacharacters other than spaces.

Here's a Mac::Glue version

pudge on 2007-01-19T06:54:50

I don't use Terminal much, I prefer iTerm. But, for Terminal:

#!/usr/bin/perl
use Cwd;
use Mac::Glue;
 
(my $cwd = cwd()) =~ s/'/'\\''/g;
 
my $term = new Mac::Glue 'Terminal';
$term->do_script(with_command => "cd '$cwd'");
For iTerm:

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

Re:Here's a Mac::Glue version

Jeff Harrell on 2007-02-02T22:02:37

I found this through a search, but ended up with my own version using pbcopy / pbpaste on a one-liner.

echo `pwd` | pbcopy; osascript -e "tell application \"Terminal\" to do script \"cd \\\"`pbpaste`\\\"\""