7

If I copy something into the clipboard then use var=Paste[]; I get Null in the var and the clipboard content is printed on the next cell. How do I get the content of the clipboard directly into the variable.

I have a basic text in my clipboard and want it stored as a string.

Moreover Windows 10's clipboard now supports multiple entries. Is there a way to select entries other than the last one?

user64494
  • 26,149
  • 4
  • 27
  • 56
user13892
  • 9,375
  • 1
  • 13
  • 41

3 Answers3

8

To get the last entry in clipboard you can use:

var = ToExpression[NotebookGet[ClipboardNotebook[]][[1, 1, 1, 1]]]

This works in version 9 / windows 10 (64bit).

kglr
  • 394,356
  • 18
  • 477
  • 896
0

This is perhaps a partial answer; it shows that using ctrl+v rather than Paste[] allows an expression to be assigned to a variable as required.

As an example, enter

Red[]

Shows red box.

Select the box then use Paste from the context menu to copy it to the clip-board; then enter

x = Paste[]
x // FullForm

This shows x = Null.

Next, enter

y=

then type ctrl+v

This gives the red box as expected; enter

y // FullForm

This gives RGBColor[1,0,0], which shows that the expressions has been passed to y as required.

-1

Actually, there is an undocumented function for this purpose:PastContent[]

yode
  • 26,686
  • 4
  • 62
  • 167
  • There is no built-in function PastContent, at least in version 12.3.1: Names["*`*Past*Content*"] returns empty list. Probably you refer to this: https://mmaqa.com/qa/1731/mmaexcelmma?show=1773#a1773 – Alexey Popkov Oct 24 '21 at 03:59
  • @AlexeyPopkov Aha..Life is hard, and I don't remember what I ever did at all. That link you quoted is exactly one of the questions I answered myself a few years ago. BTW, you're Chinese? How did you find out about that community? – yode Oct 24 '21 at 11:37
  • No, I've found this link here. – Alexey Popkov Oct 24 '21 at 14:25