有没有一种方式与Applescript在terminal启动多个标签,并执行命令对他们?
我有一个项目,要求我在terminal中启动多个服务来同时运行和监视,并希望自动执行启动过程。
一种select是使用系统事件来发送创build新选项卡所需的按键,但是限制是必须打开通用访问中的辅助设备,并且可能需要添加视线延迟。
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
据我可以从terminal脚本字典 – 你只能从标签检索信息,但不能创build新的,如你可以与Windows(例如运行脚本“清除”
看起来你可以使用GUI脚本。 (因为terminal的字典知道制表符是windows的元素,所以我希望能够制作新的制表符,但是显然是坏的)。和Chealion的答案一样,必须打开辅助设备的访问权限,但Apple提供了一个脚本,可以让你检查它在这里。
http://www.apple.com/applescript/uiscripting/
Matthew Lambie提供了使用AppleScript的GUI脚本在Terminal.app中创build选项卡的示例:
http://lambie.org/2007/11/03/tabs-in-terminal-using-applescript-on-leopard/
对于Chealion的回答,这个答案会更好,但是我还没有代表发表评论。
我不知道如何获得标签,但这会让你多个窗口:
set commands to {"ls", "pwd", "cd /tmp; ls"} repeat with com in commands tell application "Terminal" activate do script with command com end tell end repeat
更多
iTerm具有良好的脚本支持 。 法新社548有一个例子, 在不同的标签中打开多个ssh会话 。 [我怀疑这会比GUI脚本更容易做,也更可靠。]
我知道你问了Terminal,但是真的 – 你应该使用iTerm 🙂
这是从iTerm的同事写的脚本:
#!/bin/sh osascript <<-eof tell application "iTerm" set myterm to (make new terminal) tell myterm launch session "Default session" tell the last session set name to "Server" write text "cd \"$PROJECT_DIR\"" write text "script/server" end tell launch session "Default session" tell the last session set name to "Console" write text "cd \"$PROJECT_DIR\"" write text "script/console" end tell end tell end tell eof
它启动一个带有多个选项卡的新的iTerm窗口,并在其中执行一些小命令(用于Rails开发)。
我创build了一个脚本化的方法,允许用户使用基本上覆盖所有苹果脚本的new_tab(title, command) shell函数从相同的Terminal.app窗口创build和标题新的选项卡。
我的文章与代码和一些背景可以在这里find:以编程方式创build&标题选项卡在Mac OS X Terminal.app
我希望这会让别人的生活更加自动化 🙂