8
$\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$
5
  • $\begingroup$ So actually you need an analog of the MS Word command Paste Special ► Plain text, right? $\endgroup$ Commented Feb 26, 2016 at 22:24
  • $\begingroup$ @AlexeyPopkov Ideally yes although this question is slightly different. $\endgroup$
    – William
    Commented Apr 5, 2016 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.
    Commented Nov 1, 2016 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
    Commented Nov 1, 2016 at 23:33
  • $\begingroup$ You say you need this for Linux but the question is tagged Windows. Can you correct this? $\endgroup$
    – Szabolcs
    Commented Nov 2, 2016 at 8:58

3 Answers 3

6
$\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$
5
  • $\begingroup$ Note that xsel is not installed by default on Ubuntu. $\endgroup$
    – Ruslan
    Commented Nov 2, 2016 at 18:34
  • $\begingroup$ @Ruslan do you have an alternative recommendation. $\endgroup$
    – William
    Commented Nov 2, 2016 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
    Commented Nov 2, 2016 at 20:20
  • $\begingroup$ @Ruslan if you post answer I'll give you the bounty I can't give it to myself. $\endgroup$
    – William
    Commented Nov 2, 2016 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
    Commented Nov 2, 2016 at 20:28
2
$\begingroup$

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

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

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.