Home > bash, ubuntu > Find window by its name and activate it (bring to foreground)

Find window by its name and activate it (bring to foreground)

Problem
From a script, I wanted to bring a specific window to the foreground.

Solution
There is a command-line X11 automation tool called “xdotool”. You will have to install it via apt-get. This command can do a lot, it’s worth checking out its man page.

Find the window ID of a window by its window title (example):

$ xdotool search --name "Rocket Launcher"
37748739

You can also use regular expressions.

Put the focus on this window and bring it in the foreground, i.e. activate it (example):

xdotool windowactivate 37748739

Other goodies
Window ID of the currently active window (example) #1:

$ xdotool getactivewindow
50331675

Window ID of the currently active window (example) #2:

$ xdpyinfo | grep focus
focus:  window 0x300001b, revert to PointerRoot

(Where 0x300001b == 50331675). “xdpyinfo” can also give you information about the X.Org version, number of screens, screen dimensions, etc.

Get window information (example):

$ xwininfo -id 50331675

xwininfo: Window id: 0x300001b "jabba : bash – Konsole"

  Absolute upper-left X:  0
  Absolute upper-left Y:  24
  Relative upper-left X:  0
  Relative upper-left Y:  0
  Width: 1920
  Height: 1032
  Depth: 32
  Visual: 0x23
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x3000001 (not installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +0+24  -0+24  -0-24  +0-24
  -geometry 1920x1032+0+24
Advertisements
  1. No comments yet.
  1. January 10, 2013 at 08:40

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: