trying to clear the clipboard

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'd like to automatically clear the clipboard a few seconds after use.



#!/usr/bin/env bash

LOCKFILE=/tmp/.clearclip-lock
if [ -e $LOCKFILE ] && kill -0 `cat $LOCKFILE`; then
exit 1
fi
trap "rm -f $LOCKFILE; exit" INT TERM EXIT
touch $LOCKFILE
echo $$ > $LOCKFILE

if xclip -o -selection clipboard 1>&2 2>/dev/null; then
if watch -n 0.5 -t --chgexit xclip -o -selection clipboard @>/dev/null; then
sleep 10
xsel -bc
fi
fi

rm -f $LOCKFILE


I've only just started playing around with this crude executable file called ~/mypath/clearclip which I'd like to trigger with a user local systemd timer. Anyway, the service throws an error Error opening terminal: unknown.



# ~/.local/share/systemd/user/clearclip.service
[Unit]
Description=clear the clipboard
ConditionFileIsExecutable=%h/_path/clearclip.sh

[Service]
Environment=DISPLAY=:0
ExecStart=%h/_path/clearclip.sh
Type=oneshot


My main question is:



Is there a tool to perform the same-but-non-interactive functionality of watch -g? What's your approach? Would you write the output to tmp files in a while loop to compare them, revert to expect, or something else?



Another question is: How would a dual-timer setup look like?



e.g. timer would check when the clipboard changes and trigger (or restart) another timer which clears the clipboard selection



Edit 25/07:



I've given up on using this script with a user timer this week. I was playing around with zpty to emulate a terminal to get rid of the opening terminal error but I ended up just putting a clearclip & in ~/.config/zsh/.zlogin.



#!/usr/bin/env zsh
# zmodload zsh/zpty

oclip=""
let count='-1'
let timeout=70

clipchanged()
if ! xclip -o -selection clipboard 2>/dev/null 1>&2; then
count='-1'
return 1
fi
clip="$(xclip -o -selection clipboard)"
if [[ -z "$clip" ]]

while true; do
if (( count > 0 )); then
((count--))
# echo -n "r33[K$count"
fi
if (( count == 0 )); then
xsel -bc
fi
if clipchanged; then
(( count=timeout ))
fi
sleep .5
done






share|improve this question

















  • 3




    Is this only about the X clipboard? Why use systemd? Can the service even run the xclip command? You'd need to at least export the DISPLAY, XAUTHORITY etc variables. I've never played with this, but I guess you'd need a similar approach to what is needed for cron. Have a look at Using notify-send with cron
    – terdon♦
    Jul 12 at 16:31






  • 1




    Note that in principle you could run a dozen X servers on your computer, with users using them remotely etc. You need some way to associate this "service" to a single user with a single X server in a single session, so the appropriate place to start this would be during X login, not in systemd. And yes, the X server sends events when the selection changes, but I don't know any ready-made commandline tools that can hook into that.
    – dirkt
    Jul 13 at 6:26










  • Thank you both for your making the problem clear, I was quite naive. I have a single user running wayland on Arch linux, only two programs require Xwayland, and I never use the graphical session remotely. The point is not to store information on the X clipboard indefinitely. Indeed, I thought I'd like a systemd user unit so that I may at some point bind the unit to additional sessions run by different users. But it may be better to start a process to directly hook into an event.
    – Bart
    Jul 13 at 7:51










  • I tried to include the environment as suggested in the liked answer, but I think either xclip, watch or xsel expect a terminal to connect to. I've had no luck setting StandardInput= and StandardOutput=.
    – Bart
    Jul 13 at 8:10














up vote
1
down vote

favorite












I'd like to automatically clear the clipboard a few seconds after use.



#!/usr/bin/env bash

LOCKFILE=/tmp/.clearclip-lock
if [ -e $LOCKFILE ] && kill -0 `cat $LOCKFILE`; then
exit 1
fi
trap "rm -f $LOCKFILE; exit" INT TERM EXIT
touch $LOCKFILE
echo $$ > $LOCKFILE

if xclip -o -selection clipboard 1>&2 2>/dev/null; then
if watch -n 0.5 -t --chgexit xclip -o -selection clipboard @>/dev/null; then
sleep 10
xsel -bc
fi
fi

rm -f $LOCKFILE


