Search code examples
pythonmacosapplescriptiterm2

Determine the window name/number in iTerm2


I am using iTerm2 on MacOS (Sierra). I have multiple instances of iterm2 running, each has a title that are prefixed with a number which increments with each running window.

I would like to run a shell command to return this number on the command line, does any one know how to get this information?

I am looking for something like:

$ iterm_get_number()
2

Solution

  • I was able to get the names using applescript, the following script dumps a list of the open iTerm windows. Since I am interested in using these names in a python script I included a small snippet for doing this as well.


    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
    

    python for extracting information from above script

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