Shell Script : Focus already open terminal tab from second terminal tab

this may be a bit obscure but I'm trying to do something a bit tricky :

I'm writing a script that opens up a new tab with commands, I'd like to switch back to first tab after the user has input to the secondary tab (the fact that this tab focuses is intended behavior and the most important) :

echo "new tab is about to open..."
gnome-terminal --tab --active --title="install dependencies..." -- bash -c 'echo "hello";
#other commands;
read user_input;
#now I want to switch back to first tab while this tab works in the backgroud;
command;
command;
command;
command;
command;
command;
command;'

echo "hello welcome back to the correct tab, we have been waiting for you! :)"
read new_user_input;

is such a thing possible?

I had this idea because clearly the --active argument calls something that brings the new tab into focus (and even works if the tab calling this is not active). so something of the nature must be possible.


Solution 1:

Manual Method

As per this Q&A: Is there a hotkey to switch between tabs in the default terminal app?

After opening the new tab you can return to the previous tab with Ctrl + pg up

Automated Method within script

In order to send the signal to Bash Shell (gnome-terminal) use xdotool:

sudo apt install xdotool

In your script issue this command:

xdotool key Control+Page_Up

I've just finished installing and testing on Ubuntu 16.04 LTS and it works perfectly. It should work on all X11 desktops.


More conventional method

OP desires Wayland support and more importantly a POSIX method that works with many *NIX distributions.

From this Q&A:

  • Script to open terminal and show the output of the running commands

... comes this command:

gnome-terminal -e 'bash -c "sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade; exec bash"'

We will modify the example command to look like this:

gnome-terminal -e 'bash -c "second-script.sh; exec bash"'

This is what second-script.sh looks like:

#!/bin/bash

# AU question: https://askubuntu.com/questions/1134625/shell-script-focus-already-open-terminal-tab-from-second-terminal-tab/1135206#1135209

echo "Welcome to the second script."
echo " Run command 1."
sleep 1
echo " Run command 2."
sleep 1
read -n 1 -s -r -p "Press any key to continue"

touch /tmp/second-script-user-ack # signal parent to continue commands there
echo " Run command 3."
sleep 5
echo " Run command 4."
sleep 5

# Now terminates.
exit 0

Always remember to marks scripts executable using:

chmod a+x second-script.sh

Note: This opens a second terminal window that stays open until user closes it. This can be changed to auto-close.

Our first (Parent) script will look like this:

#!/bin/bash

# AU question: https://askubuntu.com/questions/1134625/shell-script-focus-already-open-terminal-tab-from-second-terminal-tab/1135206#1135209

file=/tmp/second-script-user-ack
if [ -f $file ] ; then
    rm $file
fi

window=$(xdotool getactivewindow)
# Launch second script running in background, don't wait
gnome-terminal -e 'bash -c "second-script.sh; exec bash"' &&

while true ; do
    if [ -f $file ] ; then
        break
    fi
done

xdotool windowactivate $window

echo "Child still running. Ready to run extra Parent Commands"
read -n 1 -s -r -p "Press any key to continue"
echo
echo " Parent Command 1."
sleep 5
echo " Parent Command 2."
sleep 5

echo "Parent program completed"
read -n 1 -s -r -p "Press any key to continue"

exit 0

Advantages of current approach:

  • You can see both windows running at same time. With second tab you can't see both scripts output.
  • After child process (second-script.sh) completes output is still visible until window is closed.

Wayland tools

There aren't many Wayland tools to control active windows.

I did find this: ydotool but you'll need to compile it by hand. There is no Debian/Ubuntu installation package. There is an Arch Linux installation package which some of your users can use.

Solution 2:

Well, it consumes more time then I expected. Finally, I manage to get what you want. Try this script in this I get the window name with the use of 'xdotool' and switch to the active window using 'wmctrl'.

#!/bin/bash

echo "new tab is about to open..."

winname=`xdotool getactivewindow getwindowname;`

gnome-terminal --tab --active --name="install dependencies..." -- bash -c 'echo "hello";
read new_user_input;
#other commands;
echo "winname = "$1;
wmctrl -a $1;
sleep 5;
' bash "$winname"

echo "hello welcome back to the correct tab, we have been waiting for you! :)"

read new_user_input;

Solution 3:

You can try simulating keystrokes to change to your other tab:

You can use xdotool to simulate any keystroke combination. To change tab in gnome-terminal use Ctrl + Page_Up (go one tab back). So xdotool key --clearmodifiers Ctrl+Page_Up will change to your old tab.

To make sure that the focus of this keystroke is the terminal I used wmctrl which can be used like this: wmctrl -a "install dependencies".

So a command to switch back to your previous tab would be:

wmctrl -a "install dependencies" && xdotool key --clearmodifiers Ctrl+Page_Up

You can install those tools using sudo apt-get install wmctrl xdotool

Solution 4:

Setting the title of terminal windows is removed from gnome-terminal. So I tried this on the rox terminal. You need to install this two software:-

apt install roxterm
apt install wmctrl

I can manage to get what you want. Try this script for testing.

#!/bin/bash


roxterm --title="First Tab is this" -- bash -c `

echo "new tab is about to open..."

roxterm --title="Second Tab is This" -- bash -c 'echo "hello";' &
wmctrl -a "First Tab is this";

`&