1

I am aware that this question is duplicated:
How to set window size and location of an application on screen via command line?
Resizing a maximized window from the command line

I was able to solve my question with the first link. But I am struggling to learn Bash language and I would like to know what is going wrong with the following script:

#!/bin/bash

xfce4-settings-manager & sleep 0.1
xwininfo -name "xfce4-settings-manager" | grep xwininfo: | awk {'printf$4'}
wmctrl -i -r $1 -b add,maximized_vert,maximized_horz &

After running the script I check "xwininfo" takes another ID Value of the app. I don't understand why. Can someone give me a hand with this issue?

Thanks in advance.

2
  • 1
    Your second line runs xwininfo but doesn't save the output anywhere -- it'll display to your screen. The wmctrl line uses the $1 parameter to the script; are you passing something in, or do you expect the xwininfo command to be providing that input to wmctrl?
    – Jeff Schaller
    Mar 4, 2017 at 14:03
  • @Jeff: Thanks for replying. I want the result of the second line is retrieved by $1 of the third line. How could I do that? I tried to define a variable for the second line ($WID, for instance) and retrieve in the third line, but It did not work. Can you tell me how I could do that? Thanks.
    – joshsoj
    Mar 4, 2017 at 15:28

4 Answers 4

3

The tool you're looking for is Devil's Pie (either version 1 and version 2, note that they use completely different configuration languages). There are a number of examples on this site, e.g. 1 2 3 4.

The problem with your script has already been identified by Jeff Schaller in a comment: the line that extracts the output from xwininfo doesn't plug into the line that calls wmctrl. To use the output of a command in a script, use a command substitution.

windowid=$(xwininfo -name "xfce4-settings-manager" | awk '$1 == "xwininfo:" {printf $4}')
wmctrl -i -r "$windowid" -b add,maximized_vert,maximized_horz &

I used an intermediate variable to keep the line from becoming unreadably long, but you can do it all inline:

wmctrl -i -r "$(xwininfo -name "xfce4-settings-manager" | awk '$1 == "xwininfo:" {printf $4}')" -b add,maximized_vert,maximized_horz &

I also combined the grep and awk invocations, I find it simpler to use a single tool here.

You don't need to use xwininfo to find the window ID: wmctrl can do it, just pass it the window name.

wmctrl -r "xfce4-settings-manager" -b add,maximized_vert,maximized_horz &
1
  • Thanks. It worked with "Devil's Pie" app, but it is still not working with the script. It happens the same as I explained in the first message: xwininfo takes another value of the window. I will explained with an example in a new message (look at the example below).
    – joshsoj
    Mar 5, 2017 at 9:26
1

Since you want:

the result of the second line is retrieved by $1 of the third line

you could:

#!/bin/bash

xfce4-settings-manager & sleep 0.1
wid=$(xwininfo -name "xfce4-settings-manager" | grep xwininfo: | awk {'printf$4'})
wmctrl -i -r "$wid" -b add,maximized_vert,maximized_horz &

though if I could suggest another version (untested, as I'm not in front of an X display):

#!/bin/bash

xfce4-settings-manager & 
while ! pgrep xfce4-settings-manager > /dev/null
do
  :
done
wid=$(xwininfo -name "xfce4-settings-manager" | awk '/xwininfo:/ { print $4 }')
wmctrl -i -r "$wid" -b add,maximized_vert,maximized_horz

Changes:

  • I'm not sure that wmctrl has to be put in the background, so I removed that &
  • I combined the awk and grep, because awk can "grep"
  • I changed the sleep 0.1, which assumes that xfce4-settings-manager starts in that amount of time, into a loop that asks pgrep to look for the process instead
1
  • Thanks. It worked with "Devil's Pie", but it is still not working with the script. It happens the same as I mentioned earlier. I will try to explain with an example below)
    – joshsoj
    Mar 5, 2017 at 9:29
0

Thanks for replying. I tried to do what @Gilles and @Jeff explained on the above comments, but the script does not work. It has the same problem that I mentioned in my first message.

Let me explain what is happening (line by line):

  1. The script starts "xfce4-settings-manager"

    USER@PC:~$ xfce4-settings-manager & sleep 0.1 ;

[3] 9825

  1. It requires the value of the "xfce4-settings-manager" window. In this example, I only want the output result. In the script I wrote it as Gilles and Jeff told me.

    USER@PC:~$ xwininfo -name "xfce4-settings-manager" | awk '$1 == "xwininfo:" {printf $4}' 0x5400001

  2. I include the window value in the last command (0x5400001), but the window does not maximize.

    USER@PC:~$ wmctrl -i -r 0x5400001 -b add,maximized_vert,maximized_horz

[4] 9852

  1. I run "xwininfo" command and select the "xfce4-settings-manager" window and this is its result:

    USER@PC:~$ xwininfo

xwininfo: Please select the window about which you would like information by clicking the mouse in that window.

xwininfo: Window id: 0x5800003 "Settings"

Absolute upper-left X: 343

Absolute upper-left Y: 286

Relative upper-left X: 5

Relative upper-left Y: 19

Width: 680

Height: 195

Depth: 24

Visual: 0x20

Visual Class: TrueColor

Border width: 0

Class: InputOutput

Colormap: 0x22 (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: +343+286 -343+286 -343-287 +343-287

-geometry 680x195+338+267

[2] Done xfce4-settings-manager

[4]+ Done wmctrl -i -r 0x5400001 -b add,maximized_vert,maximized_horz

The window value of "xfce4-settings-manager" is different depending of retrieving the value from the command line or from selecting the window with the cursor (0x5400001 & 0x5800003).

Any ideas?

0

evince has window titles equal to the PDF file titles, and no word evince in it... so another aproach to try is to wait for a while (sleep 0.4 for evince was enough), and then just maximize the last window in the wmctrl -l list (i.e., last added window) like this:

evince file.pdf & sleep 0.4; wmctrl -i -r "$(wmctrl -l | tail -1 | fgrep -o '^\S+')" -b add,maximized_vert,maximized_horz

This works with ANY app, though you must be sure that you have waited enough for it to be brought up to wmctrl

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .