How to make a KDE Plasma service menu entry which executes one command for each selected file?

I have made this “service menu” .desktop file for converting PNG image files to JPEG:

[Desktop Entry]
Type=Service
MimeType=image/png;
Actions=doStuff
X-KDE-Priority=TopLevel

[Desktop Action doStuff]
Exec=uxterm -hold -e ffmpeg -i "%U" -q:a 0 "%U.jpg"
Icon=background
Name=Convert to JPEG

It works, provided that I only do it on one file at a time. That is, I click one file to mark it, right-click it, select “Convert to JPEG” and watch it work.

However, if I select two or more files and then right-click one of them and pick “Convert to JPEG”, it attempts to send all the files’ paths at the same time to the same execute command, which of course just confuses FFMPEG, and it thus fails.

How do I make it so that it executes a dedicated command for each of the selected files? Neither search engines nor ChatGPT were able to tell me how.

Mention “using xargs”, then ChatGPT should give you the right answer.

Imagemagick.

You could try ( should work on any type):

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=image/*
Icon=image-png
Actions=jpg
X-KDE-Priority=TopLevel
X-KDE-StartupNotify=false
X-KDE-Submenu=Convert to

[Desktop Action jpg]
Name=Convert to JPEG image
Icon=image-jpg
Exec=filename=%f; convert %f ${filename%.*}.jpg

You are aware there are elaborate servicemenus available? Thinking of KIM or Reimage etc…that have tons more options.

I’m not looking for a workaround for this specific task, but to learn how to make service menus launch one instance per selected file. I only provided this as an example.

I don’t get what you mean. Surely this must be an option in the service menu format – not something part of the actual single command…