Is there any method which allows to detect whether some files and/or folders are selected in the right pane of Dolphin or selection is empty?
I’m working on service menu and my script should behave differently depending on condition: if some objects are selected, then it should do one action, if nothing is selected, then it should do another action. If some objects are selected, their paths are transferred to the script as a positional parameter. Sadly, if nothing is selected, current folder path is still transferred to the script as a parameter, so I cannot detect within my script whether selection was empty or not.
I checked through Dolphin’s properties using qdbus, but found nothing related to current selection. Maybe I missed something?
if you are talking about the info panel (F11), it’s contents are not simply determined by what is selected, its more complicated that that.
generally you pass the selection to your service menu by %f or %F as described by this document
service menus are only available by right clicking on an item and that means it is already selected… i’m not aware of how to evoke a service menu without right clicking on something
No, I’m talking about ordinary panel, where folders and files are displayed, not the side pane, which opens on F11.
Service menu is injected into context menu, and if you set MimeType=all/all parameter in the .desktop file which describes this service menu, it will appear always, if you click with right mouse button on some object or if you click on empty space somewhere between the files. Combining MimeTypes for folders and files in .desktop file may help in my case. There exists MimeType for folders (inode/directory), but I’m not aware of MimeType which includes all types of files.
According to the documentation, there’s a parameter X-KDE-MinNumberOfUrls which sets the limit of selected files. But it doesn’t work reliably. When I set it to X-KDE-MinNumberOfUrls=2, it works as intended: service menu is hidden when one object or nothing is selected, and appears when two files or folders are selected. When I set it to X-KDE-MinNumberOfUrls=1 (which is satisfactory in my case), service menu is shown always, even if nothing is selected. Maybe it’s a bug.
AFAIR, Gnome’s Nautilus returned the list of selected objects, if they were selected and returned empty list, if nothing was selected. Dolphin behaves differently, it always returns something to the script attached to this service menu. That’s the problem.
I don’t think you can get the current selection in Dolphin programmatically right now.
I had a look some time ago, I wanted to query the current selection so I could send it via DBUS to Gnome Sushi, and thus get a working Apple QuickView type thing. I gave up quite quickly, seems Dolphin can’t even send the selection to the embedded console.
Perhaps this project can provide ideas.
It faces similar challenges and I see there are commits related to DBUS, I’d presume there is need to get the selection from Dolphin, send it to Kiview, but keep the focus on Dolphin so you can change the selection using Arrow keys, or the other way round.
Actually, that’s not a problem to obtain the list of selected files for me, as this list is transferred to the script which is set in Exec parameter of .desktop file as positional parameters.
So in my Bash script I can use $1 to get the full path to the 1st selected object, $2 to get the second one and so on, and $@ gives me the full list. I suppose, any other scripting language will receive this list as positional parameters, without any DBUS calls.
My problem is that $1 gets populated even with empty selection.
The selection will never be accessible through dbus. That’s on purpose.
That’s a security hazard if any program can poke at the current files you are working on in dolphin.
This.
We would need a new option as X-KDE-MinNumberOfUrls, something like X-KDE-IgnoreCWDX-KDE-SkipCurrentWorkdingDirectory.
I agree, that exposing the selection to DBUS in a form of a list would be imprudent. I meant that there may exist some boolean property which changes its value on empty selection / non-empty selection–that would be sufficient. It seems that there’s no such property.
The idea to pass current working directory if nothing is selected and X-KDE-MinNumberOfUrls behavior look a bit counterintuitive, but that’s only my opinion.
Okay, the topic is still open for proposals.
Well, that’s another issue. Indeed, in case of empty selection %F returns current folder, opened in Dolphin, to the script. But $PWD, which is called from the script, returns not the same folder, but its parent folder instead.
For example: I run my script with Exec=runme.sh in .desktop file and this script contains the next code:
In case of empty selection these folders won’t match.
I tried $(pwd) and $(realpath .) instead of $(echo $PWD), the result is the same. It escapes me, what’s the logic behind this.
I’m not familiar with service menus. Can you point me to some docs?
I regularly write scripts and I use the file type option to get them included in the context menu under Open with. That works great and with %F they even get multiple selections onto the command line as argumentis.
This obviously looks like a bug or some design defect.
I don’t think that this is related to konsole somehow, because my service menu calls shell script which doesn’t invoke konsole and I observe the same behavior.
sounds as if your script would need to do whatever it is that this --workdir switch does in konsole which would require examining the konsole source code, which is over my head.
i’ve found using kdialog --msgbox $(echo $PWD) windows to helpful to debug scripts
I found a way to work around this “API weakness” and detect empty selection in Dolphin. This method depends on “Show full path in title bar” option, which is located in Settings | Startup (and which I activate at once when I start using Dolphin).
If I cannot get current working directory with $PWD, then I can get it from the program’s titlebar using qdbus. I also added a loop to detect active Dolphin window if several instances of the program were started (found hint here):
for SERVICE in $(qdbus org.kde.dolphin-*)
do
ACTIVE=$(qdbus $SERVICE /dolphin/Dolphin_1 org.qtproject.Qt.QWidget.isActiveWindow)
$ACTIVE && DIR=$(qdbus $SERVICE /dolphin/Dolphin_1 org.qtproject.Qt.QWidget.windowTitle)
done
if [[ $DIR = $1 ]]
then kdialog --title "Info" --msgbox "Selection is empty"
else kdialog --title "Info" --msgbox "Something is selected"
fi
A bit cumbersome, but works reliably. If someone knows more straightforward way, please share it.
This info bar text should be exposed to qdbus in order to be analyzed, but I haven’t found anything resembling “selected” among the properties of Dolphin’s service.