6
$\begingroup$

How do I get unmodified text content from the clipboard? If you copy the following into the clipboard and use ClipboardNotebook[] \[IndentingNewLine] is interpreted as \n:

f[\[IndentingNewLine]]

and run

FullForm@Catch[
  NotebookGet@ClipboardNotebook[] /. 
   Cell[r_, ___] :> Block[{}, Throw[r, tag] /; True];
  $Failed, tag]

"f[\n]"

Apologies if there is something in alternative to ClipboardNotebook, I haven't found it.

$\endgroup$
  • $\begingroup$ So actually you need an analog of the MS Word command Paste Special ► Plain text, right? $\endgroup$ – Alexey Popkov Feb 26 '16 at 22:24
  • $\begingroup$ @AlexeyPopkov Ideally yes although this question is slightly different. $\endgroup$ – William Apr 5 '16 at 3:09
  • $\begingroup$ @Liam - what happens if you try to paste into a "Code" cell? If I create a code cell, and just hit Ctrl-v, then I get the same thing as if I manually type f[\[NewLine]] $\endgroup$ – Jason B. Nov 1 '16 at 22:06
  • $\begingroup$ @JasonB code cell works but it isn't what I am after. I would like to programatically modify the clipboard. $\endgroup$ – William Nov 1 '16 at 23:33
  • $\begingroup$ You say you need this for Linux but the question is tagged Windows. Can you correct this? $\endgroup$ – Szabolcs Nov 2 '16 at 8:58
5
$\begingroup$

The following works for Windows borrowed mostly from here.

Needs["NETLink`"];
InstallNET[];
LoadNETType["System.Windows.Forms.Clipboard"];
LoadNETType["System.Windows.Forms.TextDataFormat"];
System`Windows`Forms`Clipboard`GetText[System`Windows`Forms`\
TextDataFormat`UnicodeText]
$\endgroup$
4
$\begingroup$

For completion here is how you do it in linux.

Import["!xsel --clipboard","Text"]
$\endgroup$
  • $\begingroup$ Note that xsel is not installed by default on Ubuntu. $\endgroup$ – Ruslan Nov 2 '16 at 18:34
  • $\begingroup$ @Ruslan do you have an alternative recommendation. $\endgroup$ – William Nov 2 '16 at 19:47
  • 1
    $\begingroup$ No, it seems all X11 clipboard-related utilities are not installed by default. So you have to just warn the user that he may need to install xsel via (for Ubuntu/Debian) sudo apt-get install xsel for your answer to work. $\endgroup$ – Ruslan Nov 2 '16 at 20:20
  • $\begingroup$ @Ruslan if you post answer I'll give you the bounty I can't give it to myself. $\endgroup$ – William Nov 2 '16 at 20:27
  • $\begingroup$ It wouldn't add anything new: I'm just suggesting you to improve this answer by adding a note about xsel (which you use here!) being not by default installed — it can still be easily installed, but the user must actually do this. $\endgroup$ – Ruslan Nov 2 '16 at 20:28
1
$\begingroup$

And lastly OSX I haven't tested this on OSX yet but believe it should work.

Import["!pbpaste","Text"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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