3

Is there a way to make docky available only in one workspace and not in any other workspace in Ubuntu 14.04.

Shash
  • 1,001

1 Answers1

2

A background script to make docky run on specific workspaces (or not)

Based on exactly the same mechanism as this answer, below a background script that starts/stops the Docky launcher, depending on the current workspace.

The mechanism itself is pretty much tested out. Having said that, in the linked question it is tested with setting different Unity Launchers and setting different wallpapers per workspace, not starting/stopping Docky. That should however make no difference whatsoever, and in the hours I tested it, it ran without a single error.

How to use

  1. The script needs wmctrl:

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

  3. Test-run the script:

    • Start Docky
    • Start the script with the command:

      python3 /path/to/docky_perworkspace.py
      
    • Now Docky runs on all workspaces. Navigate to the workspaces you don't want Docky to appear and run (while on the workspace):

      pkill docky
      

    N.B. Use pkill docky, do not close docky from its own menu!

  4. That's pretty much it. If you want to change the setting simply either start or kill docky on the workspace you'd like it to run or not; the script will remember your preference.
  5. If all works fine, add it to your startup applications: Dash > Startup Applications > Add the command:

    /bin/bash -c "sleep 15&&python3 /path/to/docky_perworkspace.py"
    

The script

#!/usr/bin/env python3
import subprocess    
import os
import time

datadir = os.environ["HOME"]+"/.config/docky_run"
if not os.path.exists(datadir):
    os.makedirs(datadir)
workspace_data = datadir+"/docky_set_"

def get_runs():
    try:
        subprocess.check_output(["pgrep", "docky"]).decode("utf-8")
        return True
    except:
        return False

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 current viewport
    res = get_res()
    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)

curr_ws1 = current()
runs1 = get_runs()

while True:
    time.sleep(1)
    runs2 = get_runs()
    curr_ws2 = current()
    datafile = workspace_data+curr_ws2
    if curr_ws2 == curr_ws1:
        if runs2 != runs1:
            open(datafile, "wt").write(str(runs2))
    else:
        if not os.path.exists(datafile):
            open(datafile, "wt").write(str(runs2))
        else:
            curr_set = eval(open(datafile).read())
            if all([curr_set == True, runs2 == False]):
                subprocess.Popen(["docky"])
            elif all([curr_set == False, runs2 == True]):
                subprocess.Popen(["pkill", "docky"])           
    curr_ws1 = curr_ws2
    runs1 = get_runs()

Explanation

The script keeps track of the current workspace (no matter how many workspaces you have). Per workspace, a file is created in /.config/docky_run, in which is "written" if docky should be present or not on the referring workspace (True or False)

If you stay on the same workspace, but docky is either started or killed (its pid appears or ends), the script "understands" it is you who made the change on the referring workspace, and the file is updated.

If the workspace changes however, the script sees if the file exists, reads it and either starts or kills docky (or does nothing if not necessary), according to the content of the file and the current situation. If the file does not exist yet (because you are using the workspace for the first time since you started the script for the first time), the file is created and set to the current (last) situation.

Because settings per workspace are remembered in files, they will be remembered even after a restart.

Jacob Vlijm
  • 83,767
  • it worked. But after I reinstalled the os again, it stopped working. Is there anything I would be missing in the new install? – Shash Aug 06 '15 at 07:39
  • @Shash could it be that you forgot to install wmctrl? If thatś not it what Ubuntu version are you on? – Jacob Vlijm Aug 06 '15 at 19:34