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

1

6

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?

Chestnut answered 6/8, 2015 at 19:45 Comment(0)
S
1

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.

Scandal answered 6/2, 2018 at 5:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.