Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 05-05-2003, 07:39 AM   #1
griffman
MVP
 
Join Date: Dec 2001
Location: Portland, OR
Posts: 1,472
Help with script submission...

The following script, which seems quite useful, is designed to open a new Terminal window set at the current window's location on your path. However, I cannot get it to run on 10.2, and the submitter has not responded to my emails. Can anyone out there figure out why this isn't working and post and/or email me the corrections? I'd love to get this online...

Code:
#! /bin/sh
# @(#)newterm 1.0.0
#
# Causes Terminal.app to open a new terminal window in the current directory
#
# Initial Setup:
#   1) Startup 'Terminal.app' and modify terminal properties as desired.
#   2) File->Save. Name saved terminal as 'newterm.term' on desktop.
#   3) Edit 'newterm.term' with 'vi', and substitute the following as
#      the string parameter for key 'ExecutionString'
#          ...
#          <key>ExecutionString</key>
#          <string>cd $DIR/</string>      <- change to this exactly!
#          ...
#      then save the file.
#   4) Make a hidden directory to store the saved terminal settings.
#          $ mkdir ~/.skel
#   5) Move the edited file to the skeleton directory.
#          $ mv ~/desktop/newterm.term ~/.skel/
#   6) Move this file to your binary directory.
#          $ mv /path/to/newterm ~/bin/
#          $ chmod u+x ~/bin/newterm
#          $ rehash
#
# Usage:
#   While inside a terminal window, issue the following command:
#          $ newterm
#   A new terminal window should appear with its current directory
#   in the same directory as the window from which the command was typed.
#
#   While inside a terminal window, issue the following command:
#          $ newterm /usr/local
#   A new terminal window should appear with its current directory
#   set to the machine-specific directory.
#
# Author:
#   Paul Roebuck
#

SCRIPT=newterm
TERM_FILE=newterm.term
SKELETON_TERM_DIR=~/.skel
SKELETON_TERM_PATH=$SKELETON_TERM_DIR/$TERM_FILE
UID=$( id -u )
TEMP_ITEMS_DIR=${TMPDIR:-"/tmp/$UID/Temporary Items"}
RUN_TERM_PATH=$TEMP_ITEMS_DIR/$TERM_FILE

case $# in
    0) DIR=$( pwd );
       ;;
    1) DIR=$1/;
       if [ ! -d $DIR ]
       then
           echo "$SCRIPT: $DIR does not exist or permission issues exist"
           exit 1
       fi
       ;;
    *) echo "Usage: $SCRIPT [directory]" 1>&2;
       exit 2 ;;
esac

if [ ! -d "$TEMP_ITEMS_DIR" ]
then
    mkdir -p "$TEMP_ITEMS_DIR"
fi
if [ -r $SKELETON_TERM_PATH ]
then
    trap '' 1 2 15      # ignore signals
    sed "s#$DIR#"$DIR/"#" $SKELETON_TERM_PATH > "$RUN_TERM_PATH"
    open "$RUN_TERM_PATH" && rm "$RUN_TERM_PATH"
else
    echo "$SCRIPT: $SKELETON_TERM_PATH does not exist or lacks read permission"
    exit 1
fi
exit 0
Thanks!

-rob.
griffman is offline   Reply With Quote
Old 05-05-2003, 12:25 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
two problems

I encountered two problems with this script.
The first problem is that the UID variable is read-only in the 'bash' shell and '/bin/sh' is actually just a copy of '/bin/bash'.

The fix for this problem is simply to avoid naming the variable "UID". (E.g. name it USERID) and all should be well.

The second problem is with the 'sed' line which is trying to replace the $DIR in the newterm.term file with the name of the desired directory. I think this is getting into trouble because of the first $DIR in the sed expression. There is no need to have the "target" string have a $ in it. That just makes things more difficult. Instead, just make up some unique string (e.g. INITIAL_DIR) and use that in the newterm.term file and in the sed expression. The sed expression would then be:
sed "s#INITIAL_DIR#"$DIR"#"

(I have also removed the extarneous slash at the end)

With these changes, the script works for me on 10.2.5
hayne is offline   Reply With Quote
Old 05-05-2003, 01:49 PM   #3
sao
Moderator
 
Join Date: Jan 2002
Location: Singapore
Posts: 4,237
griffman,

Just in case...I use an 'AppleScript' written by Marc Liyanage called 'Open Terminal Here' and it works very well for me:

http://www.entropy.ch/software/applescript/welcome.html

"This is a Toolbar Script for Mac OS X 10.1 and later. When its icon is being clicked on in the toolbar of a given directory window, it will open up a new Terminal session with that directory as working directory, i.e. it will "cd" there. It's also possible to drop one or more folders or other items onto the icon. The script will open up a Terminal session in each dropped directory, and for each non folder item a session in the item's respective parent directory".
sao is offline   Reply With Quote
Old 05-05-2003, 02:17 PM   #4
griffman
MVP
 
Join Date: Dec 2001
Location: Portland, OR
Posts: 1,472
I've seen that ... this one is cool because it's for when you're working in a directory in the Terminal already, and you just want a new Terminal window into that same directory -- no switching to the Finder, no Command-N followed by 'cd', etc...

