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.

tatsu
  • 3,107

4 Answers4

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.

  • I'm more interested in wayland desktops. (it does work on wayland) – tatsu Apr 19 '19 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 '19 at 12:26
  • Some xdotool functions still work under wayland: https://askubuntu.com/questions/956640/equivalent-to-xdotool-for-wayland. There is a project to create wodotool a Wayland equivalent to xdotool: https://github.com/whot/woodotool – WinEunuuchs2Unix Apr 19 '19 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 '19 at 12:31
  • 1
    @tatsu I've designed a more conventional method and revised answer. – WinEunuuchs2Unix Apr 19 '19 at 13:45
  • thanks for your work, however, you may have what I want confused. maybe running the actual script in question yourself would allow you to see what I'm aiming for : https://github.com/tatsujb/installFAFscript a steam username and password you can try it with is 'tatsu_unofficial' & 'thetruejester' although you'll still require steamguard. I could give it to you in pm – tatsu Apr 19 '19 at 13:58
  • as per my opening post "(the fact that this tab focuses is intended behavior and the most important)" if first terminal doesn't atomatically switch to new tab or window that is a dealbreaker. what I want now is for the tab to also switch back after the nth command (in this case after optaining sudo priveleges with sudo echo) and this should also be automatic – tatsu Apr 19 '19 at 14:01
  • I added two lines to code where Parent window steals back focus from child (second-script.sh) process. I've started Wayland research but not many people seem interested in supporting this platform. – WinEunuuchs2Unix Apr 19 '19 at 15:43
  • I'm not sure where you dug that up. Wayland's raison d'être is replacing Xorg. it's backed not only by fedora but also by canonical, who gave up on mir. Wayland is the default on any release of any distro worth it's salt that dates back less than a year. anyways I'll look into using your solution. – tatsu Apr 19 '19 at 16:27
  • I meant not many people seem to want to write a xdotool version for Wayland. Wayland itself seems to think such functionality a security risk. But that could be they haven't time to write it as they work on nVidia GPU support or whatever they consider more urgent. – WinEunuuchs2Unix Apr 19 '19 at 16:44
3

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;
Mohit Malviya
  • 586
  • 2
  • 9
  • 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 '19 at 00:03
  • Yes, it's because the $winname is not passed in the new terminal. I edit the script please try the new one. – Mohit Malviya Apr 22 '19 at 06:22
  • winname = wmctrl: option requires an argument -- 'a' also does not switch back – tatsu Apr 22 '19 at 10:09
  • the option requires an argument error is came because we do not pass the value of winname in the new terminal. – Mohit Malviya Apr 22 '19 at 10:11
  • so how do we fix that? – tatsu Apr 22 '19 at 12:30
  • Are you trying the edited script? And can you post the console output of script. – Mohit Malviya Apr 22 '19 at 13:03
  • i get winname = tatsu@pc ~ I guess it fails to switch me back to the right tab when I purposefuly open a new tab after starting the script because all the new tabs have the same name. this could be so easily solved if ubuntu would allow us to custom name current tab. – tatsu Apr 23 '19 at 07:41
  • But as I say earlier Custom Title for gnome-terminal is deprecated. You have to switch to any other terminal then. – Mohit Malviya Apr 23 '19 at 09:55
  • So what I asked for is entirely impossible with gnome-terminal? :( – tatsu Apr 23 '19 at 11:56
  • This unfortunately doesn't work for wayland. For alternatives: https://askubuntu.com/questions/956640/equivalent-to-xdotool-for-wayland – tomodachi Apr 20 '22 at 08:32
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

icezyclon
  • 466
  • 4
  • 14
  • 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 '19 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 '19 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 '19 at 12:42
  • Yea, I see what you mean. Sorry, I found no way to cycle to a specific tab in normal terminal. I myself use tmux, where it is possible to go to a specific tab and even send keystrokes directly to tabs and windows...

    Why does it need to be a tab anyway? Couldn't it be a new terminal? Then you could simply use wmctrl to switch to the terminal with the correct name

    – icezyclon Apr 19 '19 at 12:44
  • tab looks slicker but I'm willing to cut corners here if tabs are the issue. again though the main tab from which the base of the script was run doesn't have a name and naming terminals isn't possible on some other terminals, this is supposed to be a cross-OS script. on elementary OS for example you can't name terminals in io.elementary.terminal – tatsu Apr 19 '19 at 12:45
  • 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 '19 at 12:50
  • PID works fine for me. – tatsu Apr 19 '19 at 12:50
  • my_pid="echo $$"; gnome-terminal --title="install dependencies" -- bash -c 'echo "hello"; sleep 3; echo "focusing parent tab..."; wmctrl -p my_pid; sleep 100' it doesn't focus it. – tatsu Apr 19 '19 at 13:03
  • 1
    Yea, you can't change the title in gnome-terminal (only in tab)... I'm out of ideas, sorry. – icezyclon Apr 19 '19 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";

`&
Mohit Malviya
  • 586
  • 2
  • 9
  • 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 '19 at 13:09