Dolphin service-menu help

I’m trying to create a new context menu for dolphin (service-menu) using the tutorial listed here - Creating Dolphin Service Menus | Developer

The custom action I’m trying to define is to scan the directory/file that have been selected with clamdscan along with some options.

Issue: When I select only one file or directory it works as expected. However selecting more than one results in an error as it treats the 2 file files/directory/file+directory as one.

Example: I’ve created a folder within Downloads called scan-test and inside it there are 2 files file1.txt and file2.txt.

/home/pkg/Downloads/scan-test
├── file1.txt
└── file2.txt

0 directories, 2 files

Selecting only file2.txt and using the service-menu to run a scan I get the following:

/home/pkg/Downloads/scan-test/file2.txt: OK

----------- SCAN SUMMARY -----------
Infected files: 0
Time: 0.000 sec (0 m 0 s)
Start Date: 2022:06:26 18:51:37
End Date:   2022:06:26 18:51:37

If I select both the files and try to run the scan using context menu:

client_scan: Failed to determine real filename of /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt.
Failed to determine real filename of /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt.
Quarantine of the file may fail if file path contains symlinks.
ERROR: Can't access file /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt

----------- SCAN SUMMARY -----------
Infected files: 0
Total errors: 1
Time: 0.000 sec (0 m 0 s)
Start Date: 2022:06:26 18:53:21
End Date:   2022:06:26 18:53:21

Directly from the command line, I can run a scan for both files at the same time:

 pkg ~/Downloads/scan-test: clamdscan --multiscan --fdpass -v file1.txt file2.txt 
/home/pkg/Downloads/scan-test/file1.txt: OK
/home/pkg/Downloads/scan-test/file2.txt: OK

----------- SCAN SUMMARY -----------
Infected files: 0
Time: 0.000 sec (0 m 0 s)
Start Date: 2022:06:26 18:54:57
End Date:   2022:06:26 18:54:57
 pkg ~/Downloads/scan-test:

This is how my Exec line in .desktop looks like:

Exec=konsole --hold -e "clamdscan --multiscan --fdpass -v '%U'"

Based on the site linked above it should work:

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.

I’m obviously missing something.

PS: Does anyone know where the .desktop files for Ark (Compress in Dolphin context menu) is located? Perhaps I can look at that to see how they implement something similar.

/usr/share/applications/org.kde.ark.desktop

But its Exec line is pretty bland… :arrow_down:

Exec=ark %U
1 Like

@Aragorn I’m looking for the servicemenu .desktop files – similar to ones in /usr/share/kio/servicemenus. Unfortunately the ones over there for me only contain these:

total 48K
-rwxr-xr-x 1 root root 2.8K Jun  7 21:59 kget_download.desktop
-rwxr-xr-x 1 root root 5.3K Jun  7 22:35 kleopatra_decryptverifyfiles.desktop
-rwxr-xr-x 1 root root 7.2K Jun  7 22:35 kleopatra_decryptverifyfolders.desktop
-rwxr-xr-x 1 root root  11K Jun  7 22:35 kleopatra_signencryptfiles.desktop
-rwxr-xr-x 1 root root 8.0K Jun  7 22:35 kleopatra_signencryptfolders.desktop
-rwxr-xr-x 1 root root 1.2K Jun  7 22:53 kompare.desktop
-rwxr-xr-x 1 root root 3.7K Jun  7 23:24 konsolerun.desktop

I’ve been through them and they don’t seem to have an example on how to handle multiple files/folders.

does your command that is passed:
clamdscan --multiscan --fdpass -v '%U'
work as intended?

Why the single quotes around the %U?

This. :point_up_2:

Use double quotes or no quotes at all.

It does when selecting one file or one directory. When I select 2 (or more) %U returns the selections as a space separated list. Taking the error in in OP it seem to return /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt.

The command does not like this it seems. I also tried the other way around i.e. to the same result (i.e. exact error about not finding the file).

Exec=konsole --hold -e 'clamdscan --multiscan --fdpass -v -- "%F"'

why?
just test what happens when you remove the single quotes around %U

… I thought I just cross-read something about the difference between %u and %U - but I can’t find it anymore
maybe it’s also about this

I’m by no means an expert - I just saw a possible syntax error and told you about it.

