[openbox] Launching applications

Anthony Thyssen A.Thyssen at griffith.edu.au
Mon Oct 3 08:29:59 EDT 2011


On Mon, 3 Oct 2011 12:54:03 +0200
Manolo Martínez <manolo at austrohungaro.com> wrote:
| Hello everyone,
| 
| I have a number of keybindings for launching common applications. Currently,
| say, A-F5 launches mutt in a urxvt terminal and, if I press that key
| combination twice, I get two mutt instances.
| 
| Is there any way to find out whether there is already a window running mutt
| somewhere and, if so, instruct A-F5 to raise and focus that window, instead of
| launching a new one?
| 

Now I have never used urxt, but I know X Window scripting.

If the launcher 'names' or even just 'titles' the instance of the
"urxvt" then you can simply do a fast search for that instance.

For example (after looking up its man page) make you "mutt launcher"
somethng like...

  urxt -name "mutt" -title "mutt" -e mutt &

Actually urxt could be xterm, aterm, or one of any X window term
programs as they all share the basic set of options (inherited from
the original xterm they are all based on)


Now to see if a "mutt terminal window" is present do any of these...

   xwininfo -root -all | grep '"mutt"'
   xwininfo -root -tree | awk '/"mutt"/ {print; exit}'
   xwit -names mutt -print
   xdotool search -limit 1 --classname mutt  getwindowgeometry -shell
   wmctrl -l | awk '$4 == "mutt"'
   xlsclients -l | grep " -name mutt "

All will print the X window ID of the window found.
Most (like xwit) will return a false status if it failed to find it.

NOTE: the first xwininfo is slower than the others which can limit its
search, and aborts as soon as it finds any one instance.

The xdotool while long gives fine control on the search and can perform
other actions on the window that is found.  The above generates shell
variable settings that can be 'eval'ed for ease of use.

The command "wmctrl -l" is very fast as it only searchs the top level
window nodes, saving an enormous amount of time.

The last xlsclients, is fastest of all, but does not list the XwindowID
of the client found, and only lists the actual launch command itself
(if provided by the application).


Example of the solution  -- launch a mutt if not present...

   xwit -names mutt -print >/dev/null ||
      urxt -name "mutt" -title "mutt" -e mutt &


WARNING: this may not help with fast double key presses, as it can still
take some time for a window to appear and settle, by which time
a second or third window could have been launched!

To prevent that you need a much more complex script that makes use of some
type of semaphore, such as a lock file. First try to create the lock, and
then do the search.  Only remove the lock if the application is found,
launched and setteled, or a timeout (failed to launch).



  Anthony Thyssen ( System Programmer )    <A.Thyssen at griffith.edu.au>
 --------------------------------------------------------------------------
  Do you know why this TARDIS is always rattling about the place...
  It was designed to have six pilots, and I've had to do it single
  handed.                             -- Dr Who 2008, "At Journey's End"
 --------------------------------------------------------------------------
   Anthony's Castle     http://www.ict.griffith.edu.au/anthony/


More information about the openbox mailing list