X windows and Python?

P

Paul Rubin

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?
Maybe I can do it by putting up a Tkinter text widget, sticking the
string into it, and selecting it (I'm checking the docs) but uggh.
I'd prefer not to have to put anything on the screen. I just want to
put the string into the cut buffer so I can paste it into another
program.

Thanks for any suggestions.
 
P

Paul Rubin

Paul Rubin said:
I'd like to program my Python script to put a string into the X
windows cut buffer.

Hmm, looks like I can use w.clipboard_append with an arbitrary tkinter
widget, maybe without having to actually display anything. I'll try
that.
 
D

David Boddie

Paul Rubin 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? > Maybe I can do it by putting up a Tkinter
text widget, sticking the > string into it, and selecting it (I'm
checking the docs) but uggh. > I'd prefer not to have to put anything
on the screen. I just want to > put the string into the cut buffer so
I can paste it into another > program. Maybe this does what you need:
http://python-xlib.sourceforge.net/ David
 
P

Paul Rubin

Paul Rubin said:
Hmm, looks like I can use w.clipboard_append with an arbitrary tkinter
widget, maybe without having to actually display anything. I'll try
that.

Nope, that's some kind of internal Tk clipboard. Any other ideas, how
to communicate with the X window manager?
 
S

skip

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

Maybe there's some useful functionality exposed through the Python Xlib
module:

http://python-xlib.sourceforge.net/

Skip
 
G

Grant Edwards

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.
 
S

Sybren Stuvel

Paul Rubin enlightened us with:
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?

PWSafe is a console password storage utility that can put the password
into the clipboard buffer. I don't know how they do it, but you could
look through the pwsafe source and find out.

Sybren
 
G

Grant Edwards

There isn't a simple way to do that.

I forgot to mention, there are actually three different
selections (plus the cut buffers).

Here's a nice explanation of just the selection part of the
mess:

http://www.msu.edu/~huntharo/xwin/docs/xwindows/selection.pdf

If you want something quick and dirty, you can just use
os.popen() to call xsel:

http://www.niksula.hut.fi/~vherva/xsel/

xsel doesn't do the "cut buffer" thing. It obtains ownership of
the selection, and then forks off a copy of itself to service
requests for the selection contents. I would probably have
just copied the selection to cut buffer 0 and exited, but this
way works fine.
 
P

Paul Rubin

David Boddie said:
The usual follow-up to "fix" Google's "formatting". Maybe this does
what you need:

http://python-xlib.sourceforge.net/

Thanks, wow, a complete xlib implementation in Python, so I'd get to
do low-level X programming and implement the stuff Grant Edwards
described. I hope I can avoid that. What I'm doing right now is
writing the stuff out to a file and loading it into OpenOffice, then
selecting the contents there and copying it the cut buffer. Blecccch.
 
P

Paul Rubin

Grant Edwards said:
The way (well, _one_ way) it's supposed to work is you make an
Xlib call to claim ownership of "the selection". ...

Thanks, this explanation was very interesting. It would be great if
tkinter had some calls to do this stuff, which I guess means the
underlying Tk needs them (I dunno if it has them). Anyway, for what I
was doing, it wasn't that urgent, and I'm using an awful workaround.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top