I found a simple tutorial over on Java Practices that demonstrated the use of the system clipboard for transferring text between applications using copy / paste. I’m using it to copy color values from an improved version of SimpleColorPicker.pde.

The CopyPaste class described in the code can be dropped into any Processing sketch to provide access to the clipboard.

Source code – CopyPasteTool.pde

// CopyPasteTool.pde
// Marius Watz - http://workshop.evolutionzone.com
//
// Code for transferring strings via the clipboard using copy / paste.
// Useful for simple inter-app communication.

// Java classes needed for copy / paste functions.
import java.awt.datatransfer.*;

CopyPaste copypaste;

void setup() {
  copypaste=new CopyPaste();
}

void keyPressed() {
  // If 'c' is pressed the Hex string for the current color is copied
  // to the clipboard.
  if(key=='c') copypaste.sendString("Test "+(int)random(1000));
  if(key=='v') println("From the clipboard: '"+copypaste.getString()+"'");
}

//
// The following code provide for sending and receiving strings
// via the system clipboard using standard copy / paste. This class
// can be copied into any Processing sketch, ready for use.
//
// Based on code found at http://www.javapractices.com/Topic82.cjp

class CopyPaste implements ClipboardOwner {

  public void sendString(String s) {
    StringSelection stringSelection = new StringSelection( s );
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents( stringSelection, this );
  }

  public String getString() {
    String str="";

    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contents = clipboard.getContents(null);
    boolean hasTransferableText =
      (contents != null) &&
      contents.isDataFlavorSupported(DataFlavor.stringFlavor);
    if(hasTransferableText) {
      try {str=(String)contents.getTransferData(DataFlavor.stringFlavor);
      }
      catch (Exception ex){
        System.out.println("Exception: "+ex.toString());
        ex.printStackTrace();
        str=" ";
      }
    }

    return str;
  }

  // Necessary method, but does nothing
   public void lostOwnership( Clipboard clip, Transferable data) {
     //do nothing
   }
}

4 Comments »

There are 4 comments to "Code – CopyPasteTool.pde". You may leave your own comment.
1. andi, August 29th, 2006 at 23:49

hi marius,
tried the code you posted. in case people complain it doesnt work, in the keyPressed method it should say copypaste instead of transfer and void draw() {} is required so that the sketch gets started.
btw. i am at ars from friday till sunday. i see you there?

andi

2. watz, August 30th, 2006 at 04:07

Thanks, Andreas, I fixed the code. I’ll be at Ars from Friday, so see you there!

3. Andreas, September 19th, 2006 at 08:14

Hi Marius, what plugInn do you use for posting the code in wordpress?

4. watz, September 19th, 2006 at 14:12

I use a hack by Florian Jenett for using Geshi as a code colorizer, all piped through a WordPress plugin.

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">