Gnome (Ubuntu): how to bring a program window to the front using a command line from the terminal?

19,826

Solution 1

I used to use wmctrl -a <name>, which works fine, but recently switched to xdotool, e.g.:

xdotool search --name <name-or-regex-for-name> windowraise

It has many other features too.

To install:

sudo apt-get install xdotool

Solution 2

Well, after sudo apt-get install wmctrl-ing, you can play with this bash script:

#! /bin/bash

WINTITLE="Mail/News" # Main Thunderbird window has this in titlebar
PROGNAME="mozilla-thunderbird" # This is the name of the binary for t-bird

# Use wmctrl to list all windows, count how many contain WINTITLE,
# and test if that count is non-zero:

if [ `wmctrl -l | grep -c "$WINTITLE"` != 0 ]
then
wmctrl -a "$WINTITLE" # If it exists, bring t-bird window to front
else
$PROGNAME & # Otherwise, just launch t-bird
fi
exit 0

Which I found here

Share:
19,826

Related videos on Youtube

GJ.
Author by

GJ.

Updated on September 17, 2022

Comments

  • GJ.
    GJ. over 1 year

    I have a certain work environment with dozens of open Windows. How can I bring to the front a window with a known name/title programatically or using the command line?

  • Dennis Williamson
    Dennis Williamson over 13 years
    No need for the brackets and backticks: if ! wmctrl -l | grep -q "$WINTITLE"
  • vlad-ardelean
    vlad-ardelean over 9 years
    wmctrl has a -i option, which supports working with the window with its hex identifier. And so you can do this wmctrl -lp|grep 'whatever incomplete name'|cut -d' ' -f1|xargs wmctrl -ai - which would do something similar
  • jozxyqk
    jozxyqk almost 9 years
    xdotool windowraise brings the window to the front but does not give focus to the window or switch to the desktop with the window. instead, windowactivate will do all three.