Xdotool – Window Stack

J

Jarret W. Buse

Guest
Xdotool – Window Stack

As you have seen, if you read the other Xdotool articles, this tool can be very useful. The use of the Window Stack helps to increase its abilities for performing actions on multiple windows.

There are three commands which retrieves Window IDs. These three commands can get more than one ID. When an ID is found by these three commands, the IDs are stored in the Window Stack. The IDs can then be retrieved for use by other commands. The three commands which place Window IDs in the Window stack are:

  1. search
  2. getwindowfocus
  3. getactivewindow
Of course, you may notice that the search command is the main command for retrieving multiple Window IDs.

Now, let's look at an example. If I have three LibreOffice Writer applications open and I find that the Class name for the Writer is 'libreoffice', then I can perform a search for them by:

xdotool search --class libreoffice

The result is three Window IDs as follows:

14680152
14731975
14733803


Within the same command-line, these IDs will remain in the Window Stack. Once my command ends, the stack is deleted. Chaining is covered in the article Xdotool – Mouse.

Now, let's assume I want to get the Window Name of the first item only. To reference the first item in the stack use '%1'. You should note that the second item is '%2' and so on. To reference all items in the stack, use '%@'. To see the title of the first item in the Stack, I can perform the following command:

xdotool search --class libreoffice getwindowname %1

In the command, the first part of the chain performs the search and fills the Window Stack. The second command uses the first item in the Stack for the 'getwindowname' command. The output is:

xDoTool - Window Stack.doc - LibreOffice Writer

I can also list every item in the Window Stack with the following command:

xdotool search --class libreoffice getwindowname %@

The output would be:

xDoTool - Window Stack.doc - LibreOffice Writer
ls.txt - LibreOffice Writer
raw.txt - LibreOffice Writer


Any command which uses the 'window_id' as an option for a command can use the Window Stack. All of the commands read from the stack, but the three listed previously write to the Window Stack. The commands which read from the Stack are:

  • key
  • type
  • mousemove
  • click
  • getwindowpid
  • getwindowname
  • windowsize
  • windowmove
  • windowfocus
  • windowunmap
  • windowmap
  • windowminimize
  • windowkill
  • set_window
  • windowactivate
  • set_desktop_for_window
  • get_desktop_for_window

Let's look back a minute at the use of '%1'. When a 'window_id' is required for a command, but it is not specified, then the '%1' is considered the default or even the current window. It is best to always specify the Window ID or else, use the '%1' variable to specify the use of the Window Stack.

If no items are in the stack and a '%1' variable is used, an error is generated as follows:

There are no windows in the stack
Invalid window ' %1'


Two commands were not covered in the other xdotool articles. These commands deal with the Window Stack. The two commands are:

  • windowraise
  • windowreparent

The windowraise command specifies which window to raise to the foreground. It uses the '%n' value, or it can use the window_id. The syntax is:

windowraise [window_id or %n]

The windowraise command can be used to place windows over the top of windows to stack them on the screen.

If I have three windows, Window 1, 2 and 3 and the three are in the Windows stack as %1, %2 and %3, then I can place them in the order of 2,3,1 by using the following commands:

xdotool windowraise %1 windowraise %3 windowraise %2

NOTE: The number place the order as top (1), middle (2) and bottom (3)

The syntax for the windowreparent command is:

windowreparent source_window_id destination_window_id

The windowreparent command causes the source window to be placed over the destination window as if it were a child window. The windows can also be specified by the Window ID or by using the Window Stack's variable of '%1', '%2', etc.

Let's make an example by closing all Firefox windows. To start, we need to create the Window Stack of all the open Firefox windows. Once the stack is made, we can close all of the windows by using the 'windowkill' command.

With Firefox, we can find that the classname is 'Firefox'. So, the search command to fill the Window Stack is:

search --classname Firefox

Once the stack is filled, we then use the 'windowkill' command to kill the windows in the stack using the following:

windowkill %@

NOTE: Be sure that your search does not add more to the stack than you need. If you need more added to the stack, perform two or more searches in a row.

The whole command would then be:

xdotool search --classname Firefox windowkill %@

Play around with the Window Stack and I will make another article to put everything together and make some interesting experiments.
 

Attachments

  • slide.jpg
    slide.jpg
    50.5 KB · Views: 125,621

Members online


Latest posts

Top