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

时间:2014-03-11 23:59:12

标签: terminal applescript iterm osascript

以下内容适用于在iTerm 2中打开两个标签

我似乎无法弄明白如何使用split panes

我已尝试应用我在几个论坛上看到的内容,但它永远不会有效。有人能指出我正确的方向吗?

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

6 个答案:

答案 0 :(得分:20)

随着新的夜间建筑,这是非常好的。它似乎在公共版本中缺失,虽然它是在大约一年前实现的: Source - AppleScriptTest.m

tell application "iTerm"
    activate
    select first terminal 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

我不得不为此搜索太长时间,所以也许我可以帮助这个人(可能是我自己在几周内),因为这是我发现的第一件事。此解决方案甚至适用于激活的焦点跟随鼠标,在其他答案中缺少。

答案 1 :(得分:8)

正如Dom指出的那样,在新的2.9 beta版本中,其他答案将不再适用。由于无法自动执行此操作,我感到很沮丧所以我编写了一个兼容teamocil的命令行工具来完成这个工作:

https://github.com/TomAnthony/itermocil

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

答案 2 :(得分:5)

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

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

iTerm 2的AppleScript支持(截至iTerm 2 v1.0.0.201306220):

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

    < / LI>
  • 展示了一些奇怪的行为:当编译(在AppleScript编辑器中)tell "System Events" ...块中的tell application "iTerm"语句时,前缀在i term application之前,"System Events"被莫名其妙地插入 - 因为下面的代码未预先编译,所以此前缀,以避免将来出现问题。

    < / LI>
  • 错误/与其字典不一致:字典描述为exec命令 - 实际上不起作用 - 实际上是由{{{ 1}} command:它执行传递的参数或 - 如果参数有尾随空格 - 只需“输入”参数而不提交它。

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

限制,因为窗格不是字典的一部分:

  • 打开的另一个窗格不能命名为
  • 无法将命令发送到其他窗格
osascript

答案 3 :(得分:1)

如果它有用:我有类似的问题,想要在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本身。我怀疑这可能会缓解这个问题。)

答案 4 :(得分:1)

从@ 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

答案 5 :(得分:0)

好的,所以我终于弄明白了。

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

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"