[KDE 4] Adding Simple Service Menu Items

DON'T post new tutorials here! Please use the "Pending Submissions" board so the staff can review them first.
Post Reply
User avatar
lilrofl
Siliconoclast
Siliconoclast
Posts: 1363
Joined: 28 Jan 2009, 17:00
15
Location: California, USA
Contact:

[KDE 4] Adding Simple Service Menu Items

Post by lilrofl »

Adding a service menu item to the right click menu in KDE 4 is not quite the same as it was in prior versions, and the new method is not as strongly documented as one might like if... they where looking to do such a thing.

I'll preface this with; I was tired of double clicking a newly written script and having it open up in kwrite, rather then executing as planned... leading to a short pause while I opened a terminal to chmod the misbehaver. After all the trouble I thought I'd share my findings... even though I'm pretty sure most of you use Gnome :P

As with KDE 3.x the menu item consist of two parts, one to define it's location and what will activate it, the other to define what it will do. Both parts are fairly straight forward, but I'll explain the variable parts in case it's the first time you've looked at one.

Code: Select all

[Desktop Entry]
Type=Service
Encoding=UTF-8
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=text/plain;text/x-python;application/x-sh
Name=Chmod
Actions=MakeExe

[Desktop Action MakeExe]
Name=Make File Executable
Exec=chmod +x %F
Icon=kformula

Code: Select all

X-KDE-ServiceTypes=KonqPopupMenu/Plugin
This defines that this .desktop file is a plug-in for Konqueror, specifically a service menu item.

Code: Select all

MimeType=
This defines the file types that the service will be applied to using your file associations list. My list was located in

Code: Select all

/etc/mime.types
You can have as many mime types as you would like here, just separate each new type with a semi-colon. You can see that I added function for plain text, python files and shell scripts.

You could add function for all file types with

Code: Select all

application/octet-string
or just a category of types using wildcards such as

Code: Select all

text/*
or

Code: Select all

image/*

Code: Select all

Actions=
This points to the action of your menu item. You can have multiple values here pointing to different actions, but the format for all actions reletivly the same so I'll just say that you would separate multiple values with commas.

Code: Select all

[Desktop Actions MakeExe]
Begins the code off my menu item, matched to the

Code: Select all

Actions=
of the revious section... I should point out this is case sensitive, but you probablyknew that.

Code: Select all

Name=
How your menu item will appear in your service menu

Code: Select all

Exec=
The single command function of your menu item, using %variables listed below.

Code: Select all

%f A single file name
%F Multiple selected files, use this for programs that know how to act on multiple files simultaneously.
%u A single URL
%U Multiple URLs
%d A single directory containing the file represented by %f
%D A list of the same directory containing an entry for all selected files representing %F
%n filename without an associated path
%N Multiple files without paths
%k A file represented by a URL

Code: Select all

Icon=
Gives the item an associated icon.

For single variable commands like this you don't need any associated shell script to install the menu item, simple copy them into KDEs service directory... but where might that be?

run the following code to locate your service directory

Code: Select all

kde4-config --path services
pick the appropriate location and copy you file there as <filename>.desktop

You may have to restart x for your menu to take effect... but I didn't experience that.

It should be noted that there are plenty of service menu add-ons available from

Code: Select all

http://kde-look.org/
some are nice and useful, but none of them catered to the little convenience I was looking for :D

Original form of the code (for KDE 3.x) can be found in O'Reilly's Linux Desktop Hacks; #40, modifications credited to a few hours on Google (you might be surprised how much misinformation is out there), and a few hours in bash working out the kinks
knuffeltjes voor mijn knuffel
[img]http://i911.photobucket.com/albums/ac320/stuphsack/Sig.jpg[/img]

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: [KDE 4] Adding Simple Service Menu Items

Post by maboroshi »

WOW Nice man!

I use Gnome but KDE is looking more tempting because I am not a fan of Gnome 3 :-)

Anyway nice tutorial man!

*cheers

Maboroshi

lonewolf
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 185
Joined: 02 Dec 2010, 19:03
13

Re: [KDE 4] Adding Simple Service Menu Items

Post by lonewolf »

Thanks I will give it a try. I still use both KDE and Gnome on my operating systems.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: [KDE 4] Adding Simple Service Menu Items

Post by ayu »

I agree with Mabs, that KDE is looking a bit more tempting now that Gnome 3 is shit ^^

Nice tut!
"The best place to hide a tree, is in a forest"

User avatar
lilrofl
Siliconoclast
Siliconoclast
Posts: 1363
Joined: 28 Jan 2009, 17:00
15
Location: California, USA
Contact:

Re: [KDE 4] Adding Simple Service Menu Items

Post by lilrofl »

I gravitate toward KDE largely for its native Qt4 support, and superkaramba... it makes sense with all the python love.

Not that these things can't be ported over... but we like what we like and KDE4 has yet to do me wrong... also some of my shell scripts don't work for shite on Gnome :D

On a different note, LXDE is not bad if you favor a minimalistic approach to a desktop environment.
knuffeltjes voor mijn knuffel
[img]http://i911.photobucket.com/albums/ac320/stuphsack/Sig.jpg[/img]

Post Reply