打开、拆分 iTerm2 窗口并在每个窗格中执行命令 [英] Open, split a iTerm2 window and execute commands inside each of those panes

查看:31
本文介绍了打开、拆分 iTerm2 窗口并在每个窗格中执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a script which would open a iTerm2 window, split it into 3 panes vertically and run a few commands inside each of those panes.

这是我目前的尝试:

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 在其中正确执行.窗口也被垂直拆分一次,因为我很确定当我尝试访问窗口的第二个窗格时它会停止执行.

The first part about creating the frontend seems to work as the window is correctly opened and the command cd ~/Documents/frontendDir is correctly executed in it. The window is also split vertically once as I'm pretty sure it stops executing when I try to access the second pane of my window.

我收到此错误:iTerm 收到错误:无法获取当前窗口的会话 2

提前致谢

推荐答案

iTerm session 对象包含在 tabs 中,而不是 窗口.因此,正如您在下面的脚本片段中看到的那样,我已经通过 current windowcurrent tab 引用了每个要写入的会话:

iTerm session objects are contained by tabs, not windows. Therefore, as you can see in the script snippet below, I've referenced each session to write to by way of the current tab of the 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

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

As far as I can tell, you cannot set the name property of a session; it gets set to the title of the session's frame. You can reference each session either by its index (as I've done here); its id property; or its tty property; all of which are unique values.

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

As you can see, the index appears to be ordered by a session's creation:

这篇关于打开、拆分 iTerm2 窗口并在每个窗格中执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