Re: Klipper help sought: fully automatic action to replace a clipboard entry
- Date: Tue, 30 Oct 2018 04:10:38 +0000 (UTC)
- From: Duncan <1i5t5.duncan@xxxxxxx>
- Subject: Re: Klipper help sought: fully automatic action to replace a clipboard entry
J.B. Nicholson posted on Sun, 28 Oct 2018 19:58:39 -0500 as excerpted:
> I didn't notice a more appropriate mailing list to ask about this. If
> there is one, please point me to that mailing list.
> I'd like to fully automatically modify a textual clipboard item using
> Klipper and put the modified item back on the clipboard in its modified
> state so I can paste that item where needed.
> I'd like to do all of this without answering any prompt from Klipper. I
> don't want to manually start the command, I want the command to run
> anytime the top (most recently copied) text item matches a command's
> regular expression.
> Just in case this sounds like it could trigger an endless loop: I've
> made sure that the matching regular expression will never match the
> modified output so there will be no endless loop resulting from this
> modification putting matching text back on the clipboard.
> I've also got only one command in my list of commands so there's no
> question which command should run -- if the copied text matches, the
> command I've got is the command to run.
> Can Klipper do this?
> If so, how do I make Klipper run the command for the matching regular
> expression and not prompt me?
AFAIK (as a user with admin-tech and command-line scripting, not
developer-tech, skills), pure klipper won't provide a fully automated
solution by itself, but I'm quite confident I could hack up a bash-script
style solution that would do it, were I to need to do so, just as I've
hacked up solutions to other kde/plasma-related automation problems I've
had in the past.
The following assumes you have enough familiarity with the bash command-
line interface and scripting to follow along and take it from there...
xclip is one of an entire family of generic X-based utility tools that
make X properties and settings available to the command-line, and thus
bash scripts. While I don't currently have it installed, it's easy
enough to google its manpage, and it looks like the perfect tool to
access the X clipboard from the commandline, allowing scripting of the
clipboard as desired.
While it wouldn't be the least resource intensive solution, I can imagine
periodically (using sleep to control the period) polling the clipboard
content using xclip, feeding the result to grep (or even just a pure bash
pattern match) to see if it fits the desired pattern and using grep's
exit status in a bash conditional, returning to the sleep/poll loop
immediately if it doesn't match.
Once the condition matches, a sed command could do the desired
substitution, and xclip could be used once again to put the altered
content back on the clipboard. The script would then return to the sleep/
poll loop, waiting for the next poll and test period.
This script could then either be started whenever you do whatever task it
is that you need the automated clipboard magic for (and stopped when you
are done), thus avoiding the script's resource usage when you don't need
it, or it could be started along with X or plasma using one of several
available autostart mechanisms (several of which are visible in the
autostart section of kde/plasma system settings) as appropriate.
Duncan - List replies preferred. No HTML msgs.
"Every nonfree program has a lord, a master --
and if you use the program, he is your master." Richard Stallman