X windows and Python?

Grant Edwards grante at visi.com
Tue Aug 15 15:42:45 EDT 2006


On 2006-08-15, Paul Rubin <http> wrote:

> I'd like to program my Python script to put a string into the
> X windows cut buffer.  Can anyone suggest the simplest way to
> do that?

There isn't a simple way to do that.  

The basic problem is that there's not really such a thing as
"_the_ X windows cut buffer". The selection mechanism under X
is rather complicated.

The way (well, _one_ way) it's supposed to work is you make an
Xlib call to claim ownership of "the selection".  Then when
somebody wants to get the contents of the selection you get an
event to which you respond by sending the current contents of
the selection.

However, if your app exits before somebody requests the
contents of the selection, then you have to use one of the
fallback mechanisms.  In addition to the "selection", there are
a set of cut buffer buffers that you can put things into. The
first of those cut buffers is where apps _usually_ look if
there is no current selection owner.

In order to work properly, you have cover both bases:

 1) You need to request ownership of the selection and then
    respond to a selection notify event if you get one.

 2) You need to write the selection contents into cut buffer 0.

I think it might be sufficient to do the following:

  0) Open a connection to the server and create a window.

  1) Write the selected string into cut buffer 0.
  
  2) Call XSetSelectionOwner to make yourself the owner of the
     selction.

  3) Call XSetSelectionOnwer to give up ownership of the
     selection (or just close the connection to the server).

At that point, the selection won't have an owner, and most
applications will look in cut buffer 0.  But if somebody tries
to get the selection contents between steps 2 and 3, then
things break unless you handle the request.

-- 
Grant Edwards                   grante             Yow!  Is this "BOOZE"?
                                  at               
                               visi.com            



More information about the Python-list mailing list