I've only just started playing around with this crude executable file called ~/mypath/clearclip which I'd like to trigger with a user local systemd timer. Anyway, the service throws an error Error opening terminal: unknown.



# ~/.local/share/systemd/user/clearclip.service
[Unit]
Description=clear the clipboard
ConditionFileIsExecutable=%h/_path/clearclip.sh

[Service]
Environment=DISPLAY=:0
ExecStart=%h/_path/clearclip.sh
Type=oneshot


My main question is:



Is there a tool to perform the same-but-non-interactive functionality of watch -g? What's your approach? Would you write the output to tmp files in a while loop to compare them, revert to expect, or something else?



Another question is: How would a dual-timer setup look like?



e.g. timer would check when the clipboard changes and trigger (or restart) another timer which clears the clipboard selection



Edit 25/07:



I've given up on using this script with a user timer this week. I was playing around with zpty to emulate a terminal to get rid of the opening terminal error but I ended up just putting a clearclip & in ~/.config/zsh/.zlogin.



#!/usr/bin/env zsh
# zmodload zsh/zpty

oclip=""
let count='-1'
let timeout=70

clipchanged()
if ! xclip -o -selection clipboard 2>/dev/null 1>&2; then
count='-1'
return 1
fi
clip="$(xclip -o -selection clipboard)"
if [[ -z "$clip" ]]

while true; do
if (( count > 0 )); then
((count--))
# echo -n "r33[K$count"
fi
if (( count == 0 )); then
xsel -bc
fi
if clipchanged; then
(( count=timeout ))
fi
sleep .5
done






share|improve this question

















  • 3




    Is this only about the X clipboard? Why use systemd? Can the service even run the xclip command? You'd need to at least export the DISPLAY, XAUTHORITY etc variables. I've never played with this, but I guess you'd need a similar approach to what is needed for cron. Have a look at Using notify-send with cron
    – terdon♦
    Jul 12 at 16:31






  • 1




    Note that in principle you could run a dozen X servers on your computer, with users using them remotely etc. You need some way to associate this "service" to a single user with a single X server in a single session, so the appropriate place to start this would be during X login, not in systemd. And yes, the X server sends events when the selection changes, but I don't know any ready-made commandline tools that can hook into that.
    – dirkt
    Jul 13 at 6:26










  • Thank you both for your making the problem clear, I was quite naive. I have a single user running wayland on Arch linux, only two programs require Xwayland, and I never use the graphical session remotely. The point is not to store information on the X clipboard indefinitely. Indeed, I thought I'd like a systemd user unit so that I may at some point bind the unit to additional sessions run by different users. But it may be better to start a process to directly hook into an event.
    – Bart
    Jul 13 at 7:51










  • I tried to include the environment as suggested in the liked answer, but I think either xclip, watch or xsel expect a terminal to connect to. I've had no luck setting StandardInput= and StandardOutput=.
    – Bart
    Jul 13 at 8:10












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'd like to automatically clear the clipboard a few seconds after use.



#!/usr/bin/env bash

LOCKFILE=/tmp/.clearclip-lock
if [ -e $LOCKFILE ] && kill -0 `cat $LOCKFILE`; then
exit 1
fi
trap "rm -f $LOCKFILE; exit" INT TERM EXIT
touch $LOCKFILE
echo $$ > $LOCKFILE

if xclip -o -selection clipboard 1>&2 2>/dev/null; then
if watch -n 0.5 -t --chgexit xclip -o -selection clipboard @>/dev/null; then
sleep 10
xsel -bc
fi
fi

rm -f $LOCKFILE


I've only just started playing around with this crude executable file called ~/mypath/clearclip which I'd like to trigger with a user local systemd timer. Anyway, the service throws an error Error opening terminal: unknown.



# ~/.local/share/systemd/user/clearclip.service
[Unit]
Description=clear the clipboard
ConditionFileIsExecutable=%h/_path/clearclip.sh

[Service]
Environment=DISPLAY=:0
ExecStart=%h/_path/clearclip.sh
Type=oneshot


My main question is:



Is there a tool to perform the same-but-non-interactive functionality of watch -g? What's your approach? Would you write the output to tmp files in a while loop to compare them, revert to expect, or something else?



Another question is: How would a dual-timer setup look like?



