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

Joe*_*oel 9 terminal applescript osascript iterm

以下工作在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
Run Code Online (Sandbox Code Playgroud)

Dom*_*Dom 23

随着新的夜间建设,这是非常好的.它似乎在公共版本中缺失,虽然它是在大约一年前实现的: 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
Run Code Online (Sandbox Code Playgroud)

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

  • 这对我有用。我确实必须将“选择第一个终端窗口”更改为“选择第一个窗口” (3认同)

Tom*_*ony 9

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

https://github.com/TomAnthony/itermocil

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

  • 好极了!没有Applecriptz!为我效劳 (2认同)

mkl*_*nt0 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通过osascript以下方式调用AppleScript代码的脚本:

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

  • 无法命名打开的另一个窗格
  • 没有命令可以发送到其他窗格
#!/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
Run Code Online (Sandbox Code Playgroud)


Joe*_*oel 0

好吧,我终于弄清楚了。

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

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

tell i term application "System Events" to keystroke "]" using command down
Run Code Online (Sandbox Code Playgroud)

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

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"
Run Code Online (Sandbox Code Playgroud)