Ubuntu – How to refresh launcher icon application menu in ubuntu 12.04

12.04desktoplaunchermenuunity

After modifying a desktop file in /usr/share/applications, I need to refresh the launcher icon application menu so that my modification shows up.
For the sake of clarity, the menu that should get modified is the one that appears with a right click when the pointer is on an icon of the launcher (the big bar full of icon on the left side of the screen).

Best answer would be a simple command line.

I am not using ubuntu-2d and I don't want to logout/login.

I'm aware of some partial solution:

  • unity --replace reloads everything (window decoration, notification area, …)
  • killall ubuntu-2d-launcher is ubuntu-2d only, but if a similar one-line command exists for unity, that would be a good solution.
  • logging out then logging back in works also, but I can't afford loosing all my session just to update the menu.
  • on ubuntu 14.04 I don't have this issue: any changes made to "*.desktop" files seems to be repercuted live.

Any suggestions ? Is there a way thanks to ccsm ?

Best Answer

  • The most elegant way is to "restart" the icon; to remove the icon from its position in the launcher and replace it on the same position. The script below does the job. It is in python2, since 12.04 does not come with python3 by default. However, it can be used in python3 as well, only change the shebang in that case. The script can also be useful (on later Ubuntu versions as well) to immediately apply a changed icon for example.

    You can simply use it by calling the script, with the edited desktop file as an argument (see further below).

    Note: in 12.04, if a refreshed icon represents a running application, the application in question will crash, as described in this question, so if you use it, make sure the application is not running. In 14.04, the icon will simply not refresh in case of a running application.

    The script

    #!/usr/bin/env python
    
    import subprocess
    import time
    import sys
    
    desktopfile = sys.argv[-1]
    
    def read_currentlauncher():
        # reads the current launcher contents
        get_launcheritems = subprocess.Popen([
            "gsettings", "get", "com.canonical.Unity.Launcher", "favorites"
            ], stdout=subprocess.PIPE)
        return get_launcheritems.communicate()[0].decode("utf-8")
    
    def set_launcher(llist):
        # sets a defined unity launcher list
        current_launcher = str(llist).replace(", ", ",")
        subprocess.Popen([
            "gsettings", "set", "com.canonical.Unity.Launcher", "favorites",
            current_launcher,
            ])
    
    def refresh_icon(desktopfile):
        current_launcher = read_currentlauncher()
        current_launcher_temp = eval(current_launcher)
        item = [item for item in current_launcher_temp if desktopfile in item][0]
        index = current_launcher_temp.index(item)
        current_launcher_temp.pop(index)
        set_launcher(current_launcher_temp)
        time.sleep(2)
        set_launcher(current_launcher)
    
    refresh_icon(desktopfile)
    

    How to use it

    • Copy the script above into an empty file and safe it as refresh.py
    • For convenience reasons, make it executable
    • Refresh the icon by the command:

      /path/to/script/refresh.py name_of_edited_desktopfile (e.g. 'firefox.desktop')
      

    If you really want to make it smooth

    • Make the script executable, remove the .py extension, save it in ~/bin. After log out/in, you can run it by the command:

      refresh firefox.desktop (as an example)