开发者

Using AppleScript to Send Commands and Strings to Terminal.app with Echo Off

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        <--\
user开发者_如何学Goname     <----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.


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?


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