No, I appreciate it. The reason I was putting quotes around then is to handle filenames that have spaces in them. In my scan-test directory I added a new file, so it looks like this now.

 pkg ~/Downloads/scan-test: tree
.
├── file1.txt
├── file2.txt
└── Text File.txt

And changed the Exec line to this:

Exec=konsole --hold -e "clamdscan --multiscan --fdpass -v %U"

If I select file1.txt and file2.txt and run the service-menu, it works:

/home/pkg/Downloads/scan-test/file1.txt: OK
/home/pkg/Downloads/scan-test/file2.txt: OK

----------- SCAN SUMMARY -----------
Infected files: 0
Time: 0.000 sec (0 m 0 s)
Start Date: 2022:06:26 19:37:00
End Date:   2022:06:26 19:37:00

However, if I select all 3 files or just the file with space in its name, I run into an error:

/home/pkg/Downloads/scan-test/file1.txt: OK
/home/pkg/Downloads/scan-test/file2.txt: OK
client_scan: Failed to determine real filename of /home/pkg/Downloads/scan-test/Text.
Failed to determine real filename of /home/pkg/Downloads/scan-test/Text.
Quarantine of the file may fail if file path contains symlinks.
ERROR: Can't access file /home/pkg/Downloads/scan-test/Text
client_scan: Failed to determine real filename of /home/pkg/Downloads/scan-test/File.txt.
Failed to determine real filename of /home/pkg/Downloads/scan-test/File.txt.
Quarantine of the file may fail if file path contains symlinks.
ERROR: Can't access file /home/pkg/Downloads/scan-test/File.txt

----------- SCAN SUMMARY -----------
Infected files: 0
Total errors: 2
Time: 0.009 sec (0 m 0 s)
Start Date: 2022:06:26 19:37:45
End Date:   2022:06:26 19:37:45

I’m sorry but I cannot help with how to work with filenames with spaces - I don’t know how %U works
and how it would take
or how you would feed it
the complete filename, with spaces, instead of just the first part up to the space character.

Using lower case u which is meant to be used when there is only one file/directory to be passed works, but spits out the number of files/directories that have been selected, each into its own konsole window, which is not ideal.

So, selecting all 3 files and running service-menu works but returns 3 different konsole windows each with the summary for one selection made:

Exec=konsole --hold -e "clamdscan --multiscan --fdpass -v '%u'"

Use double quotes, not single quotes. Single quotes prevent the variable from being interpreted by the shell. Double quotes will work well with filenames that have spaces in them.

Could you give me an example? I tried the following (other than the ones above) and selected file1.txt and file2.txt:

Exec=konsole --hold -e clamdscan --multiscan --fdpass -v "%U"

And it errors with:

client_scan: Failed to determine real filename of /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt.
Failed to determine real filename of /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt.
Quarantine of the file may fail if file path contains symlinks.
ERROR: Can't access file /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt

----------- SCAN SUMMARY -----------
Infected files: 0
Total errors: 1
Time: 0.000 sec (0 m 0 s)
Start Date: 2022:06:26 19:55:51
End Date:   2022:06:26 19:55:51

You had this:

why not write it - with double quotes - like this:
Exec=konsole --hold -e "clamdscan --multiscan --fdpass -v "%U""

so that the command becomes:

clamdscan --multiscan --fdpass -v "%U"

1 Like

Made that change and tested by selecting file1.txt and file2.txt. That gave me a 2 errors for the same file and nothing on the other. I think it needs to escape the ".

Exec=konsole --hold -e "clamdscan --multiscan --fdpass -v "%U""
/home/pkg/Downloads/scan-test/file2.txt: File path check failure: Permission denied. ERROR
/home/pkg/Downloads/scan-test/file2.txt: File path check failure: Permission denied. ERROR

----------- SCAN SUMMARY -----------
Infected files: 0
Total errors: 2
Time: 0.000 sec (0 m 0 s)
Start Date: 2022:06:26 20:01:24
End Date:   2022:06:26 20:01:24

I think it would be best in that case to write it as follows… :arrow_down:

Exec=konsole --hold -e 'clamdscan --multiscan --fdpass -v "%U"'

Thanks, that was one of the scenarios I tested. Unfortunately, same error:

