2

I have written an install script which automates my install process. Afterwards I would like it to rearrange/add/remove the icons to the unity launcher.

Using dconf watch / I can see output as I reorder the icons. Is there a command line way to do this - possibly with gsettings?

  • Yes, it's possible to do it via gsettings , the schema is almost the same. How exactly do you want to automate it ? – Sergiy Kolodyazhnyy Apr 22 '16 at 22:26
  • Preferably I would like a single command where I can pass the program icons in a certain order. So I guess the command and the syntax – myol Apr 22 '16 at 23:49
  • That can be done, but not with program icons. The launcher uses .desktop files , which are sort of like shortcuts in Windows – Sergiy Kolodyazhnyy Apr 22 '16 at 23:52
  • 1
    ill post a script later in the day. It's a fairly simple task. – Sergiy Kolodyazhnyy Apr 22 '16 at 23:56
  • 1
    You can even set it up in one account, copy it to others: askubuntu.com/questions/527168/… – Jacob Vlijm Apr 23 '16 at 5:29
2

Introduction

The simple script bellow allows taking a file as argument and sets the launcher to have whatever shortcuts to programs appear in the file (one per line).

Basic idea is that the launcher icons are actually links to .desktop files (wherever they may appear) , and canonical method to set the launcher items is to run the following command:

gsettings set com.canonical.Unity.Launcher favorites  "[ 'item1.desktop' , 'item2.desktop' , . . . 'item3.desktop;  ]"

This can get tedious if you want to add many items and quoting can become a pain. One can always open a desired program and right click on the icon to invoke the "Lock to launcher" option, but that also is not practical when you're dealing with large amount of items.

The approach taken here is to simply read a file, with 1 time per line, build up the command text , and execute it. This is no different from running the gsettings set command above, except that the hard work is done for you.

Usage:

To run the script , save it to a file, make it executable with chmod +x /path/to/script and run as

python /path/to/script /path/to/file

The input file should contain full path to the items you want added to the launcher, such as /usr/share/applications/firefox.desktop , but it's not necessary , a line with firefox.desktop is OK too.

Demo

Before running the script

enter image description here

After running the script

enter image description here

Note that the order is exactly the same as the entries appear in the input file

Script Source

#!/usr/bin/env python
# Author: Serg Kolo
# Date: April 22, 2016
# Purpose:  programmatically set Unity launcher items
# by reading a file
# Written for: http://askubuntu.com/q/760895/295286
# Tested on: Ubuntu 14.04 LTS
import sys
import subprocess

command="""gsettings set com.canonical.Unity.Launcher favorites """

def run_command(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    output = p.stdout.read().strip()
    return output  


items=""
with open(sys.argv[1]) as file:
  for line in file:
      temp = "'" + line.strip().split('/')[-1] + "'"
      items = ",".join([items,temp])

items = '"[ ' + items[1:] + ' ]"'

print run_command(command + " " + items)
  • Fantastic answer, above and beyond. Thank you Serg – myol Apr 23 '16 at 15:45
  • For future visitors: I've created another script, launcherctl.py , which can be found here : askubuntu.com/a/829228/295286 with detailed explanation of how launcher works and how the utility can be used. That answer , however, contains basic version. I will be adding more features to it, so look into my sergrep github repository for newer version. Again, consult the answer for more info – Sergiy Kolodyazhnyy Sep 24 '16 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.