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

Sar*_*and 3 macos applescript iterm2

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

\n\n

这是我迄今为止的尝试:

\n\n
tell application "iTerm2"\n  activate\n\n  -- Create main window\n  create window with default profile\n\n  tell current session of current window\n    set name to "frontend"\n    write text "cd ~/Documents/frontendDir"\n    split vertically with default profile\n  end tell\n\n  tell second session of current window -- ERROR HERE\n    set name to "backend"\n    write text "cd ~/Documents/backendDir"\n    split vertically with default profile\n  end tell\n\n  tell third session of current window\n    set name to "apollo-server"\n    write text "cd ~/Documents/GitHub/apolloDir"\n  end tell\nend tell\n
Run Code Online (Sandbox Code Playgroud)\n\n

关于创建的第一部分frontend似乎可以正常工作,因为窗口已正确打开并且命令cd ~/Documents/frontendDir在其中正确执行。该窗口也被垂直分割一次,因为我很确定当我尝试访问窗口的第二个窗格时它会停止执行。

\n\n

我收到此错误:iTerm got an error: Can\xe2\x80\x99t get session 2 of current window

\n\n

提前致谢

\n

CJK*_*CJK 5

iTerm session对象包含在tabs,而不是windowscurrent tab因此,正如您在下面的脚本片段中看到的,我通过以下方式引用了要写入的每个会话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
Run Code Online (Sandbox Code Playgroud)

据我所知,你不能设置namea 的属性session;它被设置为会话框架的标题。您可以session通过其索引来引用每个(就像我在这里所做的那样);其id财产;或其tty财产;所有这些都是独特的价值。

正如您所看到的,索引似乎是按会话的创建排序的:

macOS 上的 iTerm