Exec=konsole --hold -e 'clamdscan --multiscan --fdpass -v "%U"'
client_scan: Failed to determine real filename of /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt.
Failed to determine real filename of /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt.
Quarantine of the file may fail if file path contains symlinks.
ERROR: Can't access file /home/pkg/Downloads/scan-test/file1.txt /home/pkg/Downloads/scan-test/file2.txt

----------- SCAN SUMMARY -----------
Infected files: 0
Total errors: 1
Time: 0.000 sec (0 m 0 s)
Start Date: 2022:06:26 20:02:43
End Date:   2022:06:26 20:02:43

I can have a look but from memory the “meld service menu” should have examples.

//EDIT: no it doesn’t work as I thought.

~/.local/share/kservices5/ServiceMenus/meld-menu.desktop

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=inode/directory;application/octet-stream;
Icon=meld
Actions=selected;_SEPARATOR_;setArg1;diff;
X-KDE-Priority=TopLevel
X-KDE-StartupNotify=false
X-KDE-Submenu=Meld menu
X-KDE-Submenu[de]=Meld Menü
X-KDE-Submenu[es]=Meld menú
X-KDE-Submenu[fr]=Meld menu

[Desktop Action setArg1]
Icon=arrow-left
Exec=echo "%U" > /tmp/meld-menu-arg1
Name=Set file/folder
Name[de]=Setze Datei/Ordner
Name[es]=Marcar archivo/carpeta
Name[fr]=Marquer le fichier/dossier

[Desktop Action diff]
Icon=arrow-right
Exec=ARG1=$(cat /tmp/meld-menu-arg1);meld "$ARG1" %U; rm /tmp/meld-menu-arg1;
Name=Compare with file/folder
Name[de]=Vergleiche mit Datei/Ordner
Name[es]=Comparar con el archivo/carpeta marcado
Name[fr]=Comparer avec le fichier/dossier marqué

[Desktop Action selected]
Icon=page-2sides
Exec=meld %U; rm /tmp/meld-menu-arg1;
Name=Compare selected files/folder
Name[de]=Vergleiche ausgewählte Dateien/Ordner
Name[es]=Comparar los archivos/carpetas seleccionadas
Name[fr]=Comparer les fichiers/dossiers sélectionnés

1 Like

Thanks, that would help. I have meld, Ark and bunch of others that do this. Just can’t find where their .desktop files are located.

KDE forums (Service Menus Missing • KDE Community Forums) seems to indicate it should be in:

The new KF5 service menus are at:
~/.local/share/kservices5/ServiceMenus/
and
/usr/share/kservices5/ServiceMenus/

Unfortunately, it does not seem to exist there for me. I’ll try running a full search and see where that takes me.

pkg  ~/.local/share/kservices5/ServiceMenus  ls
total 8.0K
-rw-r--r-- 1 pkg pkg 1.5K Jun 12 13:34 open_as_root.desktop
-rw-r--r-- 1 pkg pkg  783 Jun  5 23:28 SetAsWallpaperAndLockScreen.desktop
 pkg  ~/.local/share/kservices5/ServiceMenus  cd /usr/share/kservices5/ServiceMenus/
 pkg  /usr/share/kservices5/ServiceMenus  ls
total 28K
-rw-r--r-- 1 root root 1.4K May 18 11:36 installfont.desktop
-rw-r--r-- 1 root root 2.5K Jun  7 21:44 k3b_create_audio_cd.desktop
-rw-r--r-- 1 root root 1.5K Jun  7 21:44 k3b_create_data_project.desktop
-rw-r--r-- 1 root root 2.6K Jun  7 21:44 k3b_create_video_cd.desktop
-rw-r--r-- 1 root root 2.7K Jun  7 21:44 k3b_write_bin_image.desktop
-rw-r--r-- 1 root root 3.0K Jun  7 21:44 k3b_write_iso_image.desktop
-rw-r--r-- 1 root root 1.8K May 18 11:36 setaswallpaper.desktop
 pkg  /usr/share/kservices5/ServiceMenus  

Use locate. That’s what it’s there for. :stuck_out_tongue:

Does this work? :arrow_down:

Exec=konsole --hold -e "clamdscan --multiscan --fdpass -v \"%U\""