11

Is there any way for a bash script to look up the name of the current workspace (virtual desktop)?

This seems really useful for things like customizing behaviors in my .bashrc file based on the desktop in which the shell was created.

3
  • What is your windowmanager? Apr 6, 2017 at 21:55
  • I'm using Cinnamon desktop installed on Ubuntu. I thought I'd said, but when I edited the question down I must have pulled out too much.
    – DonGar
    Apr 7, 2017 at 3:00
  • 1
    Under Cinnamon, this gives the last word in the desktop name. That's good enough for my use case. desktop=wmctrl -d | grep '*' | sed 's/.* //g'
    – DonGar
    Apr 8, 2017 at 22:14

3 Answers 3

15

You can do it with wmctrl -d to list all workspaces. The * represents the current workspace:

~$ wmctrl -d
0  * DG: 3840x1080  VP: 0,0  WA: 0,25 3840x1055  1
1  - DG: 3840x1080  VP: N/A  WA: 0,25 3840x1055  2
2  - DG: 3840x1080  VP: N/A  WA: 0,25 3840x1055  3
3  - DG: 3840x1080  VP: N/A  WA: 0,25 3840x1055  4

So, to get only the current, grep for the *:

~$ wmctrl -d | grep -w '*'
0  * DG: 3840x1080  VP: 0,0  WA: 0,25 3840x1055  1

Hope this helps!

5
  • If OP uses Unity, there is only one workspace :), also in the output of wmctrl -d Apr 6, 2017 at 21:53
  • @JacobVlijm Then OP probably wouldn't worry about this if there was only one workspace. :)
    – Terrance
    Apr 6, 2017 at 21:57
  • Of course he will, but then they are called viewports, and not directly retrievable from wmctrl -d. Apr 6, 2017 at 22:00
  • @JacobVlijm Well, one shining light is that Unity will be no longer the default and gone after the release of 18.04. :D
    – Terrance
    Apr 6, 2017 at 22:20
  • I actually like it, and it will probably be forked. Apr 6, 2017 at 22:22
12

Viewports in Unity

If you are using Unity, the current viewport cannot be retrieved directly from

wmctrl -d

since Unity has viewports, which are not detected directly by wmctrl -d. The output will show only one workspace:

0  * DG: 5040x2100  VP: 1680,1050  WA: 59,24 1621x1026  N/A
  • where my resolution is 1680 x 1050 (from xrandr)
  • the spanning workspace (all viewports) is 5040x2100. That is 3x2 viewports: 5040/1680 = 3 and 2100 / 1050 = 2.
  • I am currently on (viewport-) position 1680,1050 (x,y)

The script below calculates the current viewport from this information:

#!/usr/bin/env python3
import subprocess

def get_res():
    # get resolution
    xr = subprocess.check_output(["xrandr"]).decode("utf-8").split()
    pos = xr.index("current")
    return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]

def current():
    # get the resolution (viewport size)
    res = get_res()
    # read wmctrl -d
    vp_data = subprocess.check_output(
        ["wmctrl", "-d"]
        ).decode("utf-8").split()
    # get the size of the spanning workspace (all viewports)
    dt = [int(n) for n in vp_data[3].split("x")]
    # calculate the number of columns
    cols = int(dt[0]/res[0])
    # calculate the number of rows
    rows = int(dt[1]/res[1])
    # get the current position in the spanning workspace
    curr_vpdata = [int(n) for n in vp_data[5].split(",")]
    # current column (readable format)
    curr_col = int(curr_vpdata[0]/res[0])
    # current row (readable format)
    curr_row = int(curr_vpdata[1]/res[1])
    # calculate the current viewport
    return curr_col+curr_row*cols+1

print(current())

To use:

  1. Install wmctrl

    sudo apt install wmctrl
    
  2. Run it by the command

    python3 /path/to/get_viewport.py
    

    It will output 1, 2, 3, or whatever the current viewport is. It automatically counts rows/columns your viewport configuration may include.

Explanation

enter image description here

The script

  • gets the size of one viewport (resolution) from xrandr, including possible extra monitors.
  • gets the current position on the spanning workspace
  • galculates the number of columns /rows in your viewport setup
  • from that, it calculates the current viewport
1
  • 1
    Regardless of how I feel about Unity, this script works really well! Nicely done! +1
    – Terrance
    Apr 6, 2017 at 22:51
3

At least in Gnome shell, but probably in other WM too, you can ask the Xserver directly (if in Wayland, no idea).

[romano:~/tmp] % desktop=$(xprop -root -notype  _NET_CURRENT_DESKTOP | perl -pe 's/.*?= (\d+)/$1/') 
[romano:~/tmp] % echo $desktop
1

Basically, the command xprop will return

 [romano:~/tmp] % xprop -root -notype  _NET_CURRENT_DESKTOP
 _NET_CURRENT_DESKTOP = 1

and then you can massage a bit the info to get what you need.

1
  • That's the workspace number, not the name. To get the name you have to look up the number in the property _NET_DESKTOP_NAMES. Apr 7, 2017 at 20:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .