Self-destructing clipboard

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
Pearsimmon
Level 1
Level 1
Posts: 27
Joined: Fri Dec 28, 2018 9:49 am

Self-destructing clipboard

Post by Pearsimmon »

I often copy sensitive/private information to clipboard and would like it to automatically clear the clipboard (by copying a single null character to it, for example) a minute after I last copied something. So for example, I copy some text or file, then if I don't copy anything else in 60 seconds from then on, it gets overwritten/deleted. Ideally, I would also want this junk placeholder content not visible in ClipIt's history. Is it possible?
User avatar
Pierre
Level 20
Level 20
Posts: 11846
Joined: Fri Sep 05, 2008 5:33 am
Location: Perth, AU.

Re: Self-destructing clipboard

Post by Pierre »

can't say that I've ever heard of such an Function:
- even on That Other Operating System
:mrgreen:
Image
Please edit your original post title to include [SOLVED] - when your problem is solved!
and DO LOOK at those Unanswered Topics - - you may be able to answer some!.
User avatar
MrEen
Level 23
Level 23
Posts: 18140
Joined: Mon Jun 12, 2017 8:39 pm

Re: Self-destructing clipboard

Post by MrEen »

One way to get started on this is to create a blank file: touch blank

Then when you want the clipboard contents cleared: xclip -selection clipboard blank followed by xclip -selection primary blank

The first line only has to be run once. The second could be scripted and added to a launcher I would think.

NOTE: In linux, there are 3 clipboards, PRIMARY, SECONDARY and CLIPBOARD. The above empties the CLIPBOARD, so right click > paste and Ctrl-V will be useless, but PRIMARY still holds it's contents meaning a middle click can still work. SECONDARY I don't understand and can find almost nothing about.
FreedomTruth
Level 4
Level 4
Posts: 439
Joined: Fri Sep 23, 2016 10:19 am

Re: Self-destructing clipboard

Post by FreedomTruth »

For manual clearing, run

Code: Select all

