Add item into context menu in Dolphin

Is it possible to add new items into context menu in Dolphin (KDE Plasma) if I right click on something?

I would like to make a script called Move to applications, which will show only if I right click on a .desktop file and then it will move it to /usr/share/applications/ folder. Or is there a faster way for moving .desktop files to /usr/share/applications/ folder.


Solution 1:

You can create a Service Menu to do what you want.

  • Create the folder ~/.local/share/kservices5
  • Create the folder ~/.local/share/kservices5/ServiceMenus
  • In ~/.local/share/kservices5/ServiceMenus create a plain text file called move.desktop

Paste the following content into move.desktop

[Desktop Action move-file]

Exec=/usr/lib/x86_64-linux-gnu/libexec/kf5/kdesu mv "%U" /usr/share/applications

Name=Move file
Icon=document-send

[Desktop Entry]
Actions=move-file

MimeType=application/x-desktop;

ServiceTypes=KonqPopupMenu/Plugin
Type=Service
X-KDE-Priority=TopLevel

The use of MimeType=application/x-desktop; ensures that this context menu entry will be seen only when the target file is a .desktop file.

This is an image of Dolphin's context menu before creating the service menu:

Dolphin context menu before creating the service menu

In the following screenshot taken after creating the service menu you'll see an additional entry, Move file:

Note the Move file entry

Clicking on Move file will prompt you for your password because you want to move a file into /usr/share/applications.

Password required

After providing your password, the selected file will be moved to /usr/share/applications:

File is now removed from its original location and is now in */usr/share/applications*

Note that the owner isn't the same as .desktop files already there. You'll need to fix that however you choose:

File ownership differs


In case you need to change ownership/permissions, you could modify the Exec= line like this (to change ownership):

Exec=printf %U | xsel -b -i && /usr/lib/x86_64-linux-gnu/libexec/kf5/kdesu mv "%U" /usr/share/applications && cd /usr/share/applications && /usr/lib/x86_64-linux-gnu/libexec/kf5/kdesu chown root:root $( basename $(xsel -b -o) )

The additional steps involve using printf and xsel to put the path of the file into the clipboard and then using basename to strip off everything but the filename. Of course, you'll need kdesu again! I don't know if there's a shorter way!