Can I make an Applescript output to stdout while it runs?

Conceptually, what I’d like to do is something like this:

repeat with x from 1 to 10
   write to stdout x
   -- do something time consuming
end repeat

And then, when I execute the above script via osascript in a Terminal window, see…

1
(pause)
2
(pause)
3

…etc., come through to the Terminal window. “log” doesn’t work. (In fact, when using osascript, as far as I can tell “log” sends information straight to the bit bucket.)

The only stdout output I seem to be able to get from an Applescript is the final “result” value from the script. I’d really like to get feedback via stdout from a script the whole time the script is running, not only one final value when it terminates.

Is this possible? If so, how?

This is not what you wish, but may help:

tell application "Terminal" 
    do script "echo 'Initializing...'" --> should open a new window
     set theWin to window 1
end tell
delay 3
repeat with i from 1 to 5
	tell application "Terminal" to do script "echo " & i in theWin
	delay 1
end repeat
tell application "Terminal" to do script "exit" in theWin

Good idea, but no, it won’t help for what I’m doing.

What I’m trying to do is this: I want to have a cross-platform Java app – not a Cocoa Java app – run an Applescript which controls iTunes, and get feedback on the progress of that script.

In its current version, when this app runs on Windows, it uses Runtime.exec to execute a JavaScript, and the script talks to iTunes via the iTunes COM SDK. So that the Java app can report on progress of the script, the script uses WScript.Echo to send progress info out to stdout, which the Java app can see and report as the script goes along.

I’d like to be able to use Runtime.exec on the Mac to perform the shell command “osascript myItunesScript” and similarly see progress as the script executes via stdout (or stderr would do). As it looks right now, about the only feedback I get via stdout is the last result value when the script is completely finished executing.

If I can’t get anything else reported to stdout, I’ll have to resort to something ugly (as if Runtime.exec isn’t ugly enough already!) like writing progress status to a temp file which the Java app reads to see what’s going on.

Of course, if anyone knows how to get a (mostly) pure Java app to more directly do Applescripting or AppleEvents, that’d be even better.

This works for me:

repeat with x from 1 to 10
	
	tell application "Terminal"
		tell window 1
			activate
			tell application "System Events"
				keystroke (x as string) & return
			end tell
		end tell
	end tell
	
	delay 1
	
end repeat

Kind regards,
Thomas

Browser: Firefox 1.5.0.1
Operating System: Mac OS X (10.4)

This version is more improved and behaves better:

tell application "Terminal"
	
	set name of window 1 to "script"
	
	repeat with x from 1 to 3
		tell window "script"
			activate
			tell application "System Events"
				keystroke (x as string) & return
			end tell
		end tell
		delay 1
	end repeat
	
end tell

-- Prevent from executing the lines echoes before
tell window "script"
	activate
	tell application "System Events"
		keystroke "C" using control down
	end tell
end tell

And this one seems to be perfect :smiley: :

(*

log2terminal 1.0 

This script allows AppleScripts executed by the 'osascript'
command to send messages to the Terminal window the script
has been launched from.

To prevent the Terminal from trying to execute all logged
lines when the script is finished, you are strictly
encouraged to use the _endlog() handler at the end of your script!

This script was written by Thomas Kuehner, [url=http://www.macgix.de]www.macgix.de[/url].
Feel free to use it as you like.

*)

on run
	
	_beginlog()
	
	_log("Hello world!")
	_log("")
	_log("I like counting...")
	_log("")
	repeat with _n from 1 to 3
		_log(_n)
		delay 1
	end repeat
	_log("")
	
	_endlog()
	
end run

-- Initialize Terminal window
on _beginlog()
	tell application "Terminal"
		tell window 1
			set title displays custom title to true
			set title displays device name to false
			set title displays shell path to false
			set title displays file name to false
			set custom title to "script"
		end tell
	end tell
end _beginlog

-- Log to specific window
on _log(x)
	
	tell application "Terminal"
		tell window "script"
			set frontmost to true
                        activate
			tell application "System Events"
				keystroke (x as string) & return
			end tell
		end tell
	end tell
	
end _log

-- Send a break signal to prevent from executin logged lines
on _endlog()
	
     tell application "Terminal"
	tell window "script"
		set frontmost to true
                activate
		tell application "System Events"
			keystroke "C" using control down
		end tell
	end tell
     end tell
	
end _endlog

Njoy!