-rob.
griffman is offline   Reply With Quote
Old 05-05-2003, 08:31 PM   #5
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
OK, perhaps I'm missing the rationale for the complexity of the initial offering, (and (sadly) I'm far too lazy to go trawling through the initial script), but how about...

#!/bin/sh
dir=`pwd`;
osascript<<END
tell app "Terminal"
do script "cd $dir"
end tell
END

It's no rocketship, but it gets the job done without having to munge any .term files. Call it "clone" or whatever and chuck it in ~/bin say, do the usual "chmod u+x clone", "rehash" and you should be away. Kind of cute really: thanks for the impetus to go exploring!

Paul (hoping he hasn't misinterpreted the required function too badly. If so: egg, meet face...)
pmccann is offline   Reply With Quote
Old 05-05-2003, 08:40 PM   #6
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
heh. i just finished 'working' on the very same thing. thanx paul.
__________________
On a clear disk, you can seek forever.
mervTormel is offline   Reply With Quote
Old 05-05-2003, 08:47 PM   #7
griffman
MVP
 
Join Date: Dec 2001
Location: Portland, OR
Posts: 1,472
Perfect!

Quite cool; I guess I'll publish this one and refer people to this thread and stay away from the original!

Nice job and thanks!

-rob.
griffman is offline   Reply With Quote
Old 05-05-2003, 08:58 PM   #8
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
(It's all in the wrist action mT, all in the wrist action; just like battling tops...)

I s'pose this borders on the ridiculous, but here's a two line version:

#!/bin/sh
osascript -e "tell app \"Terminal\" to do script \"cd `pwd`\""

Doing better than that is going to be something of a challenge (particularly with the pound-bang construction consuming one of the lines!). The one above is much cleaner and prettier in any case, and removes the need for all the 'orrible backslashes.

Cheers,
Paul

Last edited by pmccann; 05-05-2003 at 09:02 PM.
pmccann is offline   Reply With Quote
Old 05-06-2003, 08:27 AM   #9
gatorparrots
Major Leaguer
 
Join Date: Dec 2002
Posts: 441
Quote:
Originally posted by pmccann Doing better than that is going to be something of a challenge (particularly with the pound-bang construction consuming one of the lines!). The one above is much cleaner and prettier in any case, and removes the need for all the 'orrible backslashes.

Who needs backslashes when you have single quotes?
osascript -e 'tell app "Terminal" to do script "cd `pwd`"'

And of course, to make it a one-liner (removing the shebang line), make it an alias rather than a script.
gatorparrots is offline   Reply With Quote
Old 05-06-2003, 08:37 AM   #10
griffman
MVP
 
Join Date: Dec 2001
Location: Portland, OR
Posts: 1,472
What would that look like as an alias? Don't you need to add back some of the backslashes?

-rob.
griffman is offline   Reply With Quote
Old 05-06-2003, 09:35 AM   #11
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
Arrrgh! Repeat after me: folder names can have spaces in them. Hence the above stream of responses should probably all be variants of

#!/bin/sh
osascript <<END
tell app "Terminal" to do script "cd \"`pwd`\""
END

((...just when you might have thought gatorparrots had made it safe to go in the water without having to perform the backstroke! I'll leave everyone else to defend their favourite incantation, and gatorparrots to provide the alias. Me senses ugly quoting details starting to rear their hideous heads when moving in that direction.

Sorry Rob...))

Cheers,
Paul (slayer of aliases: too many shell variants to cover, too many quote marks requiring a scratch or three of my thick noggin)
pmccann is offline   Reply With Quote
Old 05-07-2003, 10:49 PM   #12
wgscott
Major Leaguer
 
Join Date: Sep 2002
Posts: 350
This also gives an error:

% ~/Scripts/terminal.sh
execution error: Can't get end. (-1728)

strangly, it seems to then work.

I think it needs the

end tell

Last edited by wgscott; 05-07-2003 at 10:53 PM.
wgscott is offline   Reply With Quote
Old 05-07-2003, 11:00 PM   #13
wgscott
Major Leaguer
 
Join Date: Sep 2002
Posts: 350
Nope. Case threw me. The last END is superfluous.

THIS works:

#!/bin/sh
osascript<<END
tell app "Terminal"
do script "cd \"`pwd`\""
end tell
wgscott is offline   Reply With Quote
Old 05-07-2003, 11:37 PM   #14
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
Quote:
Originally posted by wgscott
Nope. Case threw me. The last END is superfluous...

perhaps, in this case, because the END was implicit in the script ending.

your error was the result of a superfluous space after the terminal sentinel END

#!/bin/sh
osascript <<END
tell app "Terminal" to do script "cd \"`pwd`\""
END

copy/pasting from/to a webpage creates these space anamolies (how STNG). the terminating sentinel is very specific, and white space in the initial here-doc terminal specifier is ignored...

$ cat <<VERY specific sentinel
-> foo
-> bar
-> VERY not so specific sentinel
-> VERY
cat: specific: No such file or directory
cat: sent: No such file or directory


lesson: be VERY specific ;]
__________________
On a clear disk, you can seek forever.
mervTormel is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 12:48 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.