Friday, February 23, 2018

How can I give a script its own icon in the Unity launcher?




With Ubuntu 16.04, if you create a launcher (desktop file in ~/.local/share/applications) with a custom icon for a terminal application (so with Terminal=true) then when you start it, it will spawn a new standard terminal icon, and your custom icon will pulse and disappear within a few seconds.



In 14.04, it just worked as expected. (it would not start a new standard terminal icon).



Any idea what to do to change this behaviour? I have a few terminal apps that I want to launch from unity and the new behaviour is problematic (I lose trace of which is which since they all end up with the standard terminal icon)...



Why it doesn't work like you do



As mentioned in the comment, an application in principle can only be represented by one icon in the launcher at a time. This has always been the case.




What you are referring to is probably that Unity has become "smarter" in determining which of the .desktop files is the best representative for the application's window. Therefore, your script, running a terminal window, will be represented by the gnome-terminal -icon:



enter image description here



Therefore, what worked in the past in your setup, simply creating a launcher, starting your script, doesn't fool Unity anymore, and it picks the existing gnome-terminal launcher to represent your window.



The bad solution



...is to overrule Unity's choice by adding a line to your launcher (for 16.04):




StartupWMClass=gnome-terminal-server


enter image description here



...but then all terminal windows, no matter if they run your script or not, are grouped under this icon.



Furthermore, in general, having multiple .desktop files, calling the same application in their main command is bad, unclean practice.







EDIT



How to have (a) separate icon(s) for a running script(s)



It takes a bit of trickery and deceit, but it is possible to have a separate icon for multiple scripts, running in different terminal windows.



How it works in practice





  • Say you have a script, somscript.sh, which you want to run in a terminal window, showing its dedicated icon in the Unity Launcher while it runs.

  • Run the command:



    showicon somescript.sh someicon.png


    and the script will run inside a newly opened gnome-terminal window, showing the icon: someicon.png


  • If the window is closed, the icon is removed from the launcher again.




An example




  • I want a script, /home/jacob/Bureaublad/script.sh, run, showing in the Unity launcher with icon: /home/jacob/Thema/icon/ubu.png
    Running the command:



    showicon '/home/jacob/Bureaublad/script.sh' '/home/jacob/Thema/icon/ubu.png'


    will do that:




    enter image description here



    Now let's add another one:



    showicon '/home/jacob/Bureaublad/script2.sh' '/home/jacob/Thema/icon/banaan.png'


    The result:




    enter image description here



    Once the windows are closed, the icon(s) are removed again.




How to setup




  1. The script needs wmctrl




    sudo apt-get install wmctrl

  2. Create, if it doesn't exist yet, the directory ~/bin


  3. Copy the script below into an empty file, save it as showicon (no extension) in ~/bin, and make it executable

  4. Log out and back in, your setup should work. Test it with the command



    showicon  


    to have script.sh run in a terminal, showing icon.png in the Unity launcher.





The script



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


terminal = "gnome-terminal"
key = "com.canonical.Unity.Launcher"
script = sys.argv[1]
icon = sys.argv[2]

curr = os.path.dirname(os.path.realpath(__file__))
scriptname = script.split("/")[-1]

def get(command):
try:

return subprocess.check_output(command).decode("utf-8")
except subprocess.CalledProcessError:
pass

# --- edit Unity launcher section

def current_launcher():
return eval(get(["gsettings", "get", key, "favorites"]))

def set_launcher(desktopfile, arg):

curr_launcher = current_launcher()
last = [i for i, x in enumerate(curr_launcher) if x.startswith("application://")][-1]
new_icon = "application://"+desktopfile
if arg == "a":
if not new_icon in curr_launcher:
curr_launcher.insert(0, new_icon)
subprocess.Popen(["gsettings", "set", key,"favorites",str(curr_launcher)])
elif arg == "r":
curr_launcher.remove(new_icon)
subprocess.Popen(["gsettings", "set", key,"favorites",str(curr_launcher)])


# --- end section

def create_launcher(w, scriptname, icon):
launcher = ["[Desktop Entry]", "Type=Application",
"Exec=wmctrl -ia "+w, "Name="+scriptname, "Icon="+icon,
"StartupNotify=False"]
with open(l_name, "wt") as newlauncher:
for l in launcher:
newlauncher.write(l+"\n")


def getname():
# create unique launcher name
n = 1
while True:
nm = os.path.join(curr, "scriptlauncher_"+str(n)+".desktop")
if os.path.exists(nm):
n += 1
else:
break

return nm

wlist1 = [l.split()[0] for l in get(["wmctrl", "-l"]).splitlines()]
subprocess.Popen(["gnome-terminal", "-e", script])

while True:
time.sleep(1)
wdata = get(["wmctrl", "-l"]).splitlines()
if wdata:
try:

wlist2 = [l.split()[0] for l in wdata]
w = [w for w in wlist2 if not w in wlist1][0]
except IndexError:
pass
else:
# check if the new window belongs to the terminal
if terminal in get(["xprop", "-id", w]):
# create launcher
l_name = getname()
create_launcher(w, scriptname, icon)

set_launcher(l_name, "a")
break
wlist1 = wlist2

while True:
time.sleep(2)
wdata = get(["wmctrl", "-l"])
if wdata:
if not w in wdata:
os.remove(l_name)

set_launcher(l_name, "r")
break


Note




  • What the icon does:





    • It represents the gnome-terminal window, running your script

    • When clicking on it, it raises the window, as usual. The command to do so is automatically added to the temporary launcher:



      wmctrl -ia 


  • What it does not:




    • The only downside of this solution is that the icon does not show the usual arrow on the left for running apps, since the representation is indirect.





Explanation



Without going too much into detail:




  • The script is a wrapper. If you launch your script via showicon, an instance of showicon runs your script in a gnome-terminal window, similar to Terminal=true.

  • Subsequently, showicon waits for the new gnome-terminal window to appear and reads its window id.


  • A temporary launcher is then created, using the window id to create the command to raise the window in its Exec= line. The icon you set as argument in the command to run showicon is automatically set as icon of this temporary launcher (defined in the line Icon=).



    an example of such an automatically created (temporary) launcher:



    [Desktop Entry]
    Type=Application
    Exec=wmctrl -ia 0x04400b7f
    Name=script2.sh
    Icon=/home/jacob/Thema/icon/ubu.png
    StartupNotify=False


  • Using the very same procedure as in this answer, the temporary launcher is added to the Unity Launcher, in the top position, to represent your running script.


  • In the meantime, showicon checks for the window to exist. If not (anymore), the temporary launcher is removed from the Unity launcher and removed from existence at all, and the showicon instance is terminated.


No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...