terminalapplescriptmacos-high-sierra

in tab 2 of front window returns Terminal got an error: Can’t get tab 2 of window 1. (-1728) since High Sierra update


As the title says I have an Apple Script that does:

in tab 2 of front window

...which used to work fine but since the High Sierra upgrade returns:

Terminal got an error: Can’t get tab 2 of window 1. (-1728)

Which corresponds to errAENoSuchObject I can't find any documentation around this having changed - is this a bug? Is there a new or better way to do this?


Solution

  • The object hierarchy has changed slightly. Each tab is referenced in AppleScript as tab 1 belonging to a unique parent window object.

    So, previously, if there were three tabs open in a single window, we could refer to them as tab 1, tab 2, and tab 3 of window 1. Now, we have tab 1 of window 1, tab 1 of window 2, and tab 1 of window 3.

    I’ve found the most convenient and reliable way to target a specific tab is to identify the window object that contains the tab object with a specific tty property value. I use a command that looks something like this:

        tell application "Terminal"
            get the id of the first window ¬
                whose first tab's tty contains "003"
    
            set w to result
            close window id w
        end tell
    

    If you want to get a slightly clearer picture of things, run this:

        tell application “Terminal” to ¬
            get every tab of every window
    

    and this:

        tell application “Terminal” to ¬
            get properties of every window
    

    and this:

        tell application “Terminal” to ¬
            get properties of tab 1 of every window