问题描述
我正在尝试创建一个脚本,该脚本将打开一个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.
到目前为止,这是我的尝试:
Here's my attempt so far:
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 got an error: Can’t get session 2 of current window
预先感谢
推荐答案
iTerm session
对象包含在tabs
而不是windows
中.因此,如您在下面的脚本片段中所看到的,我已经引用了每个会话,这些会话是通过current window
的current 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
据我所知,您无法设置session
的name
属性;它被设置为会话框架的标题.您可以按其索引引用每个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窗口并在每个窗格中执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!