Accessing the clipboard (Java)

Last edited on

Overview

This example demonstrates accessing the clipboard and should work for applictions based on AWT and Swing.

Python

def main():
    startApplication("Calculator.jar")
    test.log("Clipboard contains text: %s" % get_clipboard())
 
def get_clipboard():
    dataFlavor_class = java_lang_Class.forName("java.awt.datatransfer.DataFlavor")
    toolkit = java_awt_Toolkit.getDefaultToolkit()
    clipboard = toolkit.getSystemClipboard()
    contents = clipboard.getContents(object.createNull(java_lang_Object))
    obj = contents.getTransferData(java_awt_datatransfer_DataFlavor.stringFlavor)

    # Get a scripting language text object from the AUT object:
    s = str(obj)

    return s
test.py

Ruby

# encoding: UTF-8
require 'squish'

include Squish

def main
  startApplication("Calculator.jar")
  Test.log("Clipboard contains text: %s" % get_clipboard())
end

def get_clipboard()
  dataFlavor_class = LC::Java_lang_Class.forName("java.awt.datatransfer.DataFlavor")
  toolkit = LC::Java_awt_Toolkit.getDefaultToolkit()
  clipboard = toolkit.getSystemClipboard()
  contents = clipboard.getContents(Squish::Object.createNull(LC::Java_lang_Object))
  o = contents.getTransferData(LC::Java_awt_datatransfer_DataFlavor::stringFlavor)

  # Get a scripting language text object from the AUT object:
  s = "" + o

  return s
end
test.rb