Skip to content

Custom Context Menus For Dolphin

shved. edited this page Mar 7, 2025 · 3 revisions

Servicemenus in KDE Plasma

Servicemenus in KDE Plasma are custom context menu actions that enhance file management within Dolphin, the default file manager. They allow users to perform additional operations on files and directories, such as converting images, compressing files, or running scripts, directly from the right-click menu. Servicemenus are defined using .desktop files placed in ~/.local/share/kio/servicemenus or /usr/share/kio/servicemenus/ for Plasma6 and ~/.local/share/kservices5/ServiceMenus/ or /usr/share/kservices5/ServiceMenus/ for Plasma 5. These files specify command execution rules,supported file types, and menu appearance. Servicemenus provide a flexible way to extend Plasma’s functionality without modifying core system components. More information, customization options, syntax and more can be found on the KDE portal.

Creating a Servicemenu for Image Conversion

To demonstrate the use of servicemenus in KDE Plasma, we will create a custom context menu action for converting image files to a user-specified format.

Writing the Conversion Script

First, we need a script that performs the actual image conversion. While you can store it anywhere, it is recommended to place it in $HOME/.local/bin/kio-servicemenus for consistency and ease of use. Ensure kdialog is installed, as it will handle user interactions.

Create the file convert-image.sh in the suggested directory and make it executable.
Below is the script's logic, with inline comments explaining each step:

#!/bin/bash

# Check if the 'convert' command from ImageMagick is available on the system.
# This command is necessary for image conversion.
if ! command -v convert &> /dev/null; then
  # If 'convert' is not found, show a graphical error message using KDE's kdialog.
  # This message informs the user to install ImageMagick.
  kdialog --error "ImageMagick is not installed. Please install it to proceed."
  # Exit the script with an error code (1) to indicate failure.
  exit 1
fi

# Store the first command-line argument as the target file format for conversion.
# Example: If the user runs the script with "jpg", this will be the target format.
target_format="$1"
# Shift the command-line arguments to remove the target format from the list.
# Now, $@ will only contain the file paths to process.
shift

# Loop through all remaining command-line arguments (the files to convert).
for file in "$@"; do
  # Extract the file extension from the current file.
  # Example: For "image.png", this will be "png".
  extension="${file##*.}"
  # Convert the extension to lowercase to handle case-insensitive comparisons.
  # Example: "PNG" becomes "png".
  extension="${extension,,}"

  # Check if the file's extension matches the target format.
  if [[ "$extension" == "$target_format" ]]; then
    # If the file is already in the target format, skip it and notify the user.
    echo "Skipping '$file', already in the target format."
    continue  # Jump to the next file in the loop.
  fi

  # Create the output filename by replacing the original extension with the target format.
  # Example: "image.png" becomes "image.jpg" if the target is "jpg".
  output_file="${file%.*}.$target_format"
  
  # Convert the file using ImageMagick's 'convert' command.
  convert "$file" "$output_file"
  # Notify the user about the successful conversion.
  echo "Converted '$file' to '$output_file'."
done

# After processing all files, ask the user if they want to delete the original files.
# This uses a KDE kdialog yes/no prompt with a graphical interface.
if kdialog --yesno "Do you want to delete the original files?"; then
  # If the user clicks "Yes", loop through the original files again.
  for file_to_delete in "$@"; do
    # Delete the original file (CAUTION: This is irreversible!).
    rm "$file_to_delete"
    # Notify the user about the deletion.
    echo "Deleted '$file_to_delete'."
  done
fi

You can test the script by running it with parameters <image format> <path to image>.

Creating the Servicemenu

Now, we need to define the context menu entry for Dolphin. Servicemenus are stored in $XDG_DATA_HOME/kio/servicemenus/*.desktop. Ensure that the .desktop file is executable, as servicemenus will not function otherwise.

Create a .desktop file with the following content:

[Desktop Entry]
Type=Service
MimeType=image/*
Actions=convertToPng;convertToJpg;convertToWebp;convertToJpeg;convertToAvif
X-KDE-Submenu=Convert to

[Desktop Action convertToPng]
Name=PNG
Icon=viewimage
Exec=/path/to/script.sh png %U

[Desktop Action convertToJpg]
Name=JPG
Icon=viewimage
Exec=/path/to/script.sh jpg %U

[Desktop Action convertToJpeg]
Name=JPEG
Icon=viewimage
Exec=/path/to/script.sh jpeg %U

[Desktop Action convertToWebp]
Name=WEBP
Icon=viewimage
Exec=/path/to/script.sh webp %U

[Desktop Action convertToAvif]
Name=AVIF
Icon=viewimage
Exec=/path/to/script.sh avif %U

Save the file and verify that it is executable. After this, a new "Convert to" submenu should appear in Dolphin's context menu when right-clicking on an image file, allowing format conversion.

Screenshot_20250307_115040

You can now test the servicemenu by selecting an image, right-clicking it, and choosing a conversion option.

Image Image
Screenshot_20250307_115323 Screenshot_20250307_115808

Note

You can install this script by using this command:

curl -s "https://raw.githubusercontent.com/shvedes/awesome-kde/refs/heads/main/extras/kio-servicemenus/install.sh" | bash