Help with Pre emptively launching Terminal App

I woule like my Applescript to do the following:

  1. pre-emptively launch the terminal WITHOUT opening up a Terminal window.
  2. Run other custom AppleScript Code (hmm. This is where the real work occurs)
  3. Then open up a Terminal window, send some commands (or a get it to execute a Bash Script) and set a custom title for the Terminal window.
  4. After the Terminal has finished executing the commands or script, I would like to automatically close the window after a 5 sec delay.

I have found that “Tell application “Terminal” to launch” does not really work like in other applications. If the Terminal is NOT already running, the launch command creates a new Terminal window. I had to write some custom code below to acheive the result I wanted. (i.e. Launching the Terminal without opening up a Terminal window).

I also found that AppleScript does NOT tell you when the Terminal has finished executing the commands you send to it. I had to use the “busy” property to check if the Terminal was finished. I am unsure whether the “busy” property is a reliable indicator.

Is there a better way to acheive what I want? Am I missing something?

Rob

– preemptively launch Terminal
tell application “System Events”
try
tell application process “Terminal”
get every window
end tell
on error
tell application “Terminal”
run
close window 1
end tell
end try
end tell

– Create new Terminal Window, Send commands and set Custom Title for Window
tell application “Terminal”
do script “cd Documents;ls”
tell window frontmost
set custom title to “I Hope This Works”
set title displays shell path to false
set title displays custom title to true
end tell
end tell

– Close Terminal Window when Done
try
tell application “Terminal”
repeat
tell (window every window whose name is “I Hope This Works”)
if not busy then
delay 5
close
exit repeat
else
delay 1
end if
end tell
end repeat
end tell
end try

AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Are you aware of the do shell script command? You could do your stuff directly from the AppleScript.

set L to do shell script "ls ~/documents/"

If you don’t want a new window in the Terminal, use “activate” instead of “launch”.

Thanks. Yes I am aware of the “do shell script” command. But I need to run some unix commands in a terminal. Due to the nature of the commands, the do shell script command is not suitable. (e.g. ripping audio using LAME. LAME provides the progress of the rip in the terminal window).

I also looked at activate. But “activate” ALSO opens up a new Terminal window if the Terminal app is NOT already running.

e.g. Try this script –
tell application “Terminal” to activate"

If the Terminal is not already running, a new Terminal window opens up when you run this script. Pity.

Rob