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

时间:2019-05-08 09:15:03

标签: macos applescript iterm2

我正在尝试创建一个脚本,该脚本将打开一个iTerm2窗口,将其垂直分成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的第一部分似乎可以正常运行,因为窗口已正确打开,并且命令cd ~/Documents/frontendDir在其中正确执行。该窗口也被垂直分割一次,因为当我尝试访问窗口的第二个窗格时,我确定它会停止执行。

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

预先感谢

1 个答案:

答案 0 :(得分:0)

iTerm session对象包含在tabs中,而不是windows中。因此,如您在下面的脚本片段中所看到的,我引用了每个会话,它们是通过current tab的{​​{1}}进行写入的:

current window

据我所知,您无法设置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 的{​​{1}}属性;它被设置为会话框架的标题。您可以按其索引来引用每个name(就像我在这里所做的那样);其session属性;或其session属性;所有这些都是唯一的值。

如您所见,索引似乎是由会话的创建排序的:

iTerm on macOS