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

waj*_*jiw 13 gnome command-line workspaces

我想弄清楚如何从 gnome 的终端脚本中获取工作区编号。有任何想法吗?

Isa*_*iah 15

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

例子:

xdotool get_desktop
Run Code Online (Sandbox Code Playgroud)

0如果从第一个工作区1运行,如果从第二个工作区运行等,这将返回。


小智 7

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

xprop -root -notype _NET_CURRENT_DESKTOP
Run Code Online (Sandbox Code Playgroud)


Ste*_*zzo 6

如果您正在使用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() 
Run Code Online (Sandbox Code Playgroud)

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

这是枚举的样子:

+---+---+
| 0 | 1 |
+---+---+
| 2 | 3 |
+---+---+
Run Code Online (Sandbox Code Playgroud)

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