Copy to clipboard from Max or Java

Jun 8, 2011 at 9:59pm

Copy to clipboard from Max or Java

Does anyone know any way to copy a file to the Mac OSX clipboard from Max or with Java?

I would like to copy a .wav file to clipboard and paste it into a different program.
I tried using the Java code here:

http://www.vogella.de/blog/2009/09/04/swt-clipboard/

But I get an error saying the item was not found or deleted.

Any help would be MUCH appreciated!?

#57513
Jun 13, 2011 at 5:07pm

Several years ago I wrote some Java code that I used within an mxj object to do this. I wasn’t writing the contents of a .wav file, but rather an .xml file – but perhaps this will get you going. To use it, prepend copyToClipboard in front of the full path to your file, and send that to the mxj’s inlet… Let us know if it works!

import com.cycling74.max.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.awt.Toolkit;

public class file_to_clipboard extends MaxObject
{

	private static final String[] INLET_ASSIST = new String[]{
		"full path of file to put on clipboard"
	};
	private static final String[] OUTLET_ASSIST = new String[]{
		"outlet 1 help"
	};

	public file_to_clipboard(Atom[] args)
	{
		declareInlets(new int[]{DataTypes.ALL});
		declareOutlets(new int[]{DataTypes.ALL});
		setInletAssist(INLET_ASSIST);
		setOutletAssist(OUTLET_ASSIST);
	}

    public void copyToClipboard(String file_string) {
		try {
			FileInputStream fis = new FileInputStream (MaxSystem.maxPathToNativePath(file_string));
			int the_length = fis.available();
			byte the_chars[]= new byte[the_length];
			fis.read(the_chars);
			String content = new String(the_chars);	

			Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
			Transferable transferableText =	new StringSelection(content);
			clipboard.setContents(transferableText,null);
		}
		catch (java.io.FileNotFoundException e) {}
		catch (java.io.IOException e) {}
    }
}
#206044
Jun 25, 2011 at 12:22am

Thank you so much for responding!

However, it doesn’t work. I’m not sure what to change, but there is no error message and nothing copied to the clipboard.

#206045
Jun 25, 2011 at 11:35am

Sorry – here’s a pitch that illustrates how you use it. You have to send the mxj a message with the full path of the file name who’s contents you want copied to the clipboard, prepended by “copyToClipboard”:

– Pasted Max Patch, click to expand. –

I’m assuming that you’ve already compiled the java code above to a class file in your classpath too…

FYI, I’m not sure that this code will work with a non-text file…. So first try it with a plain text file, and see if it works – if so, then you can try it with an audio file…

#206046
Jun 26, 2011 at 3:32am

Okay, so that was what I did before, but now I see that this does indeed work, but not in the way I would like it to.

This works in such a way that when I go to Finder–>Edit–>Show Clipboard, the text file does indeed show up, and for a music file I get some crazy symbols…

However, what I would like it to do is copy a file such that a Cmd+V (Paste) keystroke will paste the file somewhere else.

One reason I would like to do this sound files into Ableton Live (and thereafter use it’s transposition, warping, and other great algorithms) from Max.

#206047
Jun 26, 2011 at 12:21pm

Actually, the code does work to all Cmd+V to paste the contents into whatever app you are working on – but as you found out, it won’t work for binary file contents. You’ll have to modify the code to do that I’m afraid…

#206048

You must be logged in to reply to this topic.