确定iTerm2中的窗口名称/编号

问题描述:

我在MacOS(Sierra)上使用iTerm2。我有多个运行iterm2的实例,每个实例都有一个以每个运行窗口递增的数字作为前缀的标题。确定iTerm2中的窗口名称/编号

我想运行一个shell命令在命令行上返回这个数字,是否有人知道如何获取这些信息?

我要寻找类似:

$ iterm_get_number() 
2 

我能得到使用AppleScript的名字,下面的脚本转储开放的iTerm窗口的列表。因为我有兴趣在python脚本中使用这些名称,所以我也包含一个小的代码片段。


get_window_names.applescript

on run 
    set returnNames to {} 
    tell application "iTerm" 
     repeat with theWindow in windows 
      tell theWindow 
       set end of returnNames to get name 
      end tell 
     end repeat 
    end tell 
    return returnNames 
end run 

蟒用于从上面的脚本中提取信息

out = subprocess.check_output(['osascript', 'get_window_names.applescript']) 
print [x.strip() for x in out.split(',')]