打开,拆分 iTerm2 window 并在每个窗格中执行命令

Open, split a iTerm2 window and execute commands inside each of those panes

我正在尝试创建一个脚本来打开 iTerm2 window,将其垂直拆分为 3 个窗格,并在每个窗格中 运行 一些命令。

这是我目前的尝试:

tell application "iTerm2"
  activate

  -- Create main window
  create window with default profile

  tell current session of current window
    set name to "frontend"
    write text "cd ~/Documents/frontendDir"
    split vertically with default profile
  end tell

  tell second session of current window -- ERROR HERE
    set name to "backend"
    write text "cd ~/Documents/backendDir"
    split vertically with default profile
  end tell

  tell third session of current window
    set name to "apollo-server"
    write text "cd ~/Documents/GitHub/apolloDir"
  end tell
end tell

关于创建 frontend 的第一部分似乎有效,因为 window 已正确打开并且命令 cd ~/Documents/frontendDir 在其中正确执行。 window 也被垂直分割一次,因为我很确定当我尝试访问我的 window.

的第二个窗格时它会停止执行

我收到此错误:iTerm got an error: Can’t get session 2 of current window

提前致谢

iTerm session objects 包含在 tabs 中,而不是 windows。因此,正如您在下面的脚本片段中看到的那样,我引用了每个 session 以通过 current window:

current tab 写入
tell application "iTerm"
    activate

    set W to current window
    if W = missing value then set W to create window with default profile
    tell W's current session
        split vertically with default profile
        split vertically with default profile
    end tell
    set T to W's current tab
    write T's session 1 text "cd ~/Desktop"
    write T's session 2 text "cd ~/Downloads"
    write T's session 3 text "cd ~/Documents"
end tell

据我所知,您无法设置 sessionname 属性;它被设置为 session 框架的标题。您可以通过索引引用每个 session(就像我在这里所做的那样);它的 id 属性;或其 tty 属性;所有这些都是唯一值。

如您所见,索引似乎是由 session 创建的: