We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Simplest Copy to Clipboard
Page Index Toggle Pages: 1
Simplest Copy to Clipboard? (Read 1887 times)
Simplest Copy to Clipboard?
May 24th, 2010, 9:30am
 
Hi all:

Does anyone know the simplest way to copy text to clipboard from processing?

Backstory:

I've made a little color picker app, and right now it does a "println" to give me the hex value for the color chosen. The downside is that I then have to select the value, then copy and paste it. This also means that I can't export a standalone app, since I don't have access to the 'print' box outside of Processing.

Ideally I could just have the program automatically 'copy' the hex value when a color is chosen. Then I could export the app and run it as standalone, and easily choose a color and paste it into my code.

Adam
Re: Simplest Copy to Clipboard?
Reply #1 - May 24th, 2010, 9:54am
 
Search processing.org text field <- clipboard
Result list -> Windows Clipboard (and a hack too)
Done.
Re: Simplest Copy to Clipboard?
Reply #2 - May 24th, 2010, 10:08am
 
Thanks! It looks like this is Windows-only. Is there a Mac version that you know of?
Re: Simplest Copy to Clipboard?
Reply #3 - May 24th, 2010, 11:16am
 
Have you tried it on a Mac?  The code posted looks pretty system-agnostic, and cross-platform compatibility is supposed to be one of the primary features of Java, so offhand I don't see any reason why it wouldn't work on any platform (that has a clipboard).
Re: Simplest Copy to Clipboard?
Reply #4 - May 24th, 2010, 11:21am
 
Yes, on my Mac this line is throwing back an error:

Code:
public void run() {
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
}


It says:

Quote:
Cannot find anything named "Toolkit"


Sorry if this is a dummy question!
Re: Simplest Copy to Clipboard?
Reply #5 - May 24th, 2010, 11:24am
 
EDIT:  No, not a dummy question.  See below.

Followup:  I got it to compile and run under Linux, but I can't figure out how to make it do anything once it's running.

To get it to compile, I moved all the code above the "// Usage" line into a separate tab in the PDE, and added a second import line under the first:
Code:
import java.awt.Toolkit; 


But at the moment I can't get it to do anything other than give me a blank window (although looking at the code, I'm not sure what it's even supposed to do; it may just be a non-functioning stub to show the usage.)
Re: Simplest Copy to Clipboard?
Reply #6 - May 24th, 2010, 11:39am
 
Quote:
import java.awt.Toolkit;


Perfect!

After mucking about with it a bit I've gotten it to work. For a demo app it had a lot of unneeded extra functionality. I've simplified it to make it a little easier to understand.

Code:

// //////////////////
// Clipboard class for Processing
// by seltar, modified by adamohern
// v 0115AO
// only works with programs. applets require signing

import java.awt.datatransfer.*;
import java.awt.Toolkit;

ClipHelper cp = new ClipHelper();

void setup() { size(200,200); }
void draw() {}

// Press Any Key to Copy
void keyPressed() { cp.copyString(""+(int)random(255)); }


// CLIPHELPER OBJECT CLASS:

class ClipHelper {
Clipboard clipboard;

ClipHelper() {
getClipboard();
}

void getClipboard () {
// this is our simple thread that grabs the clipboard
Thread clipThread = new Thread() {
public void run() {
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
}
};

// start the thread as a daemon thread and wait for it to die
if (clipboard == null) {
try {
clipThread.setDaemon(true);
clipThread.start();
clipThread.join();
}
catch (Exception e) {}
}
}

void copyString (String data) {
copyTransferableObject(new StringSelection(data));
}

void copyTransferableObject (Transferable contents) {
getClipboard();
clipboard.setContents(contents, null);
}

String pasteString () {
String data = null;
try {
data = (String)pasteObject(DataFlavor.stringFlavor);
}
catch (Exception e) {
System.err.println("Error getting String from clipboard: " + e);
}
return data;
}

Object pasteObject (DataFlavor flavor)
throws UnsupportedFlavorException, IOException
{
Object obj = null;
getClipboard();

Transferable content = clipboard.getContents(null);
if (content != null)
obj = content.getTransferData(flavor);

return obj;
}
}

Page Index Toggle Pages: 1