有没有办法从命令行检测您当前所在的工作区?

我试图找出如何从gnome中的终端脚本获取工作区编号。 有任何想法吗?

如果您不使用Compiz,则可以使用xdotool 安装xdotool

例:

xdotool get_desktop 

如果从第一个工作区运行,则返回0如果从第二个工作区运行,则返回1

一个旧的,回答的线程,但我只是在相同的信息之后。 您可以使用标准xorg工具执行此操作:

 xprop -root -notype _NET_CURRENT_DESKTOP 

如果你使用compiz,这将有点困难。

编辑:这现在有和没有compiz,最后…

我写了一个“小”python脚本来做到这一点:

 #!/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() 

将其保存在某处并将其标记为可执行文件。 这将只输出0和工作空间数之间的数字。

这是枚举的样子:

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

你必须安装xdotool 安装xdotool 为了在禁用compiz的情况下工作。

没有安装任何东西,如果你使用metacity,你可以使用这个:

 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 

似乎与Unity,接受的答案

  xdotool get_desktop_viewport 

不起作用 – 它总是返回0.我猜屏幕被配置为一个非常大的视口,其中只有部分是可见的。 替代方案有点棘手,因为您必须知道工作区的大小。 即:

  xdotool get_desktop_viewport 

如果你在右上方的工作区,将返回类似“1600 0”的内容。 第一个数字可能是您拥有的最大显示器的宽度。