3

After playing a little from the terminal with wmctrl, now comes the next step. Execute wmctrl from a bash script.

I do achieve to execute wmctrl in a bash when it is completely alone.

Example:

#!/bin/bash

 SOUDIR=/usr/bin
 echo "$SOUDIR"
 $SOUDIR/wmctrl -i -r 0x040000b3 -e 0,0,0,500,800

But when I add the firefox line code it does not work. I though that adding sleep time would work but I was wrong.

This is the current code:

#!/bin/bash

firefox http://www.google.de
sleep 30s
SOUDIR=/usr/bin
echo "$SOUDIR"
$SOUDIR/wmctrl -i -r 0x040000b3 -e 0,0,0,500,800

How to resize the web browser using this bash code?

I am new to bash and ubuntu.

  • What are you trying to do, exactly? How are you obtaining the window id 0x040000b3? – steeldriver Jun 24 '15 at 12:03
  • I just want to resize the web browser. That is the ID of the web browser (in this case firefox). Just by typing wmctrl -l you obtain this ID. – kaka Jun 24 '15 at 12:05
1

OK couple of points:

  • You don't need the /usr/bin added. Shell will find wmctrl by itself. That's for very specific cases that may involve portability.
  • Add ampersand at the end of firefox command. The script will proceed only if the previous command exits. Since firefox doesn't exit, the script waits for it.
  • As Oli pointed out, better way is to use class name. I've done multiple scripts with wmctrl where i find hex id of the window by referencing class name.

Personally I'd do it this way:

#!/bin/bash
firefox http://www.google.de &
sleep 3
WM_ID=$(wmctrl -lx | awk '$3~/Navigator.Firefox/ {print $1}')
wmctrl -i -r $WM_ID -e 0,0,0,500,800

Here we open firefox , let it run in background, then we extract hex id of the window that matches class Navigator.Firefox. In wmctrl -lx field 3 is wm_class, and field 1 is hex id. awk helps us to extract field 1 here. Finally we reference that numeric id, inside the final wmctrl statement

0

It seems likely that the firefox command would block the script from progressing until it exited. Simply push it into the background and your script can progress:

firefox http://www.google.de &

On a tangential note, you might also want to look at the -no-remote for Firefox. This will stop the command interacting with any existing Firefox instances in your DISPLAY.

While we're talking tangents, the firefox command has a few helper arguments that --along with -no-remote— can help you target the new window.

For instance, you can set a specific class name and then have Compiz handle the resizing automagically. And wmctrl -x <WM_CLASS> will let you target a class rather than an ID. Probably more robust than targeting Window names or IDs too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.