Applescript (osascript):在 iTerm 2 中打开拆分窗格并执行命令



以下工作在iTerm 2中打开两个选项卡

我似乎无法弄清楚如何使用拆分窗格来解决这个问题。

我尝试应用我在几个论坛上看到的内容,但它从未奏效。有人可以指出我正确的方向吗?

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 $projectsFolder"
end tell
launch session "Default session"
tell the last session
set name to "Console"
write text "cd $projectsFolder"
end tell
end tell
end tell
eof

新的夜间构建相当不错。公共版本中似乎缺少它,尽管它是在大约一年前实现的: 来源 - AppleScriptTest.m

tell application "iTerm"
activate
select first window

# Create new tab
tell current window
create tab with default profile
end tell

# Split pane
tell current session of current window
split vertically with default profile
split vertically with default profile
end tell

# Exec commands
tell first session of current tab of current window
write text "cd ~/Developer/master-node"
write text "coffee"
end tell
tell second session of current tab of current window
write text "gulp w"
end tell
tell third session of current tab of current window
end tell
end tell

我不得不为此寻找太久,所以也许我可以帮助某人解决这个问题(可能是几周后我自己),因为这是我发现的第一件事。该解决方案甚至适用于激活的焦点跟随鼠标,这在其他答案中是缺失的。

正如 Dom 指出的那样,在新的 2.9 beta 版本中,其他答案将不再有效。我对无法自动化这一点感到沮丧,所以我编写了一个与teamocil兼容的命令行工具,该工具正是这样做的:

https://github.com/TomAnthony/itermocil

它允许您为预配置的窗口和窗格集编写 YAML 文件,这些窗口和窗格可以为给定项目运行预定义的命令集。

更新:iTerm2v3有了很大的改进,但不兼容的 AppleScript 支持- 请参阅 https://www.iterm2.com/documentation-scripting.html

为@Joel自己的答案提供一些背景:

iTerm 2 的 AppleScript 支持(自 iTerm 2 v1.0.0.201306220 起):

  • 不完整:不支持脚本拆分窗格- 因此 OP 采用发送击键的次优技术。

  • 表现出一些奇怪的行为:在tell application "iTerm"块内编译(在 AppleScript 编辑器中)tell "System Events" ...语句时,前缀i term application莫名其妙地插入在"System Events"之前 - 由于下面的代码没有预编译,因此不包括此前缀以避免将来出现问题。

  • 错误/与其字典不一致:字典描述为exec命令的内容 - 实际上不起作用 - 实际上是由write text命令实现的:它执行传递的参数或 - 如果参数有尾随空格 - 只是"键入"参数而不提交它。

以下是基于解决方法(发送击键)的拆分窗格的解决方案 - 通过osascript调用AppleScript代码的bash脚本:

由于窗格不是字典的一部分而造成的限制:

  • 无法命名打开的另一个窗格
  • 无法将任何命令发送到其他窗格
#!/usr/bin/env bash
projectFolder="$HOME/Desktop" # example
osascript <<-EOF
tell application "iTerm"
tell (make new terminal) # Create a new pseudo terminal...
tell (launch session "Default session") # ... and open a session (window)
# Name the new window (its original pane).
set name to "Server"
# Execute the 'cd' command in the original pane.
write text "cd '$projectFolder'"
# Create a new split pane, horizontally, by sending ⌘⇧-D 
tell application "System Events" to keystroke "d" using {shift down, command down}
# !! Note: We canNOT:
#  - name this pane separately
#  - execute a command in it.
# Return to the original pane, by sending ⌘-[ 
tell application "System Events" to keystroke "[" using {command down}
end tell
end tell
end tell
EOF

从@mklement0开始,这是我打开一个新选项卡的脚本,分为 4 个面板并运行命令:

#!/usr/bin/env bash
osascript <<-EOF
set cmds to {"rabbitmq-server", "mongod", "redis-server", "htop"}
tell application "iTerm"
activate
set myterm to (current terminal)
tell myterm
launch session "Default Session"
# split vertically
tell application "System Events" to keystroke "d" using command down
delay 1
# previus panel
tell application "System Events" to keystroke "[" using command down
delay 1
# split horizontally
tell application "System Events" to keystroke "d" using {shift down, command down}
delay 1
# next panel
tell application "System Events" to keystroke "]" using command down
delay 1
# split horizontally
tell application "System Events" to keystroke "d" using {shift down, command down}
set n to count of cmds
repeat with i from 1 to n
# next panel
tell application "System Events" to keystroke "]" using command down
delay 1
tell the current session to write text (item i of cmds)
end repeat
end tell
end tell  
EOF

如果有帮助:我有类似的问题,希望在 iTerm 中使用组合键快捷方式来拆分窗格并让新窗格继承原始会话的标题。我想出了以下内容,它解决了这个问题,并且较少依赖发送击键(尽管我很想完全消除它们)。

tell application "iTerm"
tell the current terminal
tell the current session
set the_name to get name
tell i term application "System Events" to keystroke "d" using {command down, shift down}
end tell
tell the current session
set name to the_name
end tell
end tell
end tell

我正在使用BetterTouchTool将一个组合键(即cmd+')绑定到此AppleScript的执行中。(我发现某些组合键会很糟糕,我会天真地猜测,因为您有效地将键组合放在脚本发送的任何击键之上。我还没有追问如何在iTerm本身的首选项中定义键盘快捷键。我怀疑这可能会缓解这个问题。

好的,所以我终于想通了。

通过向应用程序发送击键,可以打开和导航拆分窗格。

tell i term application "System Events" to keystroke "D" using command down
tell i term application "System Events" to keystroke "]" using command down

将命令发送到拆分窗格并命名每个窗格的示例。我用它来启动我的节点应用程序。

write text "cd $projectsFolder/$2.m"
write text "/usr/local/bin/frontend.sh $1 $2"
tell i term application "System Events" to keystroke "D" using command down
tell i term application "System Events" to keystroke "]" using command down
set name to "$2.api"
write text "cd $projectsFolder/$2.api"
write text "/usr/local/bin/backend.sh $1 $2"

相关内容

  • 没有找到相关文章

最新更新