Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I focus window by application / class name in XMonad

I'm trying to do the equivalent of

xdotool search "Chromium" windowactivate --sync key --clearmodifiers ctrl+r

i.e. I would like to switch to Chromium and reload the current page. However, with XMonad I get the following error:

Your windowmanager claims not to support _NET_ACTIVE_WINDOW, so the attempt to activate the window was aborted.

Is there a way in XMonad to programmatically switch to a certain application?

like image 318
Aton Avatar asked Aug 06 '15 19:08

Aton


1 Answers

Here's what I use, inspired by XMonad.Util.WindowBringer. You'll need to install the Text.PCRE package. Apologies if I forgot any import statements.

import XMonad
import qualified XMonad.StackSet as W
import XMonad.Util.NamedWindows (getName)
import Control.Applicative ((<$>))
import Data.List (find)
import Text.Regex.PCRE ((=~))

findWindow :: String -> X (Maybe Window)
findWindow regex = do
  wmap <- concat <$> (mapM mappings =<< (W.workspaces <$> gets windowset))
    :: X [(String, Window)]
  return (snd <$> find ((=~ regex) . fst) wmap)
  where mappings :: WindowSpace -> X [(String, Window)]
        mappings ws = mapM mapping $ W.integrate' (W.stack ws)
        mapping w = flip (,) w <$> show <$> getName w

warpTo :: String -> X ()
warpTo regex =
  findWindow regex >>= (flip whenJust $ windows . W.focusWindow)

In principle you should then be able to bind a key to warpTo "Chromium" >> spawn "xdotool key --clearmodifiers ctrl+r". However, this doesn't work for me, apparently due to some race condition I don't understand. Luckily, I find that the following works:

warpTo "Chromium" >> spawn "sleep 0.2; xdotool key --clearmodifiers ctrl+r"

You might be able to get away with a shorter delay.

like image 108
Matt Kramer Avatar answered Oct 22 '22 16:10

Matt Kramer