使用 AppleScript 在 Echo Off 的情况下将命令和字符串发送到 Terminal.app

发布于 2024-10-30 04:48:53 字数 1032 浏览 4 评论 0原文

我想要完成的事情类似于 这个问题。基本上使用 AppleScript 将命令发送到 Terminal.app。

但是,我不希望出现这种行为:使用 do script 指令发送的每个命令都会回显到终端。我目前正在将 AppleScript 与 Cocoa 集成,有时该软件会将密码等敏感信息发送到终端。

是否有某种方法可以禁用此行为,例如 DOS 批处理文件中的 @echo off 指令?

编辑

为了澄清我的问题,我将详细说明。假设我们有一个像这样的 AppleScript:

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

我注意到,如果终端应用程序已经在运行,无论打开或没有打开任何终端窗口,do script 指令中的命令都会在输入之前进行回显到外壳。为了说明上述脚本在终端中的结果:

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

但是,如果 Terminal.app 在脚本执行时未运行,则不会发生这种情况。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如若梦似彩虹 2024-11-06 04:48:53

您在 shell 有机会响应之前“键入”内容(在本例中,是在 login 有机会关闭回显之前)。 expect 等工具解决了编写任意命令行实用程序脚本的问题,在一般情况下这将是更好的解决方案,但从你的问题中并不清楚你想要做什么。

您尝试编写什么命令脚本,为什么通过终端执行此操作?

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?

欲拥i 2024-11-06 04:48:53

要隐藏 Terminal.app 中的命令,请首先运行此命令:

stty -echo

要再次显示命令:

stty echo

除了使用 Terminal.app,您还可以直接从 AppleScript 运行命令:

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

或者更好的是,使用 NSTask 直接从 Objective-C 运行命令。< br>
(由于您的应用程序正在使用 Cocoa,我假设它是(部分)用 Objective-C 编写的)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文