# How to get the contents of clipboard in a variable?

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?

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).

• This allows to get only the last entry from the clipboard on Windows 10. Oct 24 '21 at 4:04
• Good point, thank you @AlexeyPopkov.
– kglr
Oct 24 '21 at 19:33

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.

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

• 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: mmaqa.com/qa/1731/mmaexcelmma?show=1773#a1773 Oct 24 '21 at 3: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. Oct 24 '21 at 14:25