I'm trying to figure out how to get the workspace number from a terminal script in gnome. Any ideas?
If you aren't using Compiz, you can use xdotool .
This will return
0 if run from the first workspace,
1 if run from the second etc.
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
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() 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 ").split("+").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 == ws) # 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 for this to work in case compiz is disabled.
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
It seems that with Unity, the accepted answer
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.:
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.