Search code examples
clipboardjythonsikuli

Clearing Windows' Clipboard While Using Sikuli Or Jython


I'm trying to clear the Windows' clipboard using Sikuli or Jython.

I'm currently using the clipboard to grab user entered text from a textbox. I use the clipboard to check if the user entered text before clicking a button. I want to check if the textbox was left empty before the button was clicked.

The problem that I am having is that once the clipboard is used, I am unable to clear it. So the value in the clipboard is returned to my script every time the button is clicked.

I need to be able to clear the clipboard after grabbing the user entered text from it, or find a way to read the textbox without using the clipboard.

Help.

Thanks in advance,

Marwan


Solution

  • Using Jython, you can do as follows to set the contents of the clipboard to an empty string, which would be something like clearing it.

    from java.awt.datatransfer import StringSelection
    from java.awt.datatransfer import Clipboard
    from java.awt import Toolkit
    
    toolkit = Toolkit.getDefaultToolkit()
    clipboard = toolkit.getSystemClipboard()
    clipboard.setContents(StringSelection(""), None)
    

    And as follows to read the contents of the clipboard as a string:

    from java.awt.datatransfer import DataFlavor
    contents = clipboard.getContents(None)
    print contents.getTransferData(DataFlavor.stringFlavor)