Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > save windows clipboard content temporarily and restore later

Reply
Thread Tools

save windows clipboard content temporarily and restore later

 
 
kakarukeys
Guest
Posts: n/a
 
      10-08-2009
Is there a way to:

1. save windows clipboard content temporarily in a variable
2. (the clipboard content is then overwritten by some other
applications)
3. restore the saved data back into the clipboard.

?

I've tried win32clipboard's GetClipboardData, SetClipboardData.
The GetClipboardData method is able to retrieve clipboard content only
after a format is specified.
Restoring the data with that format could result in information loss,
for example when HTML text is saved in ordinary text format. There is
no format that could preserve 100% of any kind of clipboard content.

Does anyone has a brilliant solution?
 
Reply With Quote
 
 
 
 
Neil Hodgson
Guest
Posts: n/a
 
      10-09-2009
kakarukeys:

> Restoring the data with that format could result in information loss,
> for example when HTML text is saved in ordinary text format. There is
> no format that could preserve 100% of any kind of clipboard content.
>
> Does anyone has a brilliant solution?


Enumerate all the clipboard formats with EnumClipboardFormats and
grab the contents in each format then put them all back when finished.

Neil
 
Reply With Quote
 
 
 
 
kakarukeys
Guest
Posts: n/a
 
      10-09-2009
On Oct 9, 11:30*am, Neil Hodgson <nyamatongwe+thun...@gmail.com>
wrote:
> kakarukeys:
>
> > Restoring the data with that format could result in information loss,
> > for example when HTML text is saved in ordinary text format. There is
> > no format that could preserve 100% of any kind of clipboard content.

>
> > Does anyone has a brilliant solution?

>
> * *Enumerate all the clipboard formats with EnumClipboardFormats and
> grab the contents in each format then put them all back when finished.
>
> * *Neil


Hi Neil,

I followed your hints, and wrote the following code. It works for most
clipboard formats except files. Selecting and copying a file, followed
by backup() and restore() throw an exception:

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from test12 import *
>>> backup()
>>> restore()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test12.py", line 40, in restore
win32clipboard.SetClipboardData(format, cb[format])
TypeError: expected a readable buffer object
>>>


If I try to skip the error, pasting into a folder creates a file named
'scrap' with more or less the same content as the copied file. Is
there any solution?

import win32clipboard

storage = []

def backup():
cb = {}
win32clipboard.OpenClipboard()
format = 0
try:
while True:
format = win32clipboard.EnumClipboardFormats(format)
if format == 0:
break
else:
try:
RawData = win32clipboard.GetClipboardData(format)
except:
continue
else:
cb[format] = RawData
finally:
win32clipboard.CloseClipboard()
storage.append(cb)

def restore():
if storage != []:
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
cb = storage.pop()
for format in cb:
win32clipboard.SetClipboardData(format, cb[format])
finally:
win32clipboard.CloseClipboard()

 
Reply With Quote
 
Neil Hodgson
Guest
Posts: n/a
 
      10-09-2009
kakarukeys:

> I followed your hints, and wrote the following code. It works for most
> clipboard formats except files. Selecting and copying a file, followed
> by backup() and restore() throw an exception:


For some formats the handle stored on the clipboard may not be a
memory handle so may not be retrieved as memory. You could try using a
list of formats to include or exclude or just pass over the exception.

Neil
 
Reply With Quote
 
kakarukeys
Guest
Posts: n/a
 
      10-10-2009
On Oct 10, 6:00*am, Neil Hodgson <nyamatongwe+thun...@gmail.com>
wrote:
> kakarukeys:
>
> > I followed your hints, and wrote the following code. It works for most
> > clipboard formats except files. Selecting and copying a file, followed
> > by backup() and restore() throw an exception:

>
> * *For some formats the handle stored on the clipboard may not be a
> memory handle so may not be retrieved as memory. You could try using a
> list of formats to include or exclude or just pass over the exception.
>
> * *Neil


The exception occurred not when the program was trying to retrieve the
clipboard data, but when calling SetClipboardData to write to
clipboard. So I don't think what you said is the cause of the problem.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
$clipboard = Gtk::Clipboard.get('PRIMARY')... under windows Dan Bishop Ruby 0 08-26-2008 01:47 PM
clipboard.setContents destroys clipboard.getContents? spunibard@gmail.com Java 2 03-02-2007 07:52 PM
Paste from clipboard when clipboard changes mid script melvynm@gmail.com Javascript 4 12-14-2004 10:59 PM
Using VB.Net or C#, utilizing the clipboard object, how to copy an MS Excel graphic from the clipboard to an image control and obtain its source reference TC ASP .Net 5 09-01-2004 04:49 AM
Save Viewstate in DB and restore it later Reinhard Vornholt ASP .Net 0 07-30-2003 03:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57