osascript problem ...

inspired by an other thread in the studio/xcode forum http://bbs.applescript.net/viewtopic.php?id=17501 i tried to find a solution how to run a shell script in terminal and close the window after execution. My idea was to close the window with an osascript command at the end of the shell script. The script I posted there works - but has one potential problem: It fails to close the correct window if the user makes an other terminal focus (frontmost) during execution.

I thought, I could use the window’s id to avoid this. Closing the window by it’s id works from AppleScript:


tell application "Terminal"
	do script ""
	set newWin to first window whose frontmost is true
	set newWinID to (id of newWin)
end tell
tell application "Terminal" to tell (first window whose id is (newWinID as integer)) to close

But as soon as I try to pack the last line in an ‘osascript’-line it fails:


tell application "Terminal"
	do script ""
	set newWin to first window whose frontmost is true
	set newWinID to (id of newWin)
	do script "osascript -l AppleScript -e " & (quoted form of ("tell application \"Terminal\" to tell (first window whose id is (" & newWinID & " as integer)) to close")) in newWin
end tell

I tried several versions of the last line of script, but I allways get errors (‘Expected expression but found property or key form. (-2741)’).

I’ll appreciate any idea how to solve this or an explanation why it doesn’t work.
Many thanks in advance.

Dominik

Hi Jacques,

many thanks for your solution. The script here was just a test - not the full script from the mentioned thread - I left out the ‘repeat while exists newWin’
this now works ok for me:

tell application "Terminal"
	do script ""
	set newWin to first window whose frontmost is true
	set newWinID to (id of newWin)
	do script "lsof; osascript -l AppleScript -e " & (quoted form of ("tell application \"Terminal\" to tell (window id " & newWinID & ") to close")) in newWin
	repeat while exists newWin
	end repeat
end tell
display dialog "done"