3

I am using a 21:9 Monitor and wrote a script to tile/align my windows to the left, centre and right side of the monitor, using keyboard-shortcuts:

┌─────────┬─────────┬─────────┐
│ window1 │ window2 │ window3 │
│ left    │ centre  │ right   │
│ aligned │ aligned │ aligned │
└─────────┴─────────┴─────────┘

The script works by getting the name of the currently focused window:
WINDOW_NAME=$(xdotool getwindowfocus getwindowname)
and aligns the window using its window name:
wmctrl -ir "$WINDOW_NAME" -e 1,$X_OFFSET,$Y_OFFSET,$X_WIDTH,$Y_HEIGHT

The problem is, when having several windows opened with same window name (e.g. 2 Firefox browsers or 3 Terminals etc.), it does not move the current focused window, but from all windows with the same name, the one which was opened the first.

I.e. Let's say I open 3 Terminals, and I want do align Terminal T2, the script will align T1 not T2, because T1 was opened first.

So my idea is instead of using the window names, using the process IDs (PIDs), because unlike the name of a window, the PID is unique. The question is: Is it possible to work with wmctrl using PIDs?

In the man pages of wmctrl I found the option -i which interprets the variable <WIN> as a number instead of a string-name.

$ man wmctrl 
[...]
    -e <MVARG>
      Resize  and  move  a  window  that  has been specified with a -r
      action according to the <MVARG> argument.
[...]
    -r <WIN>
      Specify a target window for an action.
[...]
    -i     
      Interpret window arguments (<WIN>) as  a  numeric  value  rather
      than  a  string name for the window. If the numeric value starts
      with the prefix '0x' it is assumed to be a hexadecimal number.
[...]

I don't really understand what this -i option means, and thought maybe this means it is possible to use the PID instead of the window-name:

WINDOW_ID=$(xdotool getactivewindow getwindowpid)
WINDOW_ID=$(printf 0x%x $WINDOW_ID)
wmctrl -ir "$WINDOW_ID" -e 1,$X_OFFSET,$Y_OFFSET,$X_WIDTH,$Y_HEIGHT

Unfortunately this didn't work (neither with decimal, nor as hex number).

Q1: Is it possible to use wmctrl with PIDs instead of window-names?
Q2: What does the -i option actually mean?

1 Answer 1

6

The -i option needs a window id, which is what you get if you just run

xdotool getactivewindow

which prints a decimal number like 20971543. So you can do

wmctrl -i -r "$(xdotool getactivewindow)" ...

But you don't need to do this as wmctrl accepts a pseudo-window id string of :ACTIVE: to mean the focused window, so you can do

wmctrl -r :ACTIVE: ...

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.