5

I have two monitors. I have multiple workspaces. I want my launcher ("launcer placement" in settings > displays) to be present on both my monitors in the one workspace, but have only one launcher (on the main monitor) in the other workspace. This is because I want to stretch Tweetdeck across both monitors and the launcher gets in the way. Is this possible?

Workspace 1 (Good):

Image 1

Workspace 2 (NOT Good):

Image 2

Workspace 2 (Good):

Image 3

8
  • 1
    Would you have this done automatically (requires background script) or would a shortcut key do (toggle)? Left or right screen? Jan 5, 2016 at 21:21
  • 1
    Please reload your image, and make it visible in your post.
    – carnendil
    Jan 5, 2016 at 21:23
  • @carnendil Was wondering how to do that, thanks!
    – Colin
    Jan 5, 2016 at 21:29
  • @JacobVlijm Preferably automatically, but if shortcut is the only way, eh. It'll have to do. Thing is I'll be checking in on Tweetdeck regularly so having to press a combination of keys each time would be slightly inconvenient.
    – Colin
    Jan 5, 2016 at 21:32
  • @Colin both can be done :) Jan 5, 2016 at 21:37

1 Answer 1

5

How is the launcher's placement defined?

The Launcher's placement is defined by a few things:

  1. The value, set in /org/compiz/profiles/unity/plugins/unityshell/num-launchers. You can read it by the command:

    dconf read /org/compiz/profiles/unity/plugins/unityshell/num-launchers
    

    which will output either 1 (launcher on one screen) or 0 (launcher on all screens).

    To set the launcher to visible on all screens can be done by the command:

    dconf write /org/compiz/profiles/unity/plugins/unityshell/num-launchers 0
    
  2. If the value is 1 (visible on only one screen), we can set on which screen the launcher appears, by setting the targeted screen as primary screen, with an xrandr command:

    xrandr --output <screen_name> --primary
    

Those are exactly the commands the script(s) below use(s).

Script1; manually set the launcher to only left or on both screens (with a key shortcut)

Depending on the argument you run the script with, it either sets the launcher to show on all screens with the command:

dconf write /org/compiz/profiles/unity/plugins/unityshell/num-launchers 0

or on only one:

dconf write /org/compiz/profiles/unity/plugins/unityshell/num-launchers 1

In the latter case, it also sets the left screen as the primary one.

The script

#!/usr/bin/env python3
import subprocess
import sys

key = "dconf write /org/compiz/profiles/unity/plugins/unityshell/num-launchers "

arg = sys.argv[1]

if arg == "left":
    # the launcher is set to show on all screens
    subprocess.Popen(["/bin/bash", "-c", key+"1"])
elif arg == "all":
    # the launcher is set to show only on the left screen
    subprocess.Popen(["/bin/bash", "-c", key+"0"])
    # make sure the left screen is the primary one
    scr_data = subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()
    left = [l.split()[0] for l in scr_data if "+0+0" in l][0]
    subprocess.Popen(["xrandr", "--output", left, "--primary"])

How to use

  • Copy the script into an empty file, save it as launcher_pos.py
  • Test the script with both the commands (from a terminal window):

    python3 /path/to/launcher_pos.py left
    

    and

    python3 /path/to/launcher_pos.py all
    
  • if all works fine, add the commands to two shortcut key combinations: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add both commands to available shortcuts.

Script 2; automatic version

The script below automatically keeps track of which is the current workspace (no matter how many workspaces you have and in what configuration).

This script is run with the workspaces, where you want the launcher only on the left, as an argument. An example:

If you run the script with the command:

python3 /path/to/launcher_pos.py 2 3

The result is:

enter image description here

The script

import subprocess
import sys
import time

wspace = sys.argv[1:]
key = "dconf write /org/compiz/profiles/unity/plugins/unityshell/num-launchers "

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(",", "") )]

res = get_res()

def get_dt():
    # get the current viewport
    vp_data = subprocess.check_output(["wmctrl", "-d"]).decode("utf-8").split()
    dt = [int(n) for n in vp_data[3].split("x")]
    cols = int(dt[0]/res[0])
    curr_vpdata = [int(n) for n in vp_data[5].split(",")]
    curr_col = int(curr_vpdata[0]/res[0])+1; curr_row = int(curr_vpdata[1]/res[1])
    return str(curr_col+curr_row*cols)

def set_launcher(arg):
    if arg == "left":
        # the launcher is set to show on all screens
        subprocess.Popen(["/bin/bash", "-c", key+"1"])
    elif arg == "all":
        # the launcher is set to show only on the left screen
        subprocess.Popen(["/bin/bash", "-c", key+"0"])
        # make sure the left screen is the primary one
        scr_data = subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()
        left = [l.split()[0] for l in scr_data if "+0+0" in l][0]
        subprocess.Popen(["xrandr", "--output", left, "--primary"])

curr_ws1 = get_dt()

while True:
    time.sleep(1)
    curr_ws2 = get_dt()
    if curr_ws2 != curr_ws1:
        if curr_ws2 in wspace:
            arg = "left"
        else:
            arg = "all"
        set_launcher(arg)
    curr_ws1 = curr_ws2

How to use

  1. The script needs wmctrl

    sudo apt-get install wmctrl
    
  2. Copy the script into an empty file, save it as launcher_pos.py

  3. Test run it by the command:

    python3 /path/to/launcher_pos.py 1 3
    

    On workspace 1 and 3, the launcher now should appear only on the left

  4. If all works fine, add it to your Startup Applications: Dash > Startup Applications > Add. Add the command:

    /bin/bash -c "sleep 15 && python3 /path/to/launcher_pos.py 1 3"
    

    (If workspace 1 and 3 are the ones you only want the launcher on the left)

3
  • Awesome, thanks! Same applies to me, I need to head to bed. I've read your post and it seems good so far but tomorrow I'll test it more extensively etc. :)
    – Colin
    Jan 5, 2016 at 22:50
  • Works like a charm! Thanks a lot. And just out of interest, what does the -c parameter do and why "sleep 15"?
    – Colin
    Jan 6, 2016 at 12:36
  • 1
    @Colin Perfect! The sleep 15 is because the script uses xrandr, and it might break if it runs too soon or give an incorrect resolution if it is not set correctly yet on a local level (especially with multiple monitors). Then the command is complicated and needs to be executed by bash (/bin/bash -c) Jan 6, 2016 at 12:42

You must log in to answer this question.

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