Inconsistent behavior for applescript run from editor vs shell script

I’m having some very inconsistent and puzzling applescript/shell script behavior. I’m relatively new to both applescripting and shell scripting but I’ve been able to get several shell and applescripts to work in the past, but this has got me stumped.

What I am trying to accomplish is very simple. I would like to run a script in the terminal that changes the terminal’s current directory to what ever the front finder window is. The applescript below does this, and it works perfectly in the applescript editor.

-- Get path of front finder window
tell application "Finder"
	set theWindow to window 1
	set thePath to (quoted form of POSIX path of (target of theWindow as alias))
end tell

-- Change terminal directory to path of front finder window
set theShellScript to "clear ; cd " & thePath

tell application "Terminal"
	do script theShellScript in window 1
end tell

What is puzzling is that it also works fine if I run it as a shell script BUT it will only run if I comment out the “in window 1” part at the end of the do script command. While this works it doesn’t do exactly what I want because it opens a new terminal window and cd’s to the directory of the front finder window in a new terminal window. Again, I would like to have the directory change occur in the current terminal window; and again, this works fine if i run the script from the script editor.

Any ideas??? What could I possibly be doing wrong?


tell application "Terminal"
	--do script theShellScript in window 1
	do script theShellScript in first window
end tell

or


tell application "Terminal"
	set winName to name of window 1
	do script theShellScript in window winName
end tell

maybe?

Thanks for making me look at this again, I was using Keystrokes, But just had a play.

Try this.

-- Get path of front finder window
tell application "Finder"
	set theWindow to window 1
	set thePath to (quoted form of POSIX path of (target of theWindow as alias))
end tell

-- Change terminal directory to path of front finder window
set theShellScript to "clear ; cd " & thePath



tell application "Terminal"
	do script theShellScript in selected tab of window 1
end tell

edit
Ah, piped to the post by pandrake

Thanks for the replies. Unfortunately, I still have the same problem, both pandrake’s suggestions work perfectly when the script is run from the applescript editor; but they do not work when the applescript is embedded in a bash shell script and run from the terminal. Again, what puzzles me most about this is that I can get the applescript to run in the bash shell script, but it will only work if I remove reference to a specific window and allow terminal to open a new window, then the bash script works fine. My thinking is that it’s not a problem on the bash shell script side because I can get the bash script/applescript to change the terminal directory (it just happens in a new window); and it’s not on the applescript side because all three versions of the applescript (my original version and both of pandrake’s suggestions) work fine in the script editor. So I don’t know where else to look :slight_smile:

Also, I apologize for not mentioning this in my original post but I’m running tiger so no tabbed terminal; so I can’t say if mark hunte’s suggestion would work.

Just in case, here’s the full bash shell script. Again, it runs fine if you remove the “in window 1” part, but opens a new terminal window; while just the applescript part run from the applescript editor will run perfectly and change directory in window 1.

#!/bin/bash

/usr/bin/osascript <<-EOF

-- Get path of front finder window
	tell application "Finder"
		set theWindow to window 1
		set thePath to (quoted form of POSIX path of (target of theWindow as alias))
	end tell

	-- Change terminal directory to path of front finder window
	set theShellScript to "clear ; cd " & thePath

	tell application "Terminal"
		do script theShellScript in window 1
	end tell

EOF