You are not logged in.

#1 2008-12-10 19:14:53

Ashren
Member
From: Denmark
Registered: 2007-06-13
Posts: 1,226
Website

Xmswitch - A script to enable mouse based WS switching in Xmonad.

Hi,

I have created a script to enable mouse based workspace switching in Xmonad. I posted an earlier draft in the dzen and xmonad hacking thread, but the script below has several advantages compared to my earlier script. The foremost advantage is that you only need to enter names of your workspaces and what font you're using and the script will figure out what to do when you click your workspace tab.

What you'll need is the following:

* dzen2 (which is used as a statusbar and includes the gadget "textwidth" / pacman -S dzen2)
* xdotool (to send keystrokes to Xmonad / pacman -S xdotool)
* getcurpos (to get cursorposition - get the code here: http://dzen.geekmode.org/dwiki/doku.php … e-position )

You may need adjust the padding for your specific font.

Here is the script:

#!/bin/bash

#####################################################################################################################
# Workspace switcher for Xmonad using cursor position to determine which workspace to switch to.                     
# You'll need getcurpos from here: http://dzen.geekmode.org/dwiki/doku.php?id=misc:xget-mouse-position               
# and xdotool http://www.semicomplete.com/projects/xdotool/                                                          
# and dzen2 http://dzen.geekmode.org/dwiki/doku.php as statusbar in Xmonad where you'll bind this script to button1. 
#####################################################################################################################

# By ashren
# WTFPL
#
# Get and store cursor position.

XP=$(getcurpos | cut -d" " -f1)

# Font, Workspace names and layout x position.
font=-*-dina-bold-r-*-*-15-*-*-*-*-*-*-*
ws1="main"
ws2="web"
ws3="dev"
ws4="fun"
ws5="music"

# Layout switcher is optional and you'll have to find the area your workspace indicator is within,
#layl=195
#layr=213

# Padding to add spaces etc. Adjust slightly for your specific font.
pad=16

# Get and store pixel length of strings.
function tw {
    textwidth $font $1
}

len=$((`tw $ws1` + $pad))
len1=$((`tw $ws2` + $pad + $len))
len2=$((`tw $ws3` + $pad + $len1))
len3=$((`tw $ws4` + $pad + $len2))
len4=$((`tw $ws5` + $pad + $len3))

# Send to the correct workspace

if [ $XP -le "$len" ]; then

    xdotool key alt+1

elif [[ $XP -ge "$len" && $XP -le "$len1" ]]; then

    xdotool key alt+2

elif [[ $XP -ge "$len1" && $XP -le "$len2" ]]; then

    xdotool key alt+3

elif [[ $XP -ge "$len2" && $XP -le "$len3" ]]; then

    xdotool key alt+4

elif [[ $XP -ge "$len3" && $XP -le "$len4" ]]; then

    xdotool key alt+5

#elif [[ $XP -ge "$layl" && $XP -le "$layr" ]]; then

#    xdotool key alt+space

fi

To use this script put it in your path and chmod +x it, then add the following to your dzen command like this:

-e 'button1=exec:xmswitch'

This binds the script to the left mouse button.

The limitations of this script is of course that it presupposes that your workspace tabs are left aligned, but it should be easy to hack this script to accomodate other placements.

Any ideas for optimizations, conversion to haskell and integration into Xmonad are welcome. smile

- Ashren

Last edited by Ashren (2008-12-10 19:17:22)

Offline

#2 2008-12-10 22:20:13

Paul-S
Member
From: Wales
Registered: 2008-02-04
Posts: 353

Re: Xmswitch - A script to enable mouse based WS switching in Xmonad.

Very nice Ashren, works a treat smile

Offline

#3 2008-12-11 09:45:03

Ashren
Member
From: Denmark
Registered: 2007-06-13
Posts: 1,226
Website

Re: Xmswitch - A script to enable mouse based WS switching in Xmonad.

Good to hear! big_smile

Offline

#4 2009-02-13 19:30:06

Ashren
Member
From: Denmark
Registered: 2007-06-13
Posts: 1,226
Website

Re: Xmswitch - A script to enable mouse based WS switching in Xmonad.

This script is now redundant with dzen's new ^ca functionality. See this thread for more details: http://bbs.archlinux.org/viewtopic.php?id=40637

Offline

Board footer

Powered by FluxBB