Often we rely on CLI applications or small scripts to elaborate files and most of the time we would like to have quick access to them while browsing our filesystem with a file manager like Dolphin. Luckily, an highly configurable environment like KDE, overcomes this issue allowing every user to populate the contextual menu.
Contextual menu aka services
This mechanism is properly defined under KDE system as Service menu and affects applications like Dolphin or Konqueror, which use the same component to render the filesystem.
Service directives are based upon ini files installed under one of the dedicated directories, running the command below will help to know the exact path:
$ kf5-config --path services
It returns a list of path separated by the colon
: char, what we are looking for is in our home subfolder.
The ini file format is an older format used for software settings storage, from time to time I have seen some variations added to achieve particular needs, but basically, it is a simple ASCII file with a list of settings in the form key=value, optionally grouped in sections:
# No section for this group of keys key1=value 1 key2=value 2 [Section number 1] key3=value 3 key4=value 4
Keys shouldn’t contain spaces and the equal
= char separates them from the assigned values.
Sections are marked surrounding a text with square brackets when the section is missing the settings belong to an anonymous section.
Comments are introduced starting the line with a hash
# or a semicolon
The basic skeleton
From KDE 3 to KDE 5 something might be varying a little bit, the instructions in this post have been tested under KDE 4 and KDE 5.
The service I want to build will open all selected file in a single gVim window using multiple tabs instead of multiple buffers.
The first thing to do is create a simple text file with .desktop extension in our folder:
$ touch ~/.local/share/kservices5/openwithgvim.desktop
No particular permissions are needed, those applied by default are just enough.
Let’s open it with our favorite text editor and start to fill in with standard code:
[Desktop Entry] Type=Service ServiceTypes=KonqPopupMenu/Plugin Icon=gvim
These lines are mandatory to let our system understand that:
- We are declaring a desktop file and its setting (by the section entitled Desktop Entry);
- We are defining a Service and not an Application item (Type key);
- The service is a Konqueror Popup Menu Plugin (ServiceTypes key);
- While browsing the filesystem a particular icon is shown beside the filename (Icon key).
The Icon key is optional, but having a specific icon for each .desktop file is useful to quickly recognize them in our folder.
To know which values are allowed in the Icon key we can use Kdialog as seen in a previous article.
A mime-type for our service
Acting over files we must specify which file type will be interested from our plugin, being gVim a text file editor we might enable our plugin for all and only text files:
Being gVim also able to represent a file in the hexadecimal format, we can enable it for every file:
To extend our plugin to a more limited set of mime-types we can append more of them using the semicolon
; char as separator:
The full list of mime-types can be found in the mimeinfo.cache file:
$ cat $XDG_DATA_DIRS/applications/mimeinfo.cache
Or through the System settings interface where we can query directly the wanted extension:
Adding some action
For each command to include in our service, we need to define an action using an id. Actions is the key where to list all the ids we declare, separating them with a semicolon
An id is a string of characters without spaces, for our service we just need an action:
For each id we have to create a _Desktop Action
[Desktop Action open_with_gvim] Name=Open with gVim Name[it]=Apri con gVim Name[fr]=Ouvrir avec gVim Icon=gvim Exec=gvim -p %F
The listed keys define:
- The label to display in the popup menu (Name key) and the various translations which can be adopted (Name[xx] key);
- An icon beside the label (Icon key);
- The command to launch (Exec key)
The Icon is not mandatory but will help to better recognize our service among the several items in the popup menu.
The Exec key can make use of the following placeholders:
- %f for a single file;
- %F for more than one selected file;
- %u for a single URL;
- %U for more than one selected URL.
In particular, the command specified allows gVim to open all the selected files in separated tabs (-p switch).
Below the full source:
[Desktop Entry] Type=Service ServiceTypes=KonqPopupMenu/Plugin Icon=gvim MimeType=all/allfiles Actions=open_with_gvim [Desktop Action open_with_gvim] Name=Open with gVIM Name[it]=Apri con gVIM Name[fr]=Ouvrir avec gVIM Icon=gvim Exec=gvim -p %F
Submenu and Top-level menu
At this point, all we have to do is select one or more text files and right-click over them, our beautiful service will appear as a submenu under the Actions menu.
To display it under the top menu we need a new key in the Desktop Entry section:
One more interesting key allowed in the Desktop Entry section is X-KDE-Submenu
X-KDE-Submenu=gVim actions X-KDE-Submenu[it]=Azioni gVim
It allows grouping our actions under a submenu labeled with its value and placed under the top menu (X-KDE-Priority key) or under the Actions menu, it’s a good option when our service contains more than one action.
As we have seen for Name, also X-KDE-Submenu supports localization adding the language code between the squared brackets.
Services are a fantastic method to enhance our favorite file manager to speed up our frequent tasks over files without open a terminal emulator.
Just to see how useful they are, a large selection of them is provided at the Dolphin services section in the KDE store .
Last but not least the KDE techbase about service menus worths a mention for further references.