xsel -cb; xsel -cp; xsel -cs
maybe set it up on a hotkey to run as needed. (requires xsel, not sure if that's installed by default but probably not). The three commands will clear the clipboard, primary selection, and secondary selection. If you usually use the keyboard shortcut ctrl-v to paste, you could map that key combination to

Code: Select all

xsel -ob && xsel -cb
to paste and immediately clear the clipboard contents.

For self-destructing clipboard that will run 1 minute after the clipboard has something in it, you could make a bash script... something along the lines of

Code: Select all

#!/bin/bash

# initially, clear the clipboard, primary and secondary selections
xsel -cb
xsel -cp
xsel -cs

while [ -z "$(xsel -ob)" ]; do
  sleep 1 # check every 1 second to see if clipboard is empty
done

sleep 60 # wait 1 minute
$0 & # restart the program
Set the script to run at startup (make sure it's executable)
User avatar
majpooper
Level 7
Level 7
Posts: 1522
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: Self-destructing clipboard

Post by majpooper »

I have been trying to accomplish the exact same thing as the OP - in effect run xsel -cb automatically at a defined interval. xsel -cb works manually from the terminal and I also have set up a keyboard shortcut that works.
However xsel -cb does not work from cron. I wrote a very simple bash script to execute xsel -cb and placed that in cron
and that does not work. I place a line in the script to make sure the script kicks off - it does but does not execute the xsel -cb command

Code: Select all

#!/bin/bash

#clear the clipboard 
echo "Cron ran clearClipboard.sh at $(date)" >> /home/majpooper/crntest.txt
xsel -cb

Code: Select all

#crontab for root
# m h  dom mon dow   command
0 0 * * * /home/mintcleaner.sh
0 04 * * * /home/purge-old-kernels-2.sh
01 * * * * eject /dev/sdc1
* * * * * /home/clearClipboard.sh
I have tried Freedom's script - same deal - I can kick it off manually and it will work initially but will not run a second time. As well I can not get it to work from cron either.
rene
Level 17
Level 17
Posts: 7773
Joined: Sun Mar 27, 2016 6:58 pm

Re: Self-destructing clipboard

Post by rene »

It is missing the DISPLAY environment variable when run from cron. Just give it its display directly by adding -d :0 to the xsel command.
User avatar
majpooper
Level 7
Level 7
Posts: 1522
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: Self-destructing clipboard

Post by majpooper »

rene wrote:
Fri Feb 01, 2019 7:59 pm
It is missing the DISPLAY environment variable when run from cron. Just give it its display directly by adding -d :0 to the xsel command.
Still not executing from cron - kicks off but does not clear the clipboard.

Code: Select all

#!/bin/bash

#clear the clipboard, primary and secondary selections
echo "Cron ran clearClipboard.sh at $(date)" >> /home/majpooper/crntest.txt
xsel -cb -d :0 
rene
Level 17
Level 17
Posts: 7773
Joined: Sun Mar 27, 2016 6:58 pm

Re: Self-destructing clipboard

Post by rene »

Eew. Wasn't at a Mint machine when posting that; the xsel manpage promises "-d" to be synonymous to "--display" but this does not match the actual program where "-d" is "--delete". Use --display :0, that is.

[EDIT] Upon noticing my local email a bit later, it seems that xsel when run from cron also outputs the current selection even when clearing it. Having a log of say pasted passwords in /var/mail/$(whoami) might not be welcomed by all, so also add ">/dev/null". I.e.,

Code: Select all

<m> <h> <dom> <mon> <dow> xsel -cb --display :0 >/dev/null
User avatar
majpooper
Level 7
Level 7
Posts: 1522
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: Self-destructing clipboard

Post by majpooper »

HOOOOAH!!! rene That works - been trying to solve this for over a year - researched it some and went back to it a few times but but could never figure it out or find an answer. Then the OP brought up the same issue in this tread and BINGO you solved it.

THX much.
rene
Level 17
Level 17
Posts: 7773
Joined: Sun Mar 27, 2016 6:58 pm

Re: Self-destructing clipboard

Post by rene »

As compared to OP's request there's the difference though that clearing selections happens just every, say, 10 seconds if that is what you have set the cron schedule to and including if you at e.g. 9.9 seconds into any such interval selected something. That I would feel to be potentially rather annoying but doing something about it would not be possible with a high-level tool such as xsel. Looking into the issue a bit further this seemed a nice opportunity to both for the first time in a long time do some Xlib programming again and to deploy a systemd user daemon.

If interested, see attached archive of version 0.1 of xselcd (X SELection-Clear Daemon). If you after unpacking run

Code: Select all

rene@t5500:~/src/xselcd$ make
rene@t5500:~/src/xselcd$ sudo make install
rene@t5500:~/src/xselcd$ make start
you will have a user daemon running that by default every 10 seconds clears any of the PRIMARY, SECONDARY and/or CLIPBOARD selections that are at that point more than 10 seconds old. The clearing method is exactly the same as the one used by xsel.

Note, you will at minimum need the build-essential and libx11-dev packages installed to be able to compile it. The daemon installs to /usr/local/bin and the service file to /etc/systemd/user. Use xselcd -h for a usage screen if you want to provide an explicit display and/or clearing interval value in the service file. Due to Mint/Ubuntu (for now?) not using the systemd user target graphical-session.target we launch the service through the desktop autostart as per the .desktop file in /etc/xdg/autostart.

N.B., and explicitly also to OP: due to the manner in which X selections work, a clipboard history viewer such as ClipIt in fact interferes with clearing. Basically the issue is that there is no such thing as (a) clipboard (history); that a clipboard history viewer is not just a window onto pre-existing clipboard history from somewhere but that the viewer is the only thing that maintains said history in the first place. Password managers have that issue as well with their attempts to clear clipboard: basically, don't run a clipboard viewer if you find clipboard history to be sensitive -- but I guess I can try and look into methods if really desired.

Hope this is useful. Was for me certainly in the sense of a systemd user daemon being a rather useful construct. It is/can be basically just a standard little program...
Attachments
xselcd-0.1.tar.gz
(2.06 KiB) Downloaded 52 times
User avatar
majpooper
Level 7
Level 7
Posts: 1522
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: Self-destructing clipboard

Post by majpooper »

You are right the cron approach although automatic is annoying in that if copy near the end of the cycle it is gone too soon - and you have to copy again. The systemd user daemon would be ideal and very slick so I am game.

I have the dependencies build-essential and libx11-dev packages installed and unpacked and installed xselcd - so far so good . . . . I think

Code: Select all

majpooper@1150z:~/Downloads/xselcd-0.1$ make
gcc -W -Wall -O2     xselcd.c  -lX11 -o xselcd
xselcd.c: In function ‘main’:
xselcd.c:69:4: warning: empty declaration
    __attribute__ ((fallthrough));
    ^~~~~~~~~~~~~
sed -e s,@CMD@,/usr/local/bin/xselcd, xselcd.service.template >xselcd.service
sed -e s,@SRV@,xselcd.service, xselcd.desktop.template >xselcd.desktop
majpooper@1150z:~/Downloads/xselcd-0.1$ sudo make install
[sudo] password for majpooper:           
install -Ds xselcd /usr/local/bin/xselcd
install -Dm 644 xselcd.service /etc/systemd/user/xselcd.service
install -Dm 644 xselcd.desktop /etc/xdg/autostart/xselcd.desktop
majpooper@1150z:~/Downloads/xselcd-0.1$ make start
systemctl --user daemon-reload
systemctl --user start xselcd.service
majpooper@1150z:~/Downloads/xselcd-0.1$ xselcd -h

Usage: xselcd [-h] [-d <display>] [-t <seconds>]

User daemon to automatically clear the PRIMARY, SECONDARY and CLIPBOARD X
selections a configurable number of seconds from their selection.

  -h		 : This usage screen
  -d <display>  : The X display (default from the DISPLAY environment variable)
  -t <seconds>  : The clearing interval in seconds (default 10)
I am trying to understand DISPLAY environment variable a little better especially in light of the fact I actually have three "screens" (two 24" monitors and 42" TV)

I did this - I think assuming my primary sceen is :0 and setting the interval for 10 minutes - again I really do not grasp the concept of the DISPLAY environment variable.

Code: Select all

majpooper@1150z:~/Downloads/xselcd-0.1$ xselcd -d :0 -t 600[/code

Last I commented out cron and went to /etc/xdg/autostart and launched the service
Now it is clearing at the default 10 sec. - tried 
[code]majpooper@1150z:~$ xselcd -t 60
still getting the the 10sec default interval
rene
Level 17
Level 17
Posts: 7773
Joined: Sun Mar 27, 2016 6:58 pm

Re: Self-destructing clipboard

Post by rene »

That "empty declaration warning" you get during compilation is a little weird, but oh well, doesn't matter. Yes, you shouldn't be starting anything manually. The make install has installed the system service file to /etc/systemd/user/xselcd.service and only that file you should edit if you'd like to make changes to the command. I.e., edit the file as root and change

Code: Select all

ExecStart=/usr/local/bin/xselcd
to e.g.

Code: Select all

ExecStart=/usr/local/bin/xselcd -t 600
... although a 10 minute clearing interval sounds a little odd. You don't in any case need to use the -d parameter: it is present for situations where the DISPLAY environment variable is not available (i.e., just as it wasn't to xsel when you were executing xsel from cron) but seeing as how the Ubuntu setup for systemd user services is rather badly broken, I elected to start the systemd service through a desktop startup job anyway, i.e., at a point when DISPLAY certainly is available.

This is to say: after the make install the service was installed and set to launch on login. After the additional make start you already launched it also for this session, although with the default 10 second clearing interval. So, stop any manual invocation you have launched (i.e., Ctrl-C in its terminal) and if so desired, edit /etc/systemd/user as detailed above and restart the service with, as your user and not with sudo,

Code: Select all

$ systemctl --user daemon-reload
$ systemctl --user stop xselcd.service
$ systemctl --user start xselcd.service
The desktop file in /etc/xdg/autostart just autostarts the service on login; see it having appeared in Preferences -> Startup Applications which is also where you'd disable it if you no longer want it autostarted.

Note by the way that the DISPLAY variable corresponds basically to "an X server" and is something that can in turn have multiple "consoles", i.e., screens and/or keyboards+mice. For regular local use :0 is basically always the display you want -- and note again that you needn't set it manually in the first place. That all was already in place after the make install and make start and that only if the default 10 seconds doesn't work for you you need to change anything, specifically adding a -t N parameter in /etc/systemd/user/xselcd.service and restarting the service.
User avatar
majpooper
Level 7
Level 7
Posts: 1522
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: Self-destructing clipboard

Post by majpooper »

Thank you so much rene - you are a good teacher. This is perfect I really learned a lot and the and the DISPLAY Environment variable is becoming just a little bit more clear.

I set the interval to 60 for now, 10 seconds was a tad too short for me - 600 is over-kill and way too long and makes little sense.

I hope the OP has followed this and marks it [SOLVED]

THX again rene
User avatar
trytip
Level 13
Level 13
Posts: 4989
Joined: Tue Jul 05, 2016 1:20 pm

Re: Self-destructing clipboard

Post by trytip »

this is my solution (tested in xfce with xfce terminal), not automated it runs when terminal opens from the .bashrc (currently in arch linux haven't tested it in mint) still working on the kinks

Code: Select all

ln -s /dev/null $HOME/.null
open .bashrc and add this to the bottom:

Code: Select all

# Clear clipboard when Terminal opens!
xclip -i /dev/null
xclip -selection clipboard .null

anytime you copy text/files/folders they remain in clipboard until Terminal is open, when it get destroyed to a null. i'm guessing this can be combined with your scripts here for extra protection. you'll need to change habits of copying.

EDIT: works fine in mint 19.1 cinnamon
Image
User avatar
phd21
Level 19
Level 19
Posts: 9973
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Self-destructing clipboard

Post by phd21 »

Hi Pearsimmon,

I just read your post and the good replies to it. Here are my thoughts on this as well.

It would help to know more about your system setup. If you run "inxi -Fxzd" from the console terminal prompt, highlight the results, copy and paste them back here, that should provide enough information.

If you have not installed a clipboard manager, you might consider installing one like CopyQ, ClipIt, etc... Where you can easily delete (clear) the history (usually) or not save it, and you can create custom actions, etc... I use KDE's Klipper and I can just click it, then click the broom in the upper right and clear the history.

Having an auto-clear function would be cool.

Clipboard - ArchWiki
https://wiki.archlinux.org/index.php/clipboard

6 Linux clipboard managers to boost your productivity | Opensource.com
https://opensource.com/article/17/6/clipboard-managers
Install CopyQ Clipboard Manager 3.2 in Ubuntu 18.04/16.04
http://tipsonubuntu.com/2018/02/18/inst ... 8-0416-04/

CopyQ -An Advanced Clipboard Manager for Linux
https://www.fossmint.com/copyq-an-advan ... for-linux/

Welcome to CopyQ’s documentation! — CopyQ documentation
https://copyq.readthedocs.io/en/latest/
GitHub - CristianHenzel/ClipIt: ClipIt clipboard manager for GTK+
https://github.com/CristianHenzel/ClipIt


Hope this helps ...
Phd21: Mint 20 Cinnamon & xKDE (Mint Xfce + Kubuntu KDE) & KDE Neon 64-bit (new based on Ubuntu 20.04) Awesome OS's, Dell Inspiron I5 7000 (7573) 2 in 1 touch screen, Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram, Intel 4 Graphics.
rene
Level 17
Level 17
Posts: 7773
Joined: Sun Mar 27, 2016 6:58 pm

Re: Self-destructing clipboard

Post by rene »

majpooper wrote:
Mon Feb 04, 2019 11:59 am
Thank you so much rene
Glad it helped. Let me only add then that in order to uninstall the daemon again:

Code: Select all

$ systemctl --user stop xselcd.service
$ sudo rm /etc/xdg/autostart/xselcd.desktop /etc/systemd/user/xselcd.service /usr/local/bin/xselcd
I was thinking of making the thing DBus based with a Cinnamon applet to in fact dynamically set the interval and/or pause/resume the daemon, even if only so as to have an excuse to experiment with DBus based service invocation (i.e., to replace the autostart .desktop method) but I'll need some time to be in fact be able to do that, Cinnamon applets being Javascript and me rather decidedly not. But if it would be useful, to you or OP, I guess I'll look into it.
User avatar
majpooper
Level 7
Level 7
Posts: 1522
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: Self-destructing clipboard

Post by majpooper »

I don't know where the OP went because this solves his issue as well as mine in that your solution is automated. Automation is the key element otherwise I have a hot-key which is not the solution. I have several very basic scripts I run in cron just for security/house cleaning - one that cleans out my Downloads folder every night, empties the trash, deletes thumbnails, un-mounts USB etc. The point I guess is that I don't have to remember to clear the clipboard or empty the trash etc. You have solved the clipboard automation. I just did not like the idea of having passwords lying around on the clipboard that I forgot to clear.

A Cinnamon applet would be awesome (but huge amount of work) - setting the interval now I know how is easy - 10sec was a bit short, 60sec is working but just a tad too long maybe - I will probably experiment until I find the optimum time. But I can imagine situations where pause/resume would be ideal as there are times when copy/paste big files and editing clearing the clipboard so soon may be a problem or just wanting to set the interval on the fly.
rene
Level 17
Level 17
Posts: 7773
Joined: Sun Mar 27, 2016 6:58 pm

Re: Self-destructing clipboard

Post by rene »

OK. I'll look into it; should have time later this week or next weekend. Will get back to you!
rene
Level 17
Level 17
Posts: 7773
Joined: Sun Mar 27, 2016 6:58 pm

Re: Self-destructing clipboard

Post by rene »

Post Reply