e.g. timer would check when the clipboard changes and trigger (or restart) another timer which clears the clipboard selection



Edit 25/07:



I've given up on using this script with a user timer this week. I was playing around with zpty to emulate a terminal to get rid of the opening terminal error but I ended up just putting a clearclip & in ~/.config/zsh/.zlogin.



#!/usr/bin/env zsh
# zmodload zsh/zpty

oclip=""
let count='-1'
let timeout=70

clipchanged()
if ! xclip -o -selection clipboard 2>/dev/null 1>&2; then
count='-1'
return 1
fi
clip="$(xclip -o -selection clipboard)"
if [[ -z "$clip" ]]

while true; do
if (( count > 0 )); then
((count--))
# echo -n "r33[K$count"
fi
if (( count == 0 )); then
xsel -bc
fi
if clipchanged; then
(( count=timeout ))
fi
sleep .5
done






share|improve this question













I'd like to automatically clear the clipboard a few seconds after use.



#!/usr/bin/env bash

LOCKFILE=/tmp/.clearclip-lock
if [ -e $LOCKFILE ] && kill -0 `cat $LOCKFILE`; then
exit 1
fi
trap "rm -f $LOCKFILE; exit" INT TERM EXIT
touch $LOCKFILE
echo $$ > $LOCKFILE

if xclip -o -selection clipboard 1>&2 2>/dev/null; then
if watch -n 0.5 -t --chgexit xclip -o -selection clipboard @>/dev/null; then
sleep 10
xsel -bc
fi
fi

rm -f $LOCKFILE


I've only just started playing around with this crude executable file called ~/mypath/clearclip which I'd like to trigger with a user local systemd timer. Anyway, the service throws an error Error opening terminal: unknown.



# ~/.local/share/systemd/user/clearclip.service
[Unit]
Description=clear the clipboard
ConditionFileIsExecutable=%h/_path/clearclip.sh

[Service]
Environment=DISPLAY=:0
ExecStart=%h/_path/clearclip.sh
Type=oneshot


My main question is:



Is there a tool to perform the same-but-non-interactive functionality of watch -g? What's your approach? Would you write the output to tmp files in a while loop to compare them, revert to expect, or something else?



Another question is: How would a dual-timer setup look like?



e.g. timer would check when the clipboard changes and trigger (or restart) another timer which clears the clipboard selection



Edit 25/07:



I've given up on using this script with a user timer this week. I was playing around with zpty to emulate a terminal to get rid of the opening terminal error but I ended up just putting a clearclip & in ~/.config/zsh/.zlogin.



#!/usr/bin/env zsh
# zmodload zsh/zpty

oclip=""
let count='-1'
let timeout=70

clipchanged()
if ! xclip -o -selection clipboard 2>/dev/null 1>&2; then
count='-1'
return 1
fi
clip="$(xclip -o -selection clipboard)"
if [[ -z "$clip" ]]

while true; do
if (( count > 0 )); then
((count--))
# echo -n "r33[K$count"
fi
if (( count == 0 )); then
xsel -bc
fi
if clipchanged; then
(( count=timeout ))
fi
sleep .5
done








share|improve this question












share|improve this question




share|improve this question








edited Jul 25 at 18:36
























asked Jul 12 at 16:08









Bart

85




85







  • 3




    Is this only about the X clipboard? Why use systemd? Can the service even run the xclip command? You'd need to at least export the DISPLAY, XAUTHORITY etc variables. I've never played with this, but I guess you'd need a similar approach to what is needed for cron. Have a look at Using notify-send with cron
    – terdon♦
    Jul 12 at 16:31






  • 1




    Note that in principle you could run a dozen X servers on your computer, with users using them remotely etc. You need some way to associate this "service" to a single user with a single X server in a single session, so the appropriate place to start this would be during X login, not in systemd. And yes, the X server sends events when the selection changes, but I don't know any ready-made commandline tools that can hook into that.
    – dirkt
    Jul 13 at 6:26










  • Thank you both for your making the problem clear, I was quite naive. I have a single user running wayland on Arch linux, only two programs require Xwayland, and I never use the graphical session remotely. The point is not to store information on the X clipboard indefinitely. Indeed, I thought I'd like a systemd user unit so that I may at some point bind the unit to additional sessions run by different users. But it may be better to start a process to directly hook into an event.
    – Bart
    Jul 13 at 7:51










  • I tried to include the environment as suggested in the liked answer, but I think either xclip, watch or xsel expect a terminal to connect to. I've had no luck setting StandardInput= and StandardOutput=.
    – Bart
    Jul 13 at 8:10












  • 3




    Is this only about the X clipboard? Why use systemd? Can the service even run the xclip command? You'd need to at least export the DISPLAY, XAUTHORITY etc variables. I've never played with this, but I guess you'd need a similar approach to what is needed for cron. Have a look at Using notify-send with cron
    – terdon♦
    Jul 12 at 16:31






  • 1




    Note that in principle you could run a dozen X servers on your computer, with users using them remotely etc. You need some way to associate this "service" to a single user with a single X server in a single session, so the appropriate place to start this would be during X login, not in systemd. And yes, the X server sends events when the selection changes, but I don't know any ready-made commandline tools that can hook into that.
    – dirkt
    Jul 13 at 6:26










  • Thank you both for your making the problem clear, I was quite naive. I have a single user running wayland on Arch linux, only two programs require Xwayland, and I never use the graphical session remotely. The point is not to store information on the X clipboard indefinitely. Indeed, I thought I'd like a systemd user unit so that I may at some point bind the unit to additional sessions run by different users. But it may be better to start a process to directly hook into an event.
    – Bart
    Jul 13 at 7:51










  • I tried to include the environment as suggested in the liked answer, but I think either xclip, watch or xsel expect a terminal to connect to. I've had no luck setting StandardInput= and StandardOutput=.
    – Bart
    Jul 13 at 8:10







3




3




Is this only about the X clipboard? Why use systemd? Can the service even run the xclip command? You'd need to at least export the DISPLAY, XAUTHORITY etc variables. I've never played with this, but I guess you'd need a similar approach to what is needed for cron. Have a look at Using notify-send with cron
– terdon♦
Jul 12 at 16:31




Is this only about the X clipboard? Why use systemd? Can the service even run the xclip command? You'd need to at least export the DISPLAY, XAUTHORITY etc variables. I've never played with this, but I guess you'd need a similar approach to what is needed for cron. Have a look at Using notify-send with cron
– terdon♦
Jul 12 at 16:31




1




1




Note that in principle you could run a dozen X servers on your computer, with users using them remotely etc. You need some way to associate this "service" to a single user with a single X server in a single session, so the appropriate place to start this would be during X login, not in systemd. And yes, the X server sends events when the selection changes, but I don't know any ready-made commandline tools that can hook into that.
– dirkt
Jul 13 at 6:26




Note that in principle you could run a dozen X servers on your computer, with users using them remotely etc. You need some way to associate this "service" to a single user with a single X server in a single session, so the appropriate place to start this would be during X login, not in systemd. And yes, the X server sends events when the selection changes, but I don't know any ready-made commandline tools that can hook into that.
– dirkt
Jul 13 at 6:26












Thank you both for your making the problem clear, I was quite naive. I have a single user running wayland on Arch linux, only two programs require Xwayland, and I never use the graphical session remotely. The point is not to store information on the X clipboard indefinitely. Indeed, I thought I'd like a systemd user unit so that I may at some point bind the unit to additional sessions run by different users. But it may be better to start a process to directly hook into an event.
– Bart
Jul 13 at 7:51




Thank you both for your making the problem clear, I was quite naive. I have a single user running wayland on Arch linux, only two programs require Xwayland, and I never use the graphical session remotely. The point is not to store information on the X clipboard indefinitely. Indeed, I thought I'd like a systemd user unit so that I may at some point bind the unit to additional sessions run by different users. But it may be better to start a process to directly hook into an event.
– Bart
Jul 13 at 7:51












I tried to include the environment as suggested in the liked answer, but I think either xclip, watch or xsel expect a terminal to connect to. I've had no luck setting StandardInput= and StandardOutput=.
– Bart
Jul 13 at 8:10




I tried to include the environment as suggested in the liked answer, but I think either xclip, watch or xsel expect a terminal to connect to. I've had no luck setting StandardInput= and StandardOutput=.
– Bart
Jul 13 at 8:10















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454935%2ftrying-to-clear-the-clipboard%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454935%2ftrying-to-clear-the-clipboard%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos