Output to terminal at multiple points in a shebang AppleScript

I have an AppleScript that begins with a shebang line and is marked executable so that I can run the script from the terminal (i.e., the osascript is the first line of the script, not typed to the terminal). At various points in the code I want to output a line of text to the terminal (ending in a LF). I don’t care what happens when the script is run from within Script Editor or Script Debugger, just when I run the script from the shell. I don’t want to see anything else except those lines, one after the other – no echoing of shell commands, no blank lines, no prompts, etc. I cannot wait for the script to complete before the output appears – I need to see them as they are encountered – and I don’t need input. (I know how to output text like this to the terminal this if I am willing to wait for the end of the script – I simply return the text i want to see as the result of the run handler.) I have tried many, many things, in bash, in AppleScript ‘do shell script.’, in AppleScript’s 'tell application “Terminal” to do script .", etc. Any ideas?

What you can do is send the pseudo terminal as an argument that’s been used by the terminal for teh script and the script can write data to there like this:

#!/usr/bin/osascript

on run argv
do shell script "echo 'hello world!' >" & quoted form of item 1 of argv 
delay 3
do shell script "echo 'intermediate message' >" & quoted form of item 1 of argv 
delay 2
do shell script "echo 'goodbye' >" & quoted form of item 1 of argv 
end run

you need to call the script like:

[format]./scriptname $(tty)[/format]

Maybe there is something wrong with my setup. I get no output with or without the $(tty), running in bash or tcsh, typing it exactly the way it is shown.

And in my experiments where I do get echo to print something, the echo command is printed too.

Piping to wc confirms that this is not a linefeed/cr problem, though I have in fact run into those too in my experimentation.

The tty command will return the path to the file descriptor to the current pseudo terminal. You can write data to it which will be shown in the terminal window. That means this will only work when the script is called from an pseudo terminal like the terminal.app. It doesn’t work from an non interactive shell because the tty command will return an error.

So this is what I did step by step in the terminal.

[format]$ cd ~/Desktop
$ nano testtty[/format]

The new file testtty wil be opened in nano text editor and paste the the following code into the editor.

#!/usr/bin/osascript

on run argv
do shell script "echo 'hello world!' >" & quoted form of item 1 of argv 
delay 3
do shell script "echo 'intermediate message' >" & quoted form of item 1 of argv 
delay 2
do shell script "echo 'goodbye' >" & quoted form of item 1 of argv 
end run

Then press CTRL+X followed by an ‘Y’ (it will ask confirmation if you want to safe the file).
Then I did the following two commands in the terminal to make it executable and then to run it:

[format]$ chmod +x testtty
$ ./testtty $(tty)[/format]

The reason I used nano is that because TextEdit.app is no longer an plain text editor anymore, any other plain text editor is fine.

Hey DJ,

TextEdit has never been a purely plain-text editor, but it can certainly create and edit plain-text documents.

Select “Make Plain Text” in the Format menu.

I much prefer using BBEdit of course. :cool:

-Chris

What I meant that the Cocoa Text System has been updated with it’s associated views. While in plain edit mode text editor was true plain text it is on modern system not. Since TextEdit does no handling by itself but entirely relies on Cocoa Text System it inherits these flaws even in plain text mode unfortunately.

The reason to avoid TextEdit in this topic was to make sure TextEdit isn’t messing up the code while copying and pasting from MS to the file. Any plain text editor is fine to use.

Old BBEdit user here, glad switched to ST 5 years ago :cool: