在mac终端中模拟键盘按下

发布于 2025-01-08 12:12:38 字数 78 浏览 1 评论 0原文

我正在运行一些模拟,需要手动键盘输入来更改参数(令人烦恼)。

有没有办法模拟键盘按下,以便我可以使用 bash 脚本运行模拟?

I'm running some simulations which require manual keyboard input to change the parameters (annoyingly).

Is there a way to simulate keyboard presses, so that I can run the simulations with a bash script?

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

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

发布评论

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

评论(5

刘备忘录 2025-01-15 12:12:38

虽然我不推荐它,但您可以执行类似的操作(它仅列出您的主目录的内容)

tell application "Terminal"
    activate
    do script "cd ~" -- the command to run
    delay 5 -- maybe throw in a delay to let the process start up
    tell application "System Events" to keystroke "ls -la" & return -- the keystrokes to simulate
end tell

但是这相当于训练猫在键盘上行走。该代码不知道终端中发生了什么。它只是“输入”一些内容并按回车键,完全不经意间。

因此,如果您有任何其他方式将输入传递给流程,请改用它。我刚刚发布了这个,因为您确实要求 AppleScript 解决方案。我只是怀疑 AppleScript 是正确的解决方案。

While I won't recommend it, you can do something like this (it just lists your home directory's contents)

tell application "Terminal"
    activate
    do script "cd ~" -- the command to run
    delay 5 -- maybe throw in a delay to let the process start up
    tell application "System Events" to keystroke "ls -la" & return -- the keystrokes to simulate
end tell

However this is the digital equivalent of training a cat to walk on your keyboard. The code has no clue what's going on in the terminal. It just "types" something and presses return, completely obliviously.

So if you have any other way of passing the input to the process, use that instead. I just posted this, since you did ask for an AppleScript solution. I just doubt that AppleScript's the right solution.

赠我空喜 2025-01-15 12:12:38

也许你可以使用expect

还有 echo |

Maybe you could use expect?

Also echo | <cmd>.

静谧幽蓝 2025-01-15 12:12:38

事实证明我一直可以从 bash 做:

./program << ENDINPUT
$input1
$input2
$input3
ENDINPUT

Turns out I could do from bash all along:

./program << ENDINPUT
$input1
$input2
$input3
ENDINPUT
节枝 2025-01-15 12:12:38

我是 cli-driver 的作者,它是一个 Node.js 库自动化命令行,类似于网络驱动程序,但在命令行中。它支持 mac、linux 和 windows,到目前为止,我能够轻松地“自动化”我需要的任何用例,从复杂的提示,到多个命令任务、ssh 会话、使用 tab 完成命令或使用 control+r 执行历史记录,甚至使用vi、nano、emacs。

它可以让您实际创建一个新终端并模拟用户输入(仅键盘)。一般来说,您需要模拟人类用户的击键(如字符),但在特定序列中,如“enter”、“shift-tab”、“control-q”、“control-shift-right”等。但更一般地说,任何 ansi 序列都可以编写用于控制光标、键盘和显示功能 - 尽管通常您不需要这些。

它在内部使用 node-pty,它是“node.js 的 forkpty(3) 绑定。这允许您使用伪终端文件描述符来分叉进程。它返回一个允许读取和写入的终端对象。”

不过,通信只是一种方式 - 输入文本并等待输出,例如(主页上有很多):

const client = await new Driver().start()
let output = await client.enterAndWait('ls -a', '..')
expect(output).not.toContain('tmpFile')
await client.enterAndWait('echo hello > tmpFile1.txt', d=>existsSync('tmpFile1.txt'))
await client.destroy()

I'm the author of cli-driver which is is a Node.js library to automate the command line, the analogy to web-driver but in the command line. It supports mac, linux and windows and so far I was able to easily "automate" any use case I needed, from complex prompts, to multiple command tasks, ssh sessions, command completion with tab or history with control+r, or even using vi, nano, emacs.

It let you actually create a new terminal and simulate user input (keyboard only) . In general you need to simulate a human user keystrokes like characters but in particular sequences like "enter", "shift-tab", "control-q", "control-shift-right", etc. But more generally any ansi sequence can written to control the cursor, keyboard and display capabilities - although in general you don't need that.

Internally it uses node-pty which is "forkpty(3) bindings for node.js. This allows you to fork processes with pseudoterminal file descriptors. It returns a terminal object which allows reads and writes."

The comunicacion is one way only though - you enter text and wait for an output, example (plenty on the home page):

const client = await new Driver().start()
let output = await client.enterAndWait('ls -a', '..')
expect(output).not.toContain('tmpFile')
await client.enterAndWait('echo hello > tmpFile1.txt', d=>existsSync('tmpFile1.txt'))
await client.destroy()
孤星 2025-01-15 12:12:38

我喜欢使用 robotsgo 来控制机器的各个方面: https://github.com/go-vgo/机器人Go

I like using robotgo to control all aspects of your machine: https://github.com/go-vgo/robotgo

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