0
$\begingroup$

I tried using Mathematica for watching my clipboard contents.

Paste[] creates a new cell each time it is evaluated though.

  • Is there a way to make Paste[] not create a new cell?
  • Is there some other function that is more like CurrentClipboardContents[]!?

enter image description here

$\endgroup$

2 Answers 2

1
$\begingroup$

Yes, I have found a function in the Function Repository.

https://resources.wolframcloud.com/FunctionRepository/resources/ClipboardContent/

$\endgroup$
0
$\begingroup$

Using JLink`


PasteContents[writer_:Identity] := (
Needs["JLink`"];
JavaBlock[
    InstallJava[];

    LoadJavaClass /@ {
        "java.awt.Toolkit",
        "java.awt.datatransfer.Clipboard",
        "java.awt.datatransfer.DataFlavor",
        "java.io.ByteArrayOutputStream",
        "javax.imageio.ImageIO"
    };

    Block[{clip},
        clip = Toolkit`getDefaultToolkit[]
                         @getSystemClipboard[]         
                         @getContents[Null];
        Which[
            clip@isDataFlavorSupported[DataFlavor`stringFlavor],
                writer@clip@getTransferData[DataFlavor`stringFlavor],
            clip@isDataFlavorSupported[DataFlavor`imageFlavor], 
                writer@Block[{bytesOs},
                    bytesOs = JavaNew["java.io.ByteArrayOutputStream"];
                    ImageIO`write[clip@getTransferData[DataFlavor`imageFlavor], "gif", bytesOs];
                    ImportByteArray@ByteArray[
                        Mod[bytesOs @ toByteArray[], 256]]
                ],
            clip@isDataFlavorSupported[DataFlavor`javaFileListFlavor],
                writer@Block[{files},
                    files = Toolkit`getDefaultToolkit[]
                                         @getSystemClipboard[]        
                                         @getContents[Null]           
                                         @getTransferData[DataFlavor`javaFileListFlavor]
                                         @toArray[];
                     File[(# @ getPath[])]& /@ files
                ],
            True, Failure["Unsupported", 
                <|
                    "MessageTemplate"->"Type of contents in clipboard(possible types: `Types`) is unsupported.",
                    "MessageParameters"-><|
                        "Types" -> (
                            #@getHumanPresentableName[]& /@ clip@getTransferDataFlavors[]
                        )
                    |>
                |>]
]]]);

When writer is:

  • Identity, resulting to raw object, such as String, Image etc.;
  • ToExpression, trying to convert string into wolfram language expression;
  • NotebookWrite[InputNotebook[], BoxData[If[StringQ@#, #, ToBoxes@#]]]&, just like Paste.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.