Shell command to bring a program window in front of another?

17,451

Solution 1

Note:

  • More information about xdotool can be found here.

Solution 2

Another option is xdotool:

xdotool search --class Nautilus windowactivate

Solution 3

When using xdotool, it seems difficult to bring to front all windows for a given application or class using only one command. I end up having better results by wrapping it in a for loop at the shell level. Using Bash:

for WINDOW in $(xdotool search --desktop 0 Firefox); do
   xdotool windowactivate ${WINDOW}
done

Few remarks:

  • By default, xdotool search will search the pattern (here Firefox) in window name, class, and classname. If you want to restrict your search space, use the relevant --class, --name or --classname options.
  • The --desktop 0 option limits the search to the first desktop. This seems to be a workaround to avoid the XGetWindowProperty[_NET_WM_DESKTOP] failed (code=1) mentioned in some comments.
  • At the time of this writing, the xdotool project is stalled since 2015. It still remains my tool of choice though. For personal reasons, Jordan Sissel (the original author) is not as active as he was, so don't hesitate to contribute to the project.
Share:
17,451

Related videos on Youtube

OverStacked
Author by

OverStacked

Updated on September 17, 2022

Comments

  • OverStacked
    OverStacked over 1 year

    Does a shell command exist to bring a already started program in gnome in the front of another.

    i.e.:

    Gedit and Nautilus are started. Nautilus is in the background and Gedit in the foreground.

    How to i bring Nautilus in the front with a shell command?

  • frabjous
    frabjous over 13 years
    Sure edit your post just as I'm composing mine... :( )
  • Tino
    Tino over 7 years
    Fails for me with XGetWindowProperty[_NET_WM_DESKTOP] failed (code=1)
  • Tino
    Tino over 7 years
    In my case it was possible to raise a window using the ID from xwininfo and issuing several additional different xdotool commands (which I no more remember). When I tried to automate that (to get rid of xwininfo) I got visual artifacts due to otherwise hidden windows which were selected, too. I simply found no generic way to address arbitrary windows correctly. After switching to wmctrl -a everything immediately worked flawlessly out of the box without any further tweaking. Please note that I like xdotool, but it seems to be difficult to raise just the correct window with it.
  • jorfus
    jorfus almost 6 years
    You can find your window name with wmctrl -l The name is the text after the last dash: <window ID> <desktop ID> <client machine> <window title>
  • Allanrbo
    Allanrbo almost 4 years
    xdotool search --desktop 0 Nautilus windowactivate did it for me
  • Haggra
    Haggra over 3 years
    If you want to bring up all the windows of an application, instead of a for loop you can use %@ which applies it to all windows. Also, you'll want --onlyvisible option to avoid fake or invisible windows. For example, xdotool search --onlyvisible --class firefox windowactivate %@ will bring up every firefox window. If you omit %@, you'll only get the first one. You can choose which one you want by changing @ to a positive integer such as %2. Make sure you remember the --onlyvisible, especially if you don't use %@ or you probably won't get any windows.