Clear KDE clipboard (Klipper) N seconds after something was last added to it

Is there a way to clear the clipboard on KDE (Klipper) N seconds after something was last added to it?

@Kresimir can give you script which clears everything in Klipper, you’d just have to add “n seconds after last event” part :upside_down_face:

Here it is:

1 Like

As long as this script is running, it will check your clipboard every half a second, and if it is not empty, it will wait 5 seconds and then clear clipboard.

#!/bin/bash
while true; do
  while true; do
    if [ -n "$(qdbus org.kde.klipper /klipper org.kde.klipper.klipper.getClipboardContents)" ]; then
      break
    fi
    sleep 0.5s
  done
  sleep 5s
  qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory
done

Thank you.

Should I just put that in ~/.config/autostart-scripts/?

Well, you can if that’s what you want… But it might become very annoying very quickly :rofl:

I would first suggest having a terminal open with that script running (so you can easily kill it with Ctrl+C if it gets in the way). And then, if that’s really what you want, sure, add it to autostart and keep it running all the time. It should not consume too much resources.

I was planning on stretching out the timers a little. So 30s or a minute seems reasonable. Will experiment. Might increase the checking interval a little too.

1 Like

Hmm… I might need to tweak it a bit. Currently, it whacks the clipboard 5s from the first entry being picked up, as opposed to 5s from the last object being added.

You could make it a systemd service or a timer: it’s not too difficult to do so.

Just keep in mind: this gets the contents of your clipboard:

qdbus org.kde.klipper /klipper org.kde.klipper.klipper.getClipboardContents

and this clears the clipboard:

qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory

Just :duck::duck::running_man:t3: for “how to set up a systemd timer”.

1 Like

Thanks. Will play around with the script at the weekend & see what I can come up with.

Here is a modification. Every time clipboard contents changes, it starts counting from 1 to 10 and then clears the clipboard. It checks every 0.5s. Tweak the values as you like, of course.

#!/bin/bash
while true; do
  sleep 0.5s
  if [ -n "$(qdbus org.kde.klipper /klipper org.kde.klipper.klipper.getClipboardContents)" ]; then
    if [ "$CONTENTS" != "$(qdbus org.kde.klipper /klipper org.kde.klipper.klipper.getClipboardContents)" ]; then
      counter=0
    fi
    if [ $counter -ge 10 ]; then
      qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory
      unset CONTENTS
      counter=0
    fi
    counter=$((counter + 1))
    CONTENTS=$(qdbus org.kde.klipper /klipper org.kde.klipper.klipper.getClipboardContents)
  fi
done
1 Like

Looks good, but aren’t you missing a sleep 1s command to pace out the counters?

The one sleep 0.5s is enough. It will increment the counter every 0.5s (except between 0 and 1, this happens “instantly”). So it counts from 1 to 10, when it reaches 10 it clears the clipboard. If contents changes, it starts from 1 again.

Cool. Thanks again.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.