Execute custom command on current selected file/folder

System Details →

  • Arch Linux
  • Hyprland

There is a program - previewqt - can be used to quick preview files, similar to quicklook on mac and windows.

I want to bind a keyboad shortcut ( Space ), when pressed, it will execute the command previewqt $(current-selected-file/folde)

Any way to achieve this?

It is not possible, in Dolphin, to assign keyboard shortcuts to custom actions. Its not really all that possible to add custom actions to Dolphin - except through service menus.

Theoretically, you could create a service menu that runs whatever thing you want, and then use something like Autokey to bind a global shortcut to a macro that will open the service menu and launch your action - this Ask Ubuntu answer suggests such a scenario, but this will only work in you are running on X11.

For a real implementation, you may want to subscribe to the bug report 260266 – Allow assigning keyboard shortcuts to service menu actions and wait for someone to do this (or code it yourself).

I used to use dolphin quick view, a similar app. You could assign a shortcut ( aside from a servicemenu) for that one. https://github.com/Nyre221/dolphin-quick-view

1 Like

The approach implemented in the “dolphin quick view” project is actually very interesting:

They have a helper script here: dolphin-quick-view/package creation/quick_view_package/dolphin_quick_view_shortcut.sh at main · Nyre221/dolphin-quick-view · GitHub that implements the “global shortcut activates action on currently selected item in Dolphin”.

What it does, which I didn’t know was possible, is like this:

  1. Supposedly you have the script installed somewhere on your system, and you have set up a custom global keyboard shortcut (in System Settings - Shortctus) to run this script.
  2. When you trigger the script it will look up all the Dolphin windows through D-Bus, and for each instance - use D-Bus to ask it if it is the active window, which I didn’t know was possible and looks like a generic Qt behavior that can possibly be applied to all KDE/Qt applications.
  3. When it finds a Dolphin window that is active, it will use D-Bus again to ask it to copy the location of the currently selected item to the clipboard.
  4. It will then launch whatever application it wanted with the location of the selected item.

It also tries to save the original content of the clipboard and restore it later - which is really nice, but it will only work for text content in the clipboard.

This is pretty smart!

@kageyama - maybe you want to try to adopt this to your needs.

Yeah, good stuff that one. Maintained too.