save windows clipboard content temporarily and restore later

Discussion in 'Python' started by kakarukeys, Oct 8, 2009.

  1. kakarukeys

    kakarukeys Guest

    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?
     
    kakarukeys, Oct 8, 2009
    #1
    1. Advertising

  2. kakarukeys

    Neil Hodgson Guest

    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
     
    Neil Hodgson, Oct 9, 2009
    #2
    1. Advertising

  3. kakarukeys

    kakarukeys Guest

    On Oct 9, 11:30 am, Neil Hodgson <>
    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()
     
    kakarukeys, Oct 9, 2009
    #3
  4. kakarukeys

    Neil Hodgson Guest

    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
     
    Neil Hodgson, Oct 9, 2009
    #4
  5. kakarukeys

    kakarukeys Guest

    On Oct 10, 6:00 am, Neil Hodgson <>
    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.
     
    kakarukeys, Oct 10, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reinhard Vornholt

    Save Viewstate in DB and restore it later

    Reinhard Vornholt, Jul 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    519
    Reinhard Vornholt
    Jul 30, 2003
  2. Replies:
    2
    Views:
    775
  3. Dan Bishop
    Replies:
    0
    Views:
    243
    Dan Bishop
    Aug 26, 2008
  4. Replies:
    4
    Views:
    279
    Randy Webb
    Dec 14, 2004
  5. Mahsha

    Clipboard - Copy Image To Clipboard

    Mahsha, Jun 1, 2009, in forum: Javascript
    Replies:
    1
    Views:
    592
    Thomas 'PointedEars' Lahn
    Jun 1, 2009
Loading...

Share This Page