Message Boards Message Boards

[?] How to access clipboard in plain text format?

Posted 7 years ago

The only way I know of to access the clipboard in Mathematica is by reading ClipboardNotebook[]:

NotebookGet@ClipboardNotebook[]

(I do not know if ClipboardNotebook can take any arguments.)

Paste uses this as well.

There are situations when ClipboardNotebook doesn't exactly give me what I want. Both Windows and OS X can hold clipboard data in multiple formats simultaneously. For example, when we copy text, it may be placed on the clipboard both as rich text with formatting, and plain text. The rich text version may be present in multiple formats, e.g. RTF, HTML, etc. On Windows, some Office apps even have a paste option that can choose which type of data to use.

How can I programmatically obtain the plain text data in Mathematica in the following situation?

On OS X, I make a small table in Excel and copy it. If I paste it into a plain text editor, I get

1    2
3    4

where the entries are separated with tabs and newlines. However, if I paste it into Mathematica, I get "1234", without any separators. I believe this is because Mathematica does not take the plain text version from the clipboard.

This is also what I get from ClipboardNotebook[].

However, if I type " in Mathematica, and paste only then, then I do get "1\t2\n3\t4" (after Mathematica after asks me whether to escape special characters). This proves that Mathematica can indeed access the plain text clipboard entry.

Now the big question: can I also get this plain text clipboard data programmatically?

Some more evidence that the problem is that Excel places the data onto the clipboard in multiple formats:

  • I can see these separate formats using the Clipboard Viewer utility that comes with Xcode
  • If I paste into a plain text editor, then re-copy from there, then Mathematica does preserve the tab and newline characters during pasting.
POSTED BY: Szabolcs Horvát
4 Replies

Thanks Sander! (When I posted my response above, it was pbpaste only.)

I also received a J/Link response from @WReach on StackExchange:

Needs["JLink`"];
InstallJava[];
LoadJavaClass["java.awt.Toolkit", AllowShortContext -> False];
LoadJavaClass["java.awt.datatransfer.DataFlavor", AllowShortContext -> False];

getClipboardString[]:=
  JavaBlock @ Module[{clipboard, flavor}
  , clipboard = java`awt`Toolkit`getDefaultToolkit[] @ getSystemClipboard[]
  ; flavor = java`awt`datatransfer`DataFlavor`stringFlavor
  ; If[clipboard@isDataFlavorAvailable[flavor], clipboard @ getData[flavor], $Failed]
  ]

While both solutions are cross-platform, I trust the robustness of J/Link better, so I will go with that one. Then I don't have to think about whether pbpaste is present on some arbitrary Linux distribution and whether it is still in the path.

POSTED BY: Szabolcs Horvát

yes, I made a bit nicer later, I was not sure about Windows first... I've never used JLink myself, but it looks good. Compatibility-wise java solution might be better. I don't know which versions of Windows are 'paste' compatible, and which version of mac/linux/unix support pbpaste... But I also don't know how java handles that across all OSs and versions of it... Might be a good idea to add some Checks around it if you're planning to give the code to many people...

POSTED BY: Sander Huisman

To clarify, I am looking for a platform-agnostic solution. The ideal solution would use pure Mathematica, but a J/Link-based one might be workable as well (for as long as it works on all OSs without needing extra jar files).

POSTED BY: Szabolcs Horvát

How about:

Switch[$OperatingSystem,
 "MacOSX" | "Unix", RunProcess["pbpaste", "StandardOutput"],
 "Windows", RunProcess["paste", "StandardOutput"],
 _, $Failed
 ]

I don't have excel/windows to test...

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract