13

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.

4 Answers 4

7

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:

... 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.

10
  • I'm more interested in wayland desktops. (it does work on wayland)
    – tatsu
    Apr 19, 2019 at 12:20
  • what about a POSIX-compliant solution. if possible this should work on all dirtributions. this is for a shell script. this keybinding is not necessarily set this way.
    – tatsu
    Apr 19, 2019 at 12:26
  • Some xdotool functions still work under wayland: askubuntu.com/questions/956640/…. There is a project to create wodotool a Wayland equivalent to xdotool: github.com/whot/woodotool Apr 19, 2019 at 12:28
  • why not something more robust? what if the user clicks the tab himself as is currently required of him? then the previous tab becomes the "install dependencies" tab and it goes back to that. the user will be understandably confused and take a while to realize he did indeed manage to switch tabs but the script switched him back. also this keybinding is probably not functional on most other distributions.
    – tatsu
    Apr 19, 2019 at 12:31
  • 1
    @tatsu I've designed a more conventional method and revised answer. Apr 19, 2019 at 13:45
3
+200

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;
10
  • if, after running the script you open a new tab, then switch back to "install dependencies.." and type something in you end up on the third tab (the one I opened manually) and are not greeted with "hello welcome back to the correct..." unless you manually switch back to the first tab. I also tried with extra quotes arround the winname variable, like this : wmctrl -a '$winname'; this did not help.
    – tatsu
    Apr 21, 2019 at 0:03
  • Yes, it's because the $winname is not passed in the new terminal. I edit the script please try the new one. Apr 22, 2019 at 6:22
  • winname = wmctrl: option requires an argument -- 'a' also does not switch back
    – tatsu
    Apr 22, 2019 at 10:09
  • the option requires an argument error is came because we do not pass the value of winname in the new terminal. Apr 22, 2019 at 10:11
  • so how do we fix that?
    – tatsu
    Apr 22, 2019 at 12:30
2

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

9
  • the "install dependencies" tab is the one that's supposed to have the call to bring back to the tab that opened it. I imagine in your above post wmctrl -a "install dependencies" && xdotool key --clearmodifiers Ctrl+Page_Up was not meant to be part of the "install dependencies" tab?
    – tatsu
    Apr 19, 2019 at 12:36
  • Yes, but the problem of xdotool is that it sends global keystrokes. So whatever program is the focus of the user at the moment will get the keystrokes. So to make sure that the terminal gets those, first put focus on the terminal. And because the terminal's name is "install dependencies", this will be seen in wmctrl -l. (in case you have multiple terminals open). The user might be doing something else at the moment...
    – icezyclon
    Apr 19, 2019 at 12:39
  • hmm weird it does switch back either way. the issue I have is that it suffers both from going to the tab to the left, which in my test case with 3 tabs where i ran my script from the second, brought my script back to the third. also if I click another tab while it's waiting to execute the switch command, it chooses the wrong tab as well. here's a test you can try gnome-terminal --tab --active --title="install dependencies" -- bash -c 'echo "hello"; sleep 2;wmctrl -a "install dependencies" && xdotool key --clearmodifiers Ctrl+Page_Up; sleep 100'
    – tatsu
    Apr 19, 2019 at 12:42
  • 1
    I have never used this feature, but it could be possible to focus terminals by their PID? I would have to take a closer look at that...You can use ` echo $$ ` to get the PID of current shell. Then use wmctrl -p to change to pid.
    – icezyclon
    Apr 19, 2019 at 12:50
  • 1
    Yea, you can't change the title in gnome-terminal (only in tab)... I'm out of ideas, sorry.
    – icezyclon
    Apr 19, 2019 at 13:11
1

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";

`&
1
  • sorry the script installing a new terminal breaks scope of my script. users want to be able to run my script with their own terminal, as is understandable.
    – tatsu
    Apr 19, 2019 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .