10

以下工作在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
4

6 回答 6

31

有了新的夜间版本,它非常好。尽管它是在大约一年前实现的,但在公共版本中似乎缺少它: 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

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

于 2015-02-14T03:55:38.467 回答
11

正如 Dom 所指出的,在新的 2.9 beta 版本中,其他答案将不再适用。我对无法自动执行此操作感到沮丧,因此我编写了一个与 teamocil 兼容的命令行工具,它可以做到这一点:

https://github.com/TomAnthony/itermocil

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

于 2015-08-13T18:03:35.893 回答
5

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

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

iTerm 2 的 AppleScript 支持(从 iTerm 2 v1.0.0.201306220 开始):

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

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

  • 错误/与其字典不一致:字典描述为exec命令的内容 - 实际上不起作用 - 实际上由write text命令完成:它执行传递的参数或 - 如果参数有尾随空格 - 只需“类型" 没有提交它的论点。

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

限制,由于窗格不是字典的一部分:

  • 无法命名打开的另一个窗格
  • 没有命令可以发送到另一个窗格
#!/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
于 2014-03-31T02:16:39.290 回答
2

从@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
于 2015-07-02T11:33:05.657 回答
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 本身。我怀疑这可能会缓解这个问题。)

于 2014-09-25T02:40:46.090 回答
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"
于 2014-03-29T01:55:51.223 回答