Perform action when clipboard contents match regex

I use the Actions feature of KDE's Klipper utility to run certain commands when I copy something into the clipboard that matches a particular regular expression. Unfortunately, I have to turn on Enable Clipboard Actions and select the command from a menu every time I copy something that I want to run the command on. I'd like to have that command run automatically instead.

You'd think this was possible, as there is an automatic checkbox in the action settings dialog box:

Unfortunately, that appears to do nothing. The documentation included with Klipper does not indicate what that checkbox is supposed to do.

Is it possible to configure Klipper to run a command automatically if the contents of the clipboard matches a regular expression? If not, is there another way to accomplish this?


Solution 1:

I have found no way to tell klipper to apply the action automatically. The most what klipper does automatically is to pop up a menu with the actions. It will take another mouse click to actually apply the action.

The Automatic checkbox in the action properties dialog has the following effect: if you copy a text, and the text matches a configured regular expression, then a menu with the corresponding actions pops up. Note that Enable Clipboard Actions must be enabled for this. If either Enable Clipboard Actions or Automatic is disabled, then no menu will appear upon a copy.

For alternatives: I looked around and found no clipboard manager which does what you want (match regex, modify clipboard content automatically). I tested some clipboard managers which offer similar functionality to klipper. They all do not apply the actions automatically, instead, they offer a popup menu for the configured action, just like klipper.

It seems you will have to code your own solution.

A naive implementation might look like this:

  1. Monitor content of clipboard.
  2. If content changes, match against regex.
  3. If regex matches, modify content of clipboard.

It should be possible to code that using, for example, python and PyGTK or PyQt.

Alternatively you can hack up a shell script using the command xclip -o to dump the content of the clipboard, and xclip -i to insert the modified content into the clipboard.

I found a project that seems to have implemented point number 1 and some of number 2 using perl and the xclip command: https://github.com/trapd00r/clipbored. Maybe you can start form there and modify it to suit your needs.