this post was submitted on 18 Mar 2025
6 points (87.5% liked)

KDE

6201 readers
91 users here now

KDE is an international technology team creating user-friendly free and open source software for desktop and portable computing. KDE’s software runs on GNU/Linux, BSD and other operating systems, including Windows.

Plasma 6 Bugs

If you encounter a bug, proceed to https://bugs.kde.org/, check whether it has been reported.

If it hasn't, report it yourself.

PLEASE THINK CAREFULLY BEFORE POSTING HERE.

Developers do not look for reports on social media, so they will not see it and all it does is clutter up the feed.

founded 2 years ago
MODERATORS
 

Hi everyone! I recently created a custom service menu for the Dolphin file manager, but unfortunately, it doesn't seem to be working as expected.

My goal is to have a menu to convert image file formats quickly with image magick.

Here is the .desktop file I managed to arrange

[Desktop Entry]
Type=Service
Name=Convert Image
Icon=gtk-convert
MimeType=image/png;image/jpg;image/jpeg;image/ico;image/heic;image/svg;image/webp;
Actions=topng;tojpg;toico;towebp
X-KDE-Submenu=Convert Image

[Desktop Action topng]
Name=To Png
Exec=sh -c 'FILE="%f"; DIRECTORY="$(dirname $FILE)"; FILENAME="${FILE%.*}"; magick "$FILE" -format png "$DIRECTORY/$FILENAME.png"'

[Desktop Action tojpg]
Name=To Jpg
Exec=sh -c 'FILE="%f"; DIRECTORY="$(dirname $FILE)"; FILENAME="${FILE%.*}"; magick "$FILE" -format jpg "$DIRECTORY/$FILENAME.jpg"'

[Desktop Action toico]
Name=To Ico
Exec=sh -c 'FILE="%f"; DIRECTORY="$(dirname $FILE)"; FILENAME="${FILE%.*}"; magick "$FILE" -format ico "$DIRECTORY/$FILENAME.ico"'

[Desktop Action towebp]
Name=To Webp
Exec=sh -c 'FILE="%f"; DIRECTORY="$(dirname $FILE)"; FILENAME="${FILE%.*}"; magick "$FILE" -format webp "$DIRECTORY/$FILENAME.webp"'

When I right-click on an image my custom menu item appears as expected. However, when I click on the menu item, nothing happens. There are no error messages or any indication of what might be going wrong.

I made sure the .desktop file is executable and the sh script works when using it on the command line, so I'm out of ideas for what could be going wrong.

Are there any logs I can check to get more information about what's failing? I'm using plasma 6.3.3 on Arch btw

Edit: To answer how to troubleshoot custom service menus (specifically .desktops) the desktop-file-validate cli utlility checks all the syntax errors and can provide solutions to what's wrong. For my use case though, using a script file for the Exec line and avoiding any double quotes was the practical way of assuring my service menu works.

top 6 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 5 days ago* (last edited 5 days ago) (1 children)

You're using single quotes in your Exec lines, which is not legal .desktop file syntax.

I suggest replacing your single quotes with double quotes, and replacing your double quotes with backslash-escaped double quotes.

https://web.archive.org/web/20241130225327/https://specifications.freedesktop.org/desktop-entry-spec/latest/exec-variables.html

[–] [email protected] 1 points 5 days ago (1 children)

If I understood correctly, I made the changes as you said like this:

Exec=sh -c "FILE=\"%f\"; DIRECTORY=\"$(dirname \"$FILE\")\"; FILENAME=\"${FILE%.*}\"; magick \"$FILE\" -format png \"$DIRECTORY/$FILENAME.png\""

Now when I click on the service menu option this error popup appears:

Syntax error in command sh -c "FILE=%f; DIRECTORY=$(dirname $FILE); FILENAME=${FILE%.*}; magick $FILE\ -format png $DIRECTORY/$FILENAME.png" coming from

It seems escaping the double quotes doesn't actually escape the backlash with it?

I then tried escaping those new backlashes like this

Exec=sh -c "FILE=\\"%f\\"; DIRECTORY=\\"$(dirname \\"$FILE\\")\\"; FILENAME=\\"${FILE%.*}\\"; magick \\"$FILE\\" -format png \\"$DIRECTORY/$FILENAME.png\\""

and now Dolphin doesnt complain about syntax, but the new converted image doesn't get made :(

[–] [email protected] 2 points 5 days ago* (last edited 5 days ago) (1 children)

I can't tell from that error message whether the inner quotes are being discarded when the command is run, or just hidden when the error message is displayed.

Too bad it doesn't tell you what part of the command is causing the syntax error. Have you checked for more info in the output of journalctl --boot _UID=1000? (Assuming your user id is 1000 and you use systemd.)

Re-reading the spec page that I linked above, I see reference to both a general escape rule and a quoting rule. That could be complicating things with the quotes and backslashes, and maybe even the dollar signs and semicolons, which apparently are reserved. In case it helps, I don't think those semicolons are needed at all.

Before diving deeper into escaping rules, though, I would consider whether it's time to move the whole command line into a script, and simply pass %f to the script in your Exec= line. That would avoid the need for nested escaping/quoting, and allow you to write debug information to a temporary file when the script runs.

[–] [email protected] 2 points 5 days ago* (last edited 5 days ago) (1 children)

It's fixed now! At first I researched a little more and found about the desktop-file-validate cli utility from the desktop-file-utils package, and it did tell me a lot about the syntax errors I was making with the exec command.

But as you wisely suggested, dealing with those escaping rules was a bit too bothersome for my use case, so I ended going the bash script route which worked flawlessly at last! So thank you for pointing that out!

Here's the final .desktop file for anyone interested:

[Desktop Entry]
Type=Service
MimeType=image/png;image/jpg;image/jpeg;image/ico;image/heic;image/svg+xml;image/webp;
Actions=topng;tojpg;toico;towebp
X-KDE-Submenu=Convert Image Format
Icon=viewimage

[Desktop Action topng]
Name=To Png
Exec=/home/myuser/.local/share/kio/servicemenus/scripts/convert-image.sh %f png
Icon=viewimage

[Desktop Action tojpg]
Name=To Jpg
Exec=/home/myuser/.local/share/kio/servicemenus/scripts/convert-image.sh %f jpg
Icon=viewimage

[Desktop Action toico]
Name=To Ico
Exec=/home/myuser/.local/share/kio/servicemenus/scripts/convert-image.sh %f ico
Icon=viewimage

[Desktop Action towebp]
Name=To Webp
Exec=/home/myuser/.local/share/kio/servicemenus/scripts/convert-image.sh %f webp
Icon=viewimage

and the bash script coupled with it:

#!/bin/bash

FILE="${1}"
FORMAT="${2}"

# Check if magick is installed
if ! command -v magick &> /dev/null; then
    echo "Error: magick command not found. Please install ImageMagick."
    exit 1
fi

# Check if FILE exists
if [[ ! -f "$FILE" ]]; then
    echo "File not found: $FILE"
    exit 1
fi

DIRECTORY=$(dirname "$FILE")
# Get the file name by looking for the longest match starting from the beginning of the string up to the last dot.
FILENAME=$(basename "$FILE" .${FILE##*.})

# Convert the file format using magick
magick "$FILE" -format "$FORMAT" "$DIRECTORY/$FILENAME.$FORMAT"

[–] [email protected] 1 points 5 days ago (1 children)

Glad you got it working!

BTW, in case you're not aware of it, you might find the shellcheck command useful when writing scripts.

[–] [email protected] 1 points 4 days ago

ooo that looks nice! I'll try it once I need to unrust my script skills again, thanks for the help!