gnome-terminal

Identify gnome-terminal window by tab title


Assume the following:

I know that I opened the file ".bashrc" in vim somewhere. So a tab with the title ".bashrc (~) - VIM" is in one of the gnome-terminal windows. This tab might or might not be active tab in the window.

Is there any way to identify the gnome-terminal window, that holds that tab and to switch to it (preferably activating the tab)?

More general:

Is there any way to identify and activate a gnome-terminal window by the title of one of it's tabs?

or:

Is there any way to identify and activate a gnome-terminal window by the program currently executed in on of its tabs?


Solution

  • Revisiting this question I found a solution (or hack) to get what I want. Gnome Terminal implements the org.gnome.Shell.SearchProvider2 dbus interface. This interface can be used to search for terminal tabs that match a given term. By using a empty string as search term, all terminal tabs are matched and the ids are returned. With this ids it is possible to get the names (aka titles) of the tabs.

    I hacked together the following script to collect the information and using fzf to select the tab I'm looking for. Than this tab is activated.

    #!/bin/bash
    
    # get UUIDs of all gnome-terminal tabs
    get_term_ids()
    {
        # Call search with empty string ... will match all
        dbus-send --session --dest=org.gnome.Terminal --print-reply=literal \
            /org/gnome/Terminal/SearchProvider \
            org.gnome.Shell.SearchProvider2.GetInitialResultSet \
            array:string:"" \
            | tail -n 1 | sed 's/^  *//;s/ *]$//;s/  */,/g'
    }
    
    # get "uuid term-title" list
    get_term_titles()
    {
        ids="$(get_term_ids)"
        dbus-send --session --dest=org.gnome.Terminal  --print-reply=literal \
            /org/gnome/Terminal/SearchProvider \
            org.gnome.Shell.SearchProvider2.GetResultMetas \
            array:string:${ids%,} \
            | grep '^ *\(id\|name\) ' \
            | sed -e '/^  *id /{N;s/^ *id  *variant *\([^ ]*\) *)\n/\1/}' \
            -e 's/  *name  *variant  */ /;s/  *)$//'
    }
    
    # activate a term identified via uuid
    activate_term()
    {
        dbus-send --session  --dest=org.gnome.Terminal  --print-reply \
            /org/gnome/Terminal/SearchProvider \
            org.gnome.Shell.SearchProvider2.ActivateResult \
            string:$1 array:string:'' uint32:0 >/dev/null
    }
    
    result="$(get_term_titles | fzf --with-nth 2..)"
    if [ -z "$result" ]; then
        exit 1
    fi
    
    activate_term "${result%% *}"
    

    GNOME Shell Extension: https://github.com/rakus/gnome-shell-terminal-list