Is there a way to detect which workspace you are currently in from the command line?

5,387

Solution 1

If you aren't using Compiz, you can use xdotool Install xdotool.

Example:

xdotool get_desktop

This will return 0 if run from the first workspace, 1 if run from the second etc.

Solution 2

An old, and answered thread, but I was just after the same info. You can do this with standard xorg tools with:

xprop -root -notype _NET_CURRENT_DESKTOP

Solution 3

If you are using compiz, this will be a bit more difficult.

edit: this now works both with and without compiz, finally...

I wrote a "little" python script to do it:

#!/usr/bin/python
from subprocess import Popen, PIPE
getoutput = lambda x: Popen(x, stdout=PIPE).communicate()[0]
compiz_running = list(i for i in getoutput(("ps", "-aef", )).split("\n")
    if "compiz --replace" in i and not "grep" in i) != []

if compiz_running:
    # get the position of the current workspace
    ws = list(int(i.strip(",")) for i in  getoutput(("xprop", "-root",
        "-notype", "_NET_DESKTOP_VIEWPORT", )).split()[-2:])
    # get the number of horizontal and vertical workspaces
    hsize = int(getoutput(("gconftool",
        "--get", "/apps/compiz/general/screen0/options/hsize", )))
    vsize = int(getoutput(("gconftool",
        "--get", "/apps/compiz/general/screen0/options/vsize", )))
    # get the dimentions of a single workspace
    x, y = list(int(i) for i in getoutput(("xwininfo", "-root",
        "-stats", )).split("geometry ")[1].split("+")[0].split("x"))
    # enumerate workspaces
    workspaces, n = [], 0
    for j in range(vsize):
        for i in range(hsize):
            workspaces.append([n, [x*i, y*j, ], ])
            n += 1
    print list(i for i in workspaces if i[1] == ws)[0][0]
# if compiz is not running
else: # this code via @DoR
    print getoutput(("xdotool", "get_desktop", )).strip() 

Save this somewhere and mark it as executable. This will output just a number between 0 and the number of workspaces.

This is how the enumeration looks like:

+---+---+
| 0 | 1 |
+---+---+
| 2 | 3 |
+---+---+

You've got to install xdotool Install xdotool for this to work in case compiz is disabled.

Solution 4

Without installing anything and if you are using metacity, you can use this :

python -c "import wnck; s=wnck.screen_get_default(); s.force_update(); w=s.get_active_workspace();  w_num=w.get_number(); print(w_num);" 2>/dev/null
Share:
5,387

Related videos on Youtube

wajiw
Author by

wajiw

I'm a LAMP/Flex/Java programmer out to learn how to make the best web apps possible.

Updated on September 17, 2022

Comments

  • wajiw
    wajiw over 1 year

    I'm trying to figure out how to get the workspace number from a terminal script in gnome. Any ideas?

  • nealmcb
    nealmcb almost 11 years
    Thanks. I had to remove " --replace" from the compiz_running test, and also seemed to get the wrong value for vsize (1 vs 2) when running this on Precise 12.04, even though ccsm General Options said my Desktop Size / Vertical Virtual Size is 2.
  • Jochen
    Jochen over 3 years
    Thank you for this answer. I am using it to create a separate tmux session for each Gnome workspace. I just need to use this instead: xprop -root -notype _NET_SHOWING_DESKTOP