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.