How do I create a program shortcut for a command in Ubuntu 12.04 LTS?

I remember I could do that in an older version of Ubuntu, but I'm lost in Ubuntu 12.04.

I want to add an item in the Launcher bar or on Desktop, that when double clicked runs the following command for example:

emacs -q --some-other-options --load ~/xyz.el

In MS Windows speak, I want to create a program shortcut with customized parameters. It would be also nice if I could also run the command by typing "Emacs XYZ" on Launcher Search and selecting the found item. All that is possible in Windows 7 and how to do that is easily discovered by right clicking on various things.

As a workaround, I made an executable script "Emacs XYZ.sh" containing the command, and created a link to the script (by dragging while pressing Alt) on Desktop. But this workaround is incomplete because the link cannot be dragged to the Launcher bar, and when I double click on the link, Ubuntu always asks

Do you want to run "Link to Emacs XYZ.sh", or display its contents?

making it a two step to run the command. And when I search "Emacs XYZ" on Launcher Search and select the item, it just opens the script with gedit, instead of running the script. Also, the display icon for the script is not the Emacs icon.


Solution 1:

I would use alacarte:/usr/bin/alacarte to build a menu entry.

Then in alacarte, in the section where the command goes, I would use a terminal emulator to open emacs.

For Instance: terminator -x htop will now launch htop in a terminator window. Or if you prefer to stay with gnome-terminal, then it would be gnome-terminal -x htop.

Once this is added, you can then find it in search, and add it to your launch bar on Unity.

terminator and gnome-terminal have other flags (like the -x in terminator -x htop) that might do the task better..

Here is my menu entry for a program called MOC I added with alacarte, that I like to use for a audio program. I used the -e flag instead with terminator because of the fact that I didn't need to pass along any other arguments with the command.

Solution 2:

I was reading and trying many things and then I found https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles which explains everything. Based on that, here comes a self-answer.

In this answer, I will demonstrate:

  • how to display a program shortcut's details
  • how to modify a program shortcut
  • how to create a new program shortcut that runs a command
  • how to add a program shortcut to some place so that it appears in Launcher Search (By Launcher Search, I mean the search interface that shows up when you click on Dash home icon on the left right, or by pressing the Windows key.)

Prerequisites:

  • Familiarity with the commands cd and ls

Open Terminal. Cd to /usr/share/applications and display the folder's contents with ls, also display the folder with default file browser using xdg-open:

cd /usr/share/applications
ls
xdg-open . &

ls shows files with .desktop extension. These files are text files and their contents can be viewed with gedit, for example by entering:

gedit gcalctool.desktop &

Each .desktop file in the folder is a program shortcut and the file browser displays them as program shortcuts instead of displaying their actual file-names. Which program shortcuts belong to which files can be discovered with grep: for example to find out what file is for Character Map, enter:

grep -Fi 'character map' *.desktop

which will show that it corresponds to the file gucharmap.desktop.

In order to edit the shortcut, open it with gksudo gedit:

gksudo gedit gucharmap.desktop &

In order to create a new shortcut, for example to create a shortcut that runs the following command:

python -i -c 'from math import *'

enter:

gksudo gedit gcalctool.desktop my-calc.desktop &

gcalctool.desktop is the already existing shortcut file for Calculator, and my-calc.desktop is a new file name that doesn't exist yet. Using gedit, copy contents of gcalctool.desktop to my-calc.desktop and then modify contents of my-calc.desktop, and save.

contents of gcalctool.desktop:

[Desktop Entry]
Name=Calculator
Comment=Perform arithmetic, scientific or financial calculations
Exec=gcalctool
Icon=accessories-calculator
Terminal=false
Type=Application
StartupNotify=true
Categories=GNOME;GTK;Utility;Calculator;
X-GNOME-DocPath=gcalctool/gcalctool.xml
X-GNOME-Bugzilla-Bugzilla=GNOME
X-GNOME-Bugzilla-Product=gcalctool
X-GNOME-Bugzilla-Component=general
X-GNOME-Bugzilla-OtherBinaries=gnome-calculator
X-Ubuntu-Gettext-Domain=gcalctool

contents of my-calc.desktop (changed fields: Name, Comment, Exec, Terminal):

[Desktop Entry]
Name=My Calculator
Comment=Perform calculations with Python
Exec=python -i -c 'from math import *'
Icon=accessories-calculator
Terminal=true
Type=Application
StartupNotify=true
Categories=Utility;Calculator;

Now 'My Calculator' icon should appear in the file browser window opened with xdg-open .. Double clicking on it should open a terminal window with python -i -c 'from math import *' ran. You can also run it from Launcher Search by searching for 'My Calculator'. You can drag the icon (in the file browser window) to an empty spot in launcher bar on the left (the taskbar) to have it there. If you drag the icon to Desktop, Ubuntu will say

Error while moving "my-calc.desktop" ... Error moving file: Permission denied.

Instead, press Alt while dragging it to Desktop, Ubuntu will display a menu: 'Move here', 'Copy here' and 'Link here', select 'Copy here', the shortcut will be copied to Desktop.

In order to be able to delete a shortcut in /usr/share/applications, enter:

gksudo xdg-open /usr/share/applications &

It's probably good to only delete shortcuts you've made and leave others intact.

If you can't find something in /usr/share/applications and yet it appears from Launcher Search, it's probably in ~/.local/share/applications/

Further reading:

https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles

Solution 3:

Here is the easiest way I've found:

  1. Start your program (running programs show up in the launcher)
  2. Right-click the icon in the launcher and select 'lock to launcher'

If the link does not work, cd into /usr/share/applications and then edit the file that matches your application (ex. theProgramYouWant.desktop, then just change the Exec= to point to the executable for the program).