Terminal tricks: “term”
Mac OS X — 28 Feb 2005 13:19 — 2549 days ago

Today I had the idea for a short shell/AppleScript hack, and when I started to implement it I noticed I already had the same idea two years ago... So I cleaned it up a little to post it here.

What it does is open up a new Terminal window and execute the arguments as command in that new window. When no arguments are given, it opens the new window in the current directory, which means it acts like a “clone” operation for the frontmost window. This is useful if you're in some deeply nested directory and you need a second window right there. This way you don’t have to copy/paste or type the directory path.

#!/bin/sh
#
# Open a new Mac OS X terminal window with the command given
# as argument.
#
# - If there are no arguments, the new terminal window will
#   be opened in the current directory, i.e. as if the command
#   would be "cd `pwd`".
# - If the first argument is a directory, the new terminal will
#   "cd" into that directory before executing the remaining
#   arguments as command.
# - If there are arguments and the first one is not a directory,
#   the new window will be opened in the current directory and
#   then the arguments will be executed as command.
# - The optional, leading "-x" flag will cause the new terminal
#   to be closed immediately after the executed command finishes.
#
# Written by Marc Liyanage <http://www.entropy.ch>
#
# Version 1.0
#

if [ "x-x" = x"$1" ]; then
    EXIT="; exit"; shift;
fi

if [[ -d "$1" ]]; then
    WD=`cd "$1"; pwd`; shift;
else
    WD="'`pwd`'";
fi

COMMAND="cd $WD; $@"
echo "$COMMAND $EXIT"

osascript 2>/dev/null <<EOF
    tell application "Terminal"
        activate
        do script with command "$COMMAND $EXIT"
    end tell
EOF
Put this into a file called “term” and store it somewhere in your $PATH. I keep a directory called “bin”, which I add to $PATH in my .bashrc file, in my home directory for this purpose.

Some examples:

term
Open a second window in the current directory.

term ~
Open a second window in my home directory.

term vi ~/.bashrc
Open a second window and start editing my .bashrc file.

term ~ vi .bashrc
The same thing.

term vi xyz.sh
Open a second window in the current directory and start editing the xyz.sh file.

term tail -f error_log
Open a second window in the current directory (presumably /var/log/httpd) and start following the error_log file using tail.


Comments
Posted by No one on 1 Mar 2005 18:17

First - be careful copying and pasting this. From NetNewsWire I ended up with curly quotes (“”) instead of plain ("") quotes. Also - under 10.4 it doesn't seem to create a new window - but instead executes the scripts in the current/front window. Bug?

Posted by Marc on 1 Mar 2005 21:31

Oh right, I thought I told ecto not to convert the quotes for this post. I fixed them manally now, copy/paste should work now.

As for 10.4, your NDA does not allow you to discuss it in public forums like this. E-Mail me privately if you want to talk about it.

Posted by Andrew on 3 Mar 2005 19:22

fantastic.

Posted by Derek on 19 Mar 2005 21:13

This script doesn't work with the tcsh shell, only the bash shell (which became the default OS X shell with 10.3, I think).

I can execute this part of the script manually without prompting an error from tcsh:

osascript 2>/dev/null < tell application "Terminal"
activate
do script with command "$COMMAND $EXIT"
end tell
EOF

However, running "term" gives me the following error:

tcsh: xterm-color: Command not found.

I assume this error is being caused by some code above the osascript part, but I'm not a shell programming person by any means and wouldn't know where to start looking.

Any ideas how to make this work under a tcsh shell would be appreciated!

Posted by Derek on 20 Mar 2005 21:53

Hmmmm, just a followup on my previous comment. It seems that tcsh already has a built-in called "term", so renaming this script to something else (e.g., "nt" for "New Terminal" or something else that's memorable) will make it work for those of you using tcsh as your default shell.

Posted by Rick van der Zwet on 15 Jan 2007 19:38

Fantastic, works likes a sharm

Posted by Victor on 14 May 2007 20:39

Well done, thanks!

Powered By blojsom