bash 在 mac 终端中模拟键盘按下
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9417946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simulate keyboard presses in mac terminal
提问by Eddy
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?
有没有办法模拟键盘按下,以便我可以使用 bash 脚本运行模拟?
采纳答案by Eddy
Turns out I could do from bash all along:
原来我可以从 bash 一直做:
./program << ENDINPUT
$input1
$input2
$input3
ENDINPUT
回答by Flambino
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
Howeverthis 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 rightsolution.
因此,如果您有任何其他方式将输入传递给流程,请改用它。我刚刚发布了这个,因为您确实要求提供 AppleScript 解决方案。我只是怀疑 AppleScript 是正确的解决方案。
回答by cancerbero
I'm the author of cli-driverwhich 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.
我是cli-driver的作者,它是一个用于自动化命令行的 Node.js 库,类似于 web-driver 但在命令行中。它支六、纳米、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.
它让您实际创建一个新终端并模拟用户输入(仅限键盘)。一般来说,您需要模拟人类用户的击键,例如字符,但特别是“enter”、“shift-tab”、“control-q”、“control-shift-right”等序列。但更普遍的是,任何 ansi 序列都可以编写用于控制光标、键盘和显示功能 - 尽管通常您不需要它。
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."
在内部,它使用 node-pty,它是“node.js 的 forkpty(3) 绑定。这允许您使用伪终端文件描述符 fork 进程。它返回一个允许读取和写入的终端对象。”
The comunicacion is one way only though - you enter text and wait for an output, example (plenty on the home page):
comunicacion 只是一种方式 - 您输入文本并等待输出,例如(主页上有很多):
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()

