Difference between revisions of "Development/Tutorials/Creating Konqueror Service Menus"

Jump to: navigation, search
({{file}} -> {{path}})
(And Now Back to Our Regularly Scheduled Broadcast...: small edit to add Type=Service to more examples, service menu will not show in KDE4 without and it's stated above that it's required anyway)
 
(23 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{KDE3}}
+
{{Note|Part of this tutorial apply only to KDE3. Specially the dcop-part.}}
  
{{improve|port the bits on kdcop and dcop to dbus (or move that to another tutorial and reference it from here? that way we can move this into the realm of kde4. as i (aseigo) am working on some servicemenu improvements for kde4 right now, it may as well wait until that is finished.}}
+
''The ability to select mimetype-specific actions from a KDE file manager's context menu is an often requested feature. The pleasant surprise is that this is already possible. The even more pleasant surprise is that you don't need to be a software developer to create new actions. This article details step-by-step how to quickly and easily add new actions to KDE file manager context menus.''
 
+
''The ability to select mimetype-specific actions from Konqueror's context menu is an often requested feature. The pleasant surprise is that this is already possible. The even more pleasant surprise is that you don't need to be a software developer to create new actions. This article details step-by-step how to quickly and easily add new actions to Konqueror's context menu.''
+
  
 
=== Introduction  ===
 
=== Introduction  ===
In KDE-speak a "servicemenu" is a special entry that appears in Konqueror's context menu
+
In KDE-speak a "servicemenu" is a special entry that appears in a context menu (or other context-based interface) for a file (or for directory), depending on the type of files that are selected.
depending on the file(s) that are selected. You may have seen them in action without even knowing it.
+
 
For instance, if you have the KDE file archive utility "ark" installed you will see a menu entry
+
For example, if you have the KDE file archive utility Ark installed you will see a menu entry to "Extract here..." whenever you right click on a file archive. The option to "Extract here..." is a servicemenu.
to "Extract here..." whenever you right click on a tarball or zip file. The option to "Extract here..."
+
is a servicemenu.
+
  
Creating new servicemenus is very simple, requiring nothing more than an idea and a text editor.
+
Creating new servicemenus is very simple, requiring nothing more than an idea and a text editor. You don't have to be a programmer or a KDE wizard to make them. In this tutorial we will be creating a set of actions that allows us to set an image as our desktop wallpaper just by right clicking on it and selecting "Use As Wallpaper". By the end of this tutorial you should be able to create your own servicemenus with ease.
You don't have to be a programmer or a KDE wizard to make them.
+
In this tutorial we will be creating a set of actions that allows us to set an image as our desktop
+
wallpaper just by right clicking on it and selecting "Use As Wallpaper".
+
By the end of this tutorial you should be able to create your own servicemenus with ease.
+
  
=== Where the Servicemenus Live ===
+
=== Where the Servicemenus Locate ===
 
Servicemenus are defined using {{path|.desktop}} files, which are the same kind of files that are used
 
Servicemenus are defined using {{path|.desktop}} files, which are the same kind of files that are used
 
to create entries in the K Menu or on the KDE desktop. These servicemenu files are found in
 
to create entries in the K Menu or on the KDE desktop. These servicemenu files are found in
{{path|$KDEHOME/share/apps/konqueror/servicemenus}}, the directory KDE was installed to
+
{{path|$KDEHOME/share/services/ServiceMenus}}, the directory KDE was installed to
 
or any directory listed in the $KDEDIRS environment variable.
 
or any directory listed in the $KDEDIRS environment variable.
  
 
In the case of my home machine that means that servicemenu files can be found
 
In the case of my home machine that means that servicemenu files can be found
 
in the following places:
 
in the following places:
  {{path|/opt/kde3/share/apps/konqueror/servicemenus/}}
+
  {{path|/opt/kde4/share/services/ServiceMenus/}}
  {{path|/usr/share/apps/konqueror/servicemenus/}}
+
  {{path|/usr/share/services/ServiceMenus/}}
  {{path|/home/aaron/.kde3/share/apps/konqueror/servicemenus/}}
+
  {{path|/home/aaron/.kde4/share/kde4/services/ServiceMenus/}}
  
{{tip|$KDEHOME defaults to ~/.kde when it isn't set or defined. You can confirm this by running <code>kde4-config --localprefix</code> from a Konsole window. To locate where KDE was installed to, run <code>kde4-config --prefix</code> from a Konsole window.}}
+
{{tip|$KDEHOME defaults to ~/.kde when it isn't set or defined. You can find all the services directories by running <syntaxhighlight lang="text">kde4-config --path services</syntaxhighlight> from a Konsole window.}}
  
=== The Start of Our Servicemenu ===
+
=== The Start of Servicemenu ===
 
We will begin creating our wallpaper servicemenu by choosing a name for the file:
 
We will begin creating our wallpaper servicemenu by choosing a name for the file:
 
{{path|setAsWallpaper.desktop}} sounds good. The only thing that really matters with regards to the name is that it
 
{{path|setAsWallpaper.desktop}} sounds good. The only thing that really matters with regards to the name is that it
 
is unique and that it ends with {{path|.desktop}}. Next we'll open up the file in a text editor. The
 
is unique and that it ends with {{path|.desktop}}. Next we'll open up the file in a text editor. The
 
first thing we will put in the file is the "Desktop Entry" section:
 
first thing we will put in the file is the "Desktop Entry" section:
<code ini>
+
<syntaxhighlight lang="ini">
 
[Desktop Entry]
 
[Desktop Entry]
ServiceTypes=image/png,image/jpeg
+
Type=Service
 +
ServiceTypes=KonqPopupMenu/Plugin
 +
MimeType=image/png;image/jpeg;
 
Actions=setAsWallpaper
 
Actions=setAsWallpaper
</code>
+
</syntaxhighlight>
  
Every servicemenu file ''must'' have these three lines. Let's examine each of these lines one at a time.
+
Every servicemenu file ''must'' have these four lines. Let's examine each of these lines one at a time.
  
<code ini>
+
<syntaxhighlight lang="ini">
 
[Desktop Entry]
 
[Desktop Entry]
</code>
+
</syntaxhighlight>
 
+
 
KDE configuration files, including .desktop files, seperate the individual settings into sections. A section starts
 
KDE configuration files, including .desktop files, seperate the individual settings into sections. A section starts
 
with a heading made up of letters, numbers and spaces in between square brackets on a line
 
with a heading made up of letters, numbers and spaces in between square brackets on a line
Line 54: Line 47:
 
belong to the "Desktop Entry" group.
 
belong to the "Desktop Entry" group.
  
<code ini>
+
<syntaxhighlight lang="ini">
ServiceTypes=image/png,image/jpeg
+
Type=Service
</code>
+
ServiceTypes=KonqPopupMenu/Plugin
 +
MimeType=image/png;image/jpeg;
 +
</syntaxhighlight>
  
The ServiceTypes entry refers to the mimetypes that this servicemenu applies to. You can define more than one
+
The first line indicates that this .desktop file is of type Service; this is necessary since the default type is Application (i.e. something with an Exec line). Service is anything else, like plugins.
mimetype by providing a list seperated by commas (but no spaces). In this case we our servicemenu will show
+
 
up when we select PNG or JPEG images. The File Associations control panel is a good place to look for mimetype definitions.
+
The ServiceTypes entry refers to the type of plugin that this desktop file defines, and specifies that it is actually a Konqueror service menu.
{{Tip|To create a servicemenu for directories use the <code>inode/directory</code> mimetype. To create a servicemenu for all files, use the special <code>all/all</code> mimetype. To create a servicemenu for all files, but not directories, use the <code>all/allfiles</code> mimetype.}}
+
 
 +
The MimeType line defines the type of file for which this servicemenu applies. You can define more than one mimetype by providing a list seperated by semicolons (but no spaces). In this case our servicemenu will show up when we select PNG or JPEG images. The File Associations control panel is a good place to look for mimetype definitions.
 +
 
 +
{{Tip|To create a servicemenu for directories use the <syntaxhighlight lang="text">inode/directory</syntaxhighlight> mimetype. To create a servicemenu for all files, use the base <syntaxhighlight lang="text">application/octet-stream</syntaxhighlight> mimetype.}}
  
 
You can also specify an entire group of mimetypes using "typeglobs". To make our servicemenu apply not only to PNGs and JPEGs but to all images we would simply change the ServiceTypes entry to be:
 
You can also specify an entire group of mimetypes using "typeglobs". To make our servicemenu apply not only to PNGs and JPEGs but to all images we would simply change the ServiceTypes entry to be:
<code ini>
+
<syntaxhighlight lang="ini">
ServiceTypes=image/*
+
ServiceTypes=KonqPopupMenu/Plugin
</code>
+
MimeType=image/*;
 +
</syntaxhighlight>
 
Now when we right click on ''any'' image file in Konqueror we can select it as our background.
 
Now when we right click on ''any'' image file in Konqueror we can select it as our background.
<code ini>
+
<syntaxhighlight lang="ini">
 
Actions=setAsWallpaper
 
Actions=setAsWallpaper
</code>
+
</syntaxhighlight>
The Actions entry defines the actions we will create in our servicemenu. As with the ServiceTypes, you can define more
+
The Actions entry defines the actions we will create in our servicemenu. As with the ServiceTypes, you can define more than one action by using a semicolon separated list. Each of the actions listed will get a section of its own defining what that action does. In fact, that's our very next step.
than one action by using a semicolon separated list. Each of the actions listed will get a section of its own
+
defining what that action does. In fact, that's our very next step.
+
  
 
=== Creating an Action ===
 
=== Creating an Action ===
So far we have defined one action in our servicemenu file: setAsWallpaper. Now we need to define
+
So far we have defined one action in our servicemenu file: setAsWallpaper. Now we need to define what that action looks like and what it actually does. We begin by adding a new heading to the end of our file:
what that action looks like and what it actually does. We begin by adding a new heading to the end of our file:
+
 
<code ini>
+
<syntaxhighlight lang="ini">
 
[Desktop Action setAsWallpaper]
 
[Desktop Action setAsWallpaper]
</code>
+
</syntaxhighlight>
  
Note that it contains the setAsWallpaper action name. It is important to note that these files
+
Note that it contains the setAsWallpaper action name. It is important to note that these files are case sensitive, so we need to watch the capitalization here. Now that we have a section for our action, let's give our action a name that the user will see.
are case sensitive, so we need to watch the capitalization here. Now that we have a section for our action,
+
 
let's give our action a name that the user will see.
+
<syntaxhighlight lang="ini">
<code ini>
+
 
Name=Set As Background Image
 
Name=Set As Background Image
</code>
+
</syntaxhighlight>
  
To translate the name, we add another <tt>Name</tt> entry followed by the language code. For instance, the French translation for the "Open Terminal Here" service is provided by an entry that looks like this:
+
To translate the name, we add another <tt>Name</tt> entry followed by the language code. For instance, the Finnish translation for the "Set As Background Image" service is provided by an entry that looks like this:
<code ini>
+
 
Name[fr]=Ouvrir un terminal ici
+
<syntaxhighlight lang="ini">
</code>
+
Name[fi]=Aseta taustakuvaksi
 +
</syntaxhighlight>
  
 
Next let's add an icon:
 
Next let's add an icon:
<code ini>
+
 
 +
<syntaxhighlight lang="ini">
 
Icon=background
 
Icon=background
</code>
+
</syntaxhighlight>
  
Notice that we didn't include the .png file extension, but just referred to the icon by name. If we had left
+
Notice that we didn't include the .png file extension, but just referred to the icon by name. If we had left this line out our action would still work, it just wouldn't look as fancy. Now that we've achieved fancyness, let's finish up by making it useful:
this line out our action would still work, it just wouldn't look as fancy. Now that we've achieved
+
<syntaxhighlight lang="ini">
fancyness, let's finish up by making it useful:
+
<code ini>
+
 
Exec=dcop kdesktop KBackgroundIface setWallpaper %u 6
 
Exec=dcop kdesktop KBackgroundIface setWallpaper %u 6
</code>
+
</syntaxhighlight>
  
The Exec line defines what is run when the user selects the action from the menu. We can put any command we want
+
The Exec line defines what is run when the user selects the action from the menu. We can put any command we want there. The magic in this line is the "%u" which gets replaced with the URL of the image file before the command is run. If our command can accept more than one file at a time we can use "%U" instead.
there. The magic in this line is the "%u" which gets replaced with the URL of the image file before the
+
command is run. If our command can accept more than one file at a time we can use "%U" instead.
+
 
There are [http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html other special %values] but %u and %U are probably the most useful for servicemenus.
 
There are [http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html other special %values] but %u and %U are probably the most useful for servicemenus.
  
 
Our file now looks like this:
 
Our file now looks like this:
<code ini>
+
<syntaxhighlight lang="ini">
 
[Desktop Entry]
 
[Desktop Entry]
ServiceTypes=image/*
+
Type=Service
 +
ServiceTypes=KonqPopupMenu/Plugin
 +
MimeType=image/*;
 
Actions=setAsWallpaper
 
Actions=setAsWallpaper
  
Line 121: Line 117:
 
Icon=background
 
Icon=background
 
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 6
 
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 6
</code>
+
</syntaxhighlight>
  
If we save it and open up Konqueror, when we right click on a PNG, JPEG or GIF image we should now have a
+
If we save it and open up Konqueror, when we right click on a PNG, JPEG or GIF image we should now have a "Set as background" item in the menu. Try it out!
"Set as background" item in the menu. Try it out!
+
  
 
{{Tip|If you have a complex task that requires more than one command (for example if we wanted to copy the image file somewhere first and then use dcop to set it as the background) use a shell:<br/>
 
{{Tip|If you have a complex task that requires more than one command (for example if we wanted to copy the image file somewhere first and then use dcop to set it as the background) use a shell:<br/>
Exec<nowiki>=</nowiki>/bin/sh -c "&lt;YOUR COMMANDS HERE&gt;"}}
 
  
=== A Brief Excursion, or "The Amazingly Useful kdcop" ===
+
Exec<nowiki>=</nowiki>/bin/sh -c ";&lt;YOUR COMMANDS HERE&gt;"}}
[[Image:servicemenus_kdcop.png|thumb|right|400px]] Before we explore with our servicemenu a bit more, let's take a look at that exec line:
+
<code ini>
+
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 6
+
</code>
+
  
It may look complicated but it really isn't. The command takes
+
=== And Now Back to Our Regularly Scheduled Broadcast... ===
advantage of the KDE DCOP service which is used by KDE programs to talk to each other. Fortunately for us, we can also
+
use it for scripting.
+
  
The first thing I did when formulating the Exec line was to run <code>kdcop</code> which is a graphical DCOP browser.
+
Back from the land of DCOP, we have produced a working servicemenu. Now what? We improve it, of course!
Using it you can see every DCOP interface that is available for use at that moment. Browsing through the listing I found the
+
kdesktop entry and expanded it. Under kdesktop I quickly found the {{class|KBackgroundIface}}, and expanding that node of the tree
+
I spotted what I was looking for: <code>setWallpaper</code>! According to the listing it took two arguments:
+
* a {{qt3|QString}} (which is just a bunch of text) called "wallpaper"
+
* an integer called (somewhat mysteriously) "mode"
+
  
With {{path|setAsWallpaper.desktop}} open in KWrite I simply
+
Our current servicemenu scales the image to the size of the desktop and sets it as the wallpaper. But this isn't appropriate for wallpaper tiles which not be scaled but should be, well, ''tiled''. So let's add an action for tiles.
dragged and dropped the setWallpaper entry into Exec line!
+
First we'll need to change the Actions line to say something like this:
From there I replaced the parameters "wallpaper mode" with "%U 6".
+
  
Presto! Blamo! It worked! Now you're probably wondering where I got the number 6 from. The answer
+
<syntaxhighlight lang="text">
is I tried a bunch of numbers starting at 0 and working my way up until I achieved the results I
+
Actions=setAsWallpaper;tileAsWallpaper;
wanted. I could've looked at the source code or read the developer's documentation, but I opted
+
</syntaxhighlight>
for the guess-and-test method instead.
+
Note the use of semicolons rather than commas in that line. Next, we'll add a new action section to the end of the file that looks something like this:
 
+
<syntaxhighlight lang="ini">
=== And Now Back to Our Regularly Scheduled Broadcast... ===
+
 
+
Back from the land of DCOP, we have produced a working servicemenu. Now what? We improve it, of course!
+
  
Our current servicemenu scales the image to the size of the desktop and sets it as the wallpaper.
 
But this isn't appropriate for wallpaper tiles which not be scaled but should be, well, ''tiled''.
 
So let's add an action for tiles.
 
First we'll need to change the Actions line
 
to say something like this:
 
<code>
 
Actions=setAsWallpaper;tileAsWallpaper
 
</code>
 
Note the use of a semicolon. While the ServiceTypes are separated by commas, Actions are separated with semicolons.
 
Quirks-R-Us. Moving right along, we'll add a new action section to the end of the file that looks something like this:
 
<code ini>
 
 
[Desktop Action tileAsWallpaper]
 
[Desktop Action tileAsWallpaper]
 
Name=Use As Wallpaper Tile
 
Name=Use As Wallpaper Tile
 
Icon=background
 
Icon=background
 
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 2
 
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 2
</code>
+
</syntaxhighlight>
  
Note that "tileAsWallpaper" appears in the action section's heading. This is what
+
Note that "tileAsWallpaper" appears in the action section's heading. This is what tells Konqueror which action it is. In addition, we have a slightly different Name and a very slightly different Exec line. Now when we right click on an image we have another option, this time to tile the image across our desktop. We didn't even have to restart Konqueror, since it automatically rereads the file when it changes!
tells Konqueror which action it is. In addition, we have a slightly different Name and a very slightly
+
different Exec line. Now when we right click on an image we have another option, this time to tile
+
the image across our desktop. We didn't even have to restart Konqueror, since it automatically rereads
+
the file when it changes!
+
  
KDE's desktop offers several background image options, of which Scale and Tile are just two. Of course if we start adding all those various background options, and then add those to all the other servicemenus that a typical KDE installation has it's easy to see how the Action menu can quickly get out of hand. Starting with KDE 3.2 we can create submenus for our servicemenus by adding a line like the following to the <code ini>[Desktop Entry]</code> group of the .desktop file:
+
KDE's desktop offers several background image options, of which Scale and Tile are just two. Of course if we start adding all those various background options, and then add those to all the other servicemenus that a typical KDE installation has it's easy to see how the Action menu can quickly get out of hand. Starting with KDE 3.2 we can create submenus for our servicemenus by adding a line like the following to the <syntaxhighlight lang="ini">[Desktop Entry]</syntaxhighlight> group of the .desktop file:
<code ini>
+
<syntaxhighlight lang="ini">
 
X-KDE-Submenu=Set As Background
 
X-KDE-Submenu=Set As Background
</code>
+
</syntaxhighlight>
 
This will create a submenu called "Set As Background" and put all of the servicemenu's actions into it.
 
This will create a submenu called "Set As Background" and put all of the servicemenu's actions into it.
 
Our servicemenu .desktop file now looks like this:
 
Our servicemenu .desktop file now looks like this:
<code>
+
<syntaxhighlight lang="ini">
 
[Desktop Entry]
 
[Desktop Entry]
ServiceTypes=image/*
+
Type=Service
Actions=setAsWallpaper;tileAsWallpaper
+
ServiceTypes=KonqPopupMenu/Plugin
 +
MimeType=image/*;
 +
Actions=setAsWallpaper;tileAsWallpaper;
 
X-KDE-Submenu=Use As Wallpaper
 
X-KDE-Submenu=Use As Wallpaper
  
Line 203: Line 169:
 
Icon=background
 
Icon=background
 
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 2
 
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 2
</code>
+
</syntaxhighlight>
 
+
Pretty simple, huh?
+
 
+
=== Cha-a-a-a-rge! ===
+
Now it's time to strap on your imagination helmets: What sort of cool and useful servicemenus can you dream up? Open up a text editor and let the mayhem begin! Just don't forget to share the spoils of your adventures with the rest of us! ;-)
+

Latest revision as of 00:12, 7 July 2011

noframe
 
Note
Part of this tutorial apply only to KDE3. Specially the dcop-part.

The ability to select mimetype-specific actions from a KDE file manager's context menu is an often requested feature. The pleasant surprise is that this is already possible. The even more pleasant surprise is that you don't need to be a software developer to create new actions. This article details step-by-step how to quickly and easily add new actions to KDE file manager context menus.

Contents

[edit] Introduction

In KDE-speak a "servicemenu" is a special entry that appears in a context menu (or other context-based interface) for a file (or for directory), depending on the type of files that are selected.

For example, if you have the KDE file archive utility Ark installed you will see a menu entry to "Extract here..." whenever you right click on a file archive. The option to "Extract here..." is a servicemenu.

Creating new servicemenus is very simple, requiring nothing more than an idea and a text editor. You don't have to be a programmer or a KDE wizard to make them. In this tutorial we will be creating a set of actions that allows us to set an image as our desktop wallpaper just by right clicking on it and selecting "Use As Wallpaper". By the end of this tutorial you should be able to create your own servicemenus with ease.

[edit] Where the Servicemenus Locate

Servicemenus are defined using .desktop files, which are the same kind of files that are used to create entries in the K Menu or on the KDE desktop. These servicemenu files are found in $KDEHOME/share/services/ServiceMenus, the directory KDE was installed to or any directory listed in the $KDEDIRS environment variable.

In the case of my home machine that means that servicemenu files can be found in the following places:

/opt/kde4/share/services/ServiceMenus/
/usr/share/services/ServiceMenus/
/home/aaron/.kde4/share/kde4/services/ServiceMenus/
Ktip.png
 
Tip
$KDEHOME defaults to ~/.kde when it isn't set or defined. You can find all the services directories by running
kde4-config --path services
from a Konsole window.


[edit] The Start of Servicemenu

We will begin creating our wallpaper servicemenu by choosing a name for the file: setAsWallpaper.desktop sounds good. The only thing that really matters with regards to the name is that it is unique and that it ends with .desktop. Next we'll open up the file in a text editor. The first thing we will put in the file is the "Desktop Entry" section:

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=image/png;image/jpeg;
Actions=setAsWallpaper

Every servicemenu file must have these four lines. Let's examine each of these lines one at a time.

[Desktop Entry]

KDE configuration files, including .desktop files, seperate the individual settings into sections. A section starts with a heading made up of letters, numbers and spaces in between square brackets on a line by itself. This first line means that all the options that follow, up until the next heading, belong to the "Desktop Entry" group.

Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=image/png;image/jpeg;

The first line indicates that this .desktop file is of type Service; this is necessary since the default type is Application (i.e. something with an Exec line). Service is anything else, like plugins.

The ServiceTypes entry refers to the type of plugin that this desktop file defines, and specifies that it is actually a Konqueror service menu.

The MimeType line defines the type of file for which this servicemenu applies. You can define more than one mimetype by providing a list seperated by semicolons (but no spaces). In this case our servicemenu will show up when we select PNG or JPEG images. The File Associations control panel is a good place to look for mimetype definitions.

Ktip.png
 
Tip
To create a servicemenu for directories use the
inode/directory
mimetype. To create a servicemenu for all files, use the base
application/octet-stream
mimetype.


You can also specify an entire group of mimetypes using "typeglobs". To make our servicemenu apply not only to PNGs and JPEGs but to all images we would simply change the ServiceTypes entry to be:

ServiceTypes=KonqPopupMenu/Plugin
MimeType=image/*;

Now when we right click on any image file in Konqueror we can select it as our background.

Actions=setAsWallpaper

The Actions entry defines the actions we will create in our servicemenu. As with the ServiceTypes, you can define more than one action by using a semicolon separated list. Each of the actions listed will get a section of its own defining what that action does. In fact, that's our very next step.

[edit] Creating an Action

So far we have defined one action in our servicemenu file: setAsWallpaper. Now we need to define what that action looks like and what it actually does. We begin by adding a new heading to the end of our file:

[Desktop Action setAsWallpaper]

Note that it contains the setAsWallpaper action name. It is important to note that these files are case sensitive, so we need to watch the capitalization here. Now that we have a section for our action, let's give our action a name that the user will see.

Name=Set As Background Image

To translate the name, we add another Name entry followed by the language code. For instance, the Finnish translation for the "Set As Background Image" service is provided by an entry that looks like this:

Name[fi]=Aseta taustakuvaksi

Next let's add an icon:

Icon=background

Notice that we didn't include the .png file extension, but just referred to the icon by name. If we had left this line out our action would still work, it just wouldn't look as fancy. Now that we've achieved fancyness, let's finish up by making it useful:

Exec=dcop kdesktop KBackgroundIface setWallpaper %u 6

The Exec line defines what is run when the user selects the action from the menu. We can put any command we want there. The magic in this line is the "%u" which gets replaced with the URL of the image file before the command is run. If our command can accept more than one file at a time we can use "%U" instead. There are other special %values but %u and %U are probably the most useful for servicemenus.

Our file now looks like this:

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=image/*;
Actions=setAsWallpaper
 
[Desktop Action setAsWallpaper]
Name=Use As Wallpaper
Icon=background
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 6

If we save it and open up Konqueror, when we right click on a PNG, JPEG or GIF image we should now have a "Set as background" item in the menu. Try it out!

Ktip.png
 
Tip
If you have a complex task that requires more than one command (for example if we wanted to copy the image file somewhere first and then use dcop to set it as the background) use a shell:
Exec=/bin/sh -c ";<YOUR COMMANDS HERE>"


[edit] And Now Back to Our Regularly Scheduled Broadcast...

Back from the land of DCOP, we have produced a working servicemenu. Now what? We improve it, of course!

Our current servicemenu scales the image to the size of the desktop and sets it as the wallpaper. But this isn't appropriate for wallpaper tiles which not be scaled but should be, well, tiled. So let's add an action for tiles. First we'll need to change the Actions line to say something like this:

Actions=setAsWallpaper;tileAsWallpaper;

Note the use of semicolons rather than commas in that line. Next, we'll add a new action section to the end of the file that looks something like this:

[Desktop Action tileAsWallpaper]
Name=Use As Wallpaper Tile
Icon=background
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 2

Note that "tileAsWallpaper" appears in the action section's heading. This is what tells Konqueror which action it is. In addition, we have a slightly different Name and a very slightly different Exec line. Now when we right click on an image we have another option, this time to tile the image across our desktop. We didn't even have to restart Konqueror, since it automatically rereads the file when it changes!

KDE's desktop offers several background image options, of which Scale and Tile are just two. Of course if we start adding all those various background options, and then add those to all the other servicemenus that a typical KDE installation has it's easy to see how the Action menu can quickly get out of hand. Starting with KDE 3.2 we can create submenus for our servicemenus by adding a line like the following to the
[Desktop Entry]
group of the .desktop file:
X-KDE-Submenu=Set As Background

This will create a submenu called "Set As Background" and put all of the servicemenu's actions into it. Our servicemenu .desktop file now looks like this:

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=image/*;
Actions=setAsWallpaper;tileAsWallpaper;
X-KDE-Submenu=Use As Wallpaper
 
[Desktop Action setAsWallpaper]
Name=Scaled
Icon=background
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 6
 
[Desktop Action tileAsWallpaper]
Name=Tiled
Icon=background
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 2

This page was last modified on 7 July 2011, at 00:12. This page has been accessed 39,698 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal