Automate unity launcher icon ordering

Solution 1:

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)