Forums > MaxMSP

Copy to clipboard from Max or Java

June 8, 2011 | 9:59 pm

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:

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

Any help would be MUCH appreciated!?

June 13, 2011 | 5:07 pm

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.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});

    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];;
			String content = new String(the_chars);	

			Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
			Transferable transferableText =	new StringSelection(content);
		catch ( e) {}
		catch ( e) {}

June 25, 2011 | 12:22 am

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.

June 25, 2011 | 11:35 am

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…

June 26, 2011 | 3:32 am

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.

June 26, 2011 | 12:21 pm

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…

Viewing 6 posts - 1 through 6 (of 6 total)