A Little Clipboard Daemon




 # execute in wish from path \
 exec wish "$0" ${1+"[email protected]"}

 # clipserver.tcl - Keeps an eye on everything that is copied to the clipboard 
 #                      and doesn't let it disappear in the ether if the 
 #                      application it was copied from exits.
 # Author: Luciano Espirito Santo 
 # More on the purpose of this program:
 # In Linux, when we copy text from an application and close that application, 
 # we often lose that content. It is no longer available to be pasted 
 # elsewhere. That can easily become a nightmare if you're used to Windows' 
 # clipboard because in that OS clipboard content does not evaporate when we 
 # close an application. The content remains faithfully available to all and 
 # sundry until something else is copied and replaces it.
 # This program watches all changes that occur in the system's clipboard and 
 # retains the content so it never gets lost until something else is copied.
 # Calling it a "server" may be an exaggeration, but extending this code 
 # would be a simple task. One could even transmit the clipboard content 
 # over a network or sockets, if one can find an actual use for that.
 # Post edit: now it strikes me that perhaps I should have called it
 # "A little clipboard daemon". Too bad we can't rename wikit pages. :-(
 # History 
 #      Version 1.0     2005-02-24      Luciano Espirito Santo
 #      First version. Alpha stage.
 #      Version 1.01    2005-02-28      Michael Kirkham
 #      Improved with "hash bang" line method suggested at https://wiki.tcl-lang.org/812.
 #      KNOWN ISSUES: 
 #      It will work in Windows too, but Windows users simply don't need it.
 #      This program offers no guarantees! Use it at your own risk!
 #      TO DO: 
 #      - Update this code. The version I actually run on my desktop
 #        nowadays (October 2009) is quite different, and it needs a 
 #        fair amount of adjustment before it can be shared.
 #      LICENSE: BSD
 # How to use it:
 # Just run it with wish. It has to be wish, not tclsh. Keep it running all 
 # the time. Suggestion: run it from KDE's Autostart folder in your home 
 # directory. It runs invisibly. No window or widget is displayed.

 # ================================================
 # packages: none 
 # {refresh clipboard}: the single proc and core of this application 
 # ================================================
 # proc 1 of 1
 proc {refresh clipboard} {}     {
 global myClip1  myClip2

        # Sometimes the clipboard contains something else other than text and 
        # that causes an error. In that case, we trap the error and assign to 
        # myClip1 the value of myClip2, because myClip2 always has a copy of the 
        # last valid content.

        if  { [ catch { set myClip1 [ selection get -selection CLIPBOARD ] } myError ] !=0 } { 
            set  myClip1  $myClip2 

        # If myClip1 is not empty, i.e. received "good" content from myClip2, or 
        # even better, found new valid content by itself, we put that good 
        # content into myClip2, so that myClip2 always has the newest valid 
        # content. If the clipboard receives bad content and myClip1 chokes in 
        # the next iteration, myClip2 will come to the rescue.

        if  { $myClip1  !=  "" }        {
            set  myClip2  $myClip1

        # myClip2 always has the good content. Let's force it into the clipboard. 
        # This is the part that makes sure the content isn't lost if the 
        # application that provided it exits.

        clipboard  clear  -displayof   .
        clipboard  append  -displayof  .  $myClip2
        # And here is how we poll (update) the clipboard every 600 milliseconds

        after  600  [ list {refresh clipboard} ]
 # ================================================
 # run the program
 # ================================================

 catch { destroy .clipserver errorswindow }
 set w [ toplevel .clipserver ]
 wm  withdraw  .
 wm  withdraw  $::w
 wm  title  $::w  "clipboard server"
 tk  appname  "clipboard server"
 set  myClip1  ""
 set  myClip2  ""
 {refresh clipboard}


if 0 {

Interesting. I once saw a Tcl/Tk-based network clipboard server that allowed you to cut and paste between 'Windows and Unix. I never bothered with it but it seemed really cool. CLN

LES: I guess you're talking about Clipboard teleportation.

Anyone have a thought on handling all the various selections that X provides, as well as, perhaps, some sort of history that would allow one to select a variety of things?

LES: http://people.debian.org/~kims/xclip/

218: Also you can take a look at xcb [1 ].

LES on Feb 24, 2005: I had suggested running it from the rc.local file, but of course that doesn't work, because X is not running yet by the time rc.local is run, and wish can't work correctly. So this application has to be launched after the window manager is already running. In KDE, for example, you can place a link to it in KDE's Autostart folder in your $HOME directory.

I also ran into another problem: it crashes. I don't know why. But it was clear that it would no longer be running after a while. For the time being, I fixed it with a crontab entry that calls this shell script every minute:

 if  [ `ps aux | grep clipserver | wc -l`  -le  1 ]
        then  wish /path/to/clipserver.tcl

See also: Clipboard teleportation

Other clipboard things: https://wiki.tcl-lang.org/2?clipboard and https://wiki.tcl-lang.org/2?clipboard*

LES on 2009-10-16: important change made to this page (not the code): deleted two inaccurate bug reports that lingered about in this page for a lot, lot longer than they should have.

Hum, I still wish I could rename this page to "A Little Clipboard Daemon"...

HJG Done :-)

HZe I had a little programm like this in my tool box: ClipboardMonitor. It monitors the changes of the clipboard and keeps a history (also saved in a rc file).