1

what I want to accomplish is something like the one described in this this question. Basically using AppleScript to send commands to the Terminal.app.

However there's this behavior that I don't want: every command sent using do script directive is echoed to the Terminal. I am currently integrating an AppleScript with Cocoa, and sometimes the software would send sensitive information such as password to the Terminal.

Is there some way to disable this behavior, such as @echo off directive in DOS batch files?

EDIT

To clarify my question, I will elaborate more. Suppose we have an AppleScript such as this one:

tell application "Terminal"
    set currentTab to do script "login"
    do script "username" in currentTab
    do script "password" in currentTab
end tell

I noticed that if the Terminal application is already running, with or without any terminal window open, the commands in the do script directive will be echoed before it is fed to the shell. To illustrate the result of the above script in a Terminal:

Last login: Tue 5 Apr hh:mm:ss on ttys001
login        <--\
username     <----unwanted echoes
password     <--/
<machine>:~ <user>$ login
username: username
password: ****
... (interactive Terminal session)

This doesn't happen however, if the Terminal.app is not running at script execution.

Community
  • 1
  • 1
avee
  • 766
  • 1
  • 13
  • 22

2 Answers2

1

You're "typing" things before the shell has a chance to respond to them (in this case, before login has a chance to turn off echoing). Tools such as expect solve the problem of scripting arbitrary command line utilities, which would be a better solution in the general case, but it's not clear from your question what you're trying to do.

What command are you trying to script, and why are you doing it via Terminal?

Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124
  • I'm trying to script ssh login from a Cocoa application. That's why I need the Terminal.app. – avee Apr 05 '11 at 03:38
  • @avee: If you do not use Expect, you will likely just end up reimplementing some bits of its functionality: “wait for "login:"; send username; wait for "assword:"; send password; …” – Chris Johnsen Apr 05 '11 at 06:23
  • @Chris Johnsen @Nicholas Riley: Thanks, I think I will take a look at Expect – avee Apr 05 '11 at 06:41
0

To hide the commands in Terminal.app first run this command:

stty -echo

To show the commands again:

stty echo

Instead of using Terminal.app you can also run commands directly from AppleScript:

set theResult to do shell script "cal -y 2011"

Or even better, run the commands directly from Objective-C with NSTask.
(Since your app is using Cocoa I assume it has been (partially) written in Objective-C)

Anne
  • 26,765
  • 9
  • 65
  • 71
  • Thank you for the answer but this is not exactly what I was asking about. I have edited my question to clarify what I meant. `do shell script` would not work because I needed an interactive shell. Using NSTask would not work either for the same reason. – avee Apr 05 '11 at 02:39