macos 使用 Applescript 将命令和字符串发送到 Terminal.app
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1870270/
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
Sending commands and strings to Terminal.app with Applescript
提问by Petruza
I want to do something like this:
我想做这样的事情:
tell application "Terminal"
activate
do script "ssh [email protected]"
-- // write user's password
-- // write some linux commands to remote server
end tell
For example to log in to the server, enter the password, and then login to mysql and select a DB.
I type that every day and it would be reallyhelpful to bundle it into a script.
比如登录服务器,输入密码,然后登录mysql,选择一个DB。
我每天都输入它,将它捆绑到脚本中会非常有帮助。
Also, is there a reference of what commands, properties, functions, etc. do applications (Terminal, Finder, etc) have available to use within Applescript? thanks!
此外,是否有应用程序(终端、Finder 等)可以在 Applescript 中使用的命令、属性、功能等的参考?谢谢!
EDIT: Let me clear this up: I don't want to do several 'do script' as I tried and doesn't work. I want to open a Terminal window, and then emulate a human typing in some characters and hitting enter. Could be passwords, could be commands, whatever, just sending chars to the Terminal which happens to be running ssh. I tried keystroke and doesn't seem to work.
编辑:让我澄清一下:我不想像我尝试过的那样做几个“执行脚本”但不起作用。我想打开一个终端窗口,然后模拟人类输入一些字符并按回车键。可能是密码,也可能是命令,等等,只是将字符发送到恰好运行 ssh 的终端。我试过击键,但似乎不起作用。
回答by CRP
First connect to the server and wait for 6 seconds (you can change that) and then execute whatever you need on the remote server using the same tab
首先连接到服务器并等待 6 秒(您可以更改它),然后使用相同的选项卡在远程服务器上执行您需要的任何内容
tell application "Terminal"
set currentTab to do script ("ssh user@server;")
delay 6
do script ("do something remote") in currentTab
end tell
回答by Petruza
As EvanK stated each do script line will open a new window however you can run
two commands with the same do script by separating them with a semicolon. For example:
正如 EvanK 所说,每个 do 脚本行都会打开一个新窗口,但是您可以
使用相同的 do 脚本运行两个命令,方法是用分号分隔它们。例如:
tell application "Terminal"
do script "date;time"
end tell
But the limit appears to be two commands.
但限制似乎是两个命令。
However, you can append "in window 1" to the do script command (for every do script after the first one) to get the same effect and continue to run as many commands as you need to in the same window:
但是,您可以将“in window 1”附加到 do script 命令(对于第一个之后的每个 do 脚本)以获得相同的效果并继续在同一窗口中运行尽可能多的命令:
tell application "Terminal"
do script "date"
do script "time" in window 1
do script "who" in window 1
end tell
Note that I just used the who, date, and time command as an example...replace
with whatever commands you need.
请注意,我只是将 who、date 和 time 命令用作示例...替换为您需要的任何命令。
回答by Dan
Here's another way, but with the advantage that it launches Terminal, brings it to the front, and creates only one window.
这是另一种方式,但它的优势在于它启动终端,将它带到前面,并且只创建一个窗口。
I like this when I want to be neatly presented with the results of my script.
当我想整齐地呈现我的脚本结果时,我喜欢这个。
tell application "Terminal"
activate
set shell to do script "echo 1" in window 1
do script "echo 2" in shell
do script "echo 3" in shell
end tell
回答by Allen Robel
How about this? There's no need for key codes (at least in Lion, not sure about earlier), and a subroutine simplifies the main script.
这个怎么样?不需要关键代码(至少在 Lion 中,之前不确定),并且子程序简化了主脚本。
The below script will ssh to localhost as user "me", enter password "myPassw0rd" after a 1 second delay, issue ls, delay 2 seconds, and then exit.
下面的脚本将以用户“me”的身份ssh到本地主机,延迟1秒后输入密码“myPassw0rd”,发出ls,延迟2秒,然后退出。
tell application "Terminal"
activate
my execCmd("ssh me@localhost", 1)
my execCmd("myPassw0rd", 0)
my execCmd("ls", 2)
my execCmd("exit", 0)
end tell
on execCmd(cmd, pause)
tell application "System Events"
tell application process "Terminal"
set frontmost to true
keystroke cmd
keystroke return
end tell
end tell
delay pause
end execCmd
回答by user1984702
You don't need to "tell" Terminal to do anything. AppleScript can do shell scripts directly.
你不需要“告诉”终端做任何事情。AppleScript 可以直接做 shell 脚本。
set theDir to "~/Desktop/"
do shell script "touch " & theDir &"SomeFile.txt"
or whatever ...
管他呢 ...
回答by K. Biermann
Why don't use expect:
为什么不使用期望:
tell application "Terminal"
activate
set currentTab to do script ("expect -c 'spawn ssh user@IP; expect \"*?assword:*\"; send \"MySecretPass
\"; interact'")
end tell
回答by Nathan Pitman
Kinda related, you might want to look at Shuttle (http://fitztrev.github.io/shuttle/), it's a SSH shortcut menu for OSX.
有点相关,你可能想看看 Shuttle ( http://fitztrev.github.io/shuttle/),它是 OSX 的 SSH 快捷菜单。
回答by pizzaFace
Your question is specifically about how to get Applescript to do what you want. But, for the particular example described, you might want to look into 'expect' as a solution.
您的问题特别是关于如何让 Applescript 做您想做的事。但是,对于所描述的特定示例,您可能希望将“期望”作为解决方案。
回答by user1019711
The last example get errors under 10.6.8 (Build 10K549) caused by the keyword "pause".
最后一个示例在 10.6.8 (Build 10K549) 下出现由关键字“pause”引起的错误。
Replacing it by the word "wait" makes it work:
用“等待”这个词代替它使它起作用:
tell application "Terminal"
activate
my execCmd("ssh me@localhost", 1)
my execCmd("myPassw0rd", 0)
my execCmd("ls", 2)
my execCmd("exit", 0)
end tell
on execCmd(cmd, wait)
tell application "System Events"
tell application process "Terminal"
set frontmost to true
keystroke cmd
keystroke return
end tell
end tell
delay wait
end execCmd
回答by user1019711
Petruza,
彼得鲁萨,
Instead of using keystroke use key code.
The following example should work for you.
而不是使用击键使用键码。
以下示例应该适合您。
tell application "System Events"
tell application process "Terminal"
set frontmost to true
key code {2, 0, 17, 14}
keystroke return
end tell
end tell
The above example will send the characters {d a t e}
to Terminal and then
keystroke return will enter and run
the command. Use the above example
with whatever key codes you need
and you'll be able to do what you're trying to do.
上面的示例将字符 {date} 发送到终端,然后
按键返回将输入并运行命令。将上面的示例
与您需要的任何关键代码一起使用,您将能够做您想做的事情。