Copy to clipboard from Max or Java

zzzeddartha's icon

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!?

Dan Nigrin's icon

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) {}
    }
}
zzzeddartha's icon

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.

Dan Nigrin's icon
Max Patch
Copy patch and select New From Clipboard in Max.

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":

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...

zzzeddartha's icon

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.

Dan Nigrin's icon

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...

felix's icon

Hey guys, would really help me out for some basic instructions on just copying text onto my computer's clipboard (Mac, but preferable multi-platform).

I really have been trying before resorting to hassling you guys with such a basic requirement of instructions...

I'm not a Java guy and always work within the confines of native objects.. but ay,.. could you provide some real basic instructions/downloads etc to get this going ?

Dan Nigrin's icon

Explaining how to use Java in Max is probably beyond what I'm prepared to do here on the forum, but to get you started, I've attached the compiled Java class file for the file_to_clipboard object I wrote way back when. If you use that .class file together with the Max patch above in this thread, it should get you going.

file_to_clipboard.zip
zip
felix's icon

Dan, you're a BIG help,.. I appreciate your getting back to me on this!!

felix's icon

Hmmm..

Still can't get this going.. just want to copy sentences of text onto the computers general clipboard (for Command+V copying in other apps).

Can't even copy files to the clipboard right now... i tried on Max 6 + 7 and my Java is up to date.

Any advise welcomed!

Thanks Dan!

Dan Nigrin's icon

First, is the Max patch that I posted above opening with no errors?

felix's icon

Yeah,. this is what I see in the Max Window... and no activity from the outlets of the mxj during tests...

Screen-Shot-2015-04-24-at-20.26.24.png
png
Dan Nigrin's icon

I don't believe I have anything coming out of the outlets of the mxj object - but did you check to see if the text file contents were on your Mac's clipboard?

felix's icon

I'm seeing lots of crazy code on the clipboard, and nothing can be pasted (CMD+V or manually from Finder)...

However when things are copied normally outside of Max MSP the clipboard just super clearly references the file to be copied.

Its a pain, I only want to be able to store text in a patch (in a or something) and then be able to click a button to copy it to my Mac's clipboard to be pasted else where.

Dan Nigrin's icon

OK, sorry, I'm afraid that's all I can help with at the moment. Good luck!