Score:1

KDE custom menu action on directories with whitespaces in the name

xk flag

I created a script that takes different directories paths to merge the files inside them into one directory. I would exec this scrict by custom Kde-Dolphin action menu, so I created this .desktop file:

[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=inode/directory;
Actions=mergeDir
X-KDE-AuthorizeAction=shell_access

[Desktop Action mergeDir]
Name=Merge this directories
Exec=konsole --noclose -e bash -c '/sc/dirs_merge.py -s %F'
Icon=folder-red

Unfurtuntely the %F doesn't consider the escape character for white spaces, so there is an issue when I use this action with directory with withe spaces in the name, because it's not possibile to discriminate a whitespace in the dir-name between white space as directories separator:

e.g: /transit/mergetest/Test white space 2 /transit/mergetest/Test whitespace 1

Someone knows a solution to this issue?

Thank you.

Score:1
hr flag

The %F placeholder should handle filenames containing spaces just fine - as noted in the freedesktop documentation for The Exec key

%F A list of files. Use for apps that can open several local files at once. Each file is passed as a separate argument to the executable program.

So provided your python script knows how to properly handle multiple arguments, you shouldn't need to involve a bash shell at all - just pass the %F argument list to your python script directly:

Exec=konsole --noclose -e /sc/dirs_merge.py -s %F

If you do really need a bash shell, then pass the arguments as positional parameters and remember to quote the expansion of $@ to prevent word-splitting by the shell:

Exec=konsole --noclose -e bash -c '/sc/dirs_merge.py -s "$@"' bash %F

(the second bash is just to fill the shell's $0 parameter - you can substitute any string there).

Amarabottiglia avatar
xk flag
only one word: wow.
Amarabottiglia avatar
xk flag
Exec=konsole --noclose -e /sc/dirs_merge.py -s %F --> This create the problem, no way to identify arguments with space in the "name". Exec=konsole --noclose -e bash -c '/sc/dirs_merge.py -s "$@"' bash %F --> This is perfetc, in this way each argument is included between two '. Perfect, many many thanks.
Raffa avatar
jp flag
@Amarabottiglia Please see [What should I do when someone answers my question?](https://askubuntu.com/help/someone-answers)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.