The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 3,277β€…    Votes:  7β€…
Tags: command-line   scripts   gnome-terminal  
Link: πŸ” See Original Answer on Ask Ubuntu ⧉ πŸ”—

URL: https://askubuntu.com/q/1135206
Title: Shell Script : Focus already open terminal tab from second terminal tab
ID: /2019/04/19/Shell-Script-_-Focus-already-open-terminal-tab-from-second-terminal-tab
Created: April 19, 2019    Edited:  June 12, 2020
Upload: April 8, 2024    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


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:


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.

⇧ Is there any shortcut for parent folder in console? How to Switch Focus to a Gnome Terminal Tab via Script or Shell Command  β‡©