Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question

5 Answers 5

up vote 8 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
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. –  nealmcb Aug 18 '13 at 19:40

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|improve this answer

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
share|improve this answer

It seems that with Unity, the accepted answer

 xdotool get_desktop_viewport

doesn't work - it always returns 0. I guess the screen is configured as a really big viewport of which only part is visible. The alternative is a bit tricky since you have to know the size of your workspace. I.e.:

 xdotool get_desktop_viewport

will return something like "1600 0" if you are in the upper right workspace. The first number is probably the width of the largest display you have.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.