Function Repository Resource:

ClipboardContent

Source Notebook

Get the contents of the clipboard as an expression

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["ClipboardContent"][]

creates an expression from the contents of the clipboard.

ResourceFunction["ClipboardContent"][h]

wraps the head h around the results before returning.

Examples

Basic Examples (2) 

Get the contents of the clipboard:

In[1]:=
CopyToClipboard["Hello world"];
ResourceFunction["ClipboardContent"][]
Out[2]=

Apply the head Hold to prevent evaluation:

In[3]:=
CopyToClipboard[Defer[Print[1 + 1]]];
ResourceFunction["ClipboardContent"][Hold]
Out[4]=

Scope (2) 

When multiple cells are copied from a notebook, a list of expressions will be returned:

In[5]:=
CopyToClipboard[Notebook[{
    Cell[BoxData[RowBox[{"1", "+", "1"}]], "Input"],
    Cell[BoxData["2"], "Output"]
    }]];
ResourceFunction["ClipboardContent"][HoldForm]
Out[6]=

Cell content will be converted to strings when necessary:

In[7]:=
CopyToClipboard[Notebook[{
    Cell["Here's the title", "Title"],
    Cell[TextData[{"Here\[CloseCurlyQuote]s ", StyleBox["some", FontSlant -> "Italic"], " text."}], "Text"],
    Cell[BoxData[RowBox[{"1", "+", "1"}]], "Input"],
    Cell[BoxData["2"], "Output"]
    }]];
ResourceFunction["ClipboardContent"][HoldForm]
Out[8]=

Properties and Relations (2) 

ClipboardContent is similar to Paste:

In[9]:=
CopyToClipboard["Hello world"];
Paste[]

However, Paste prints a new cell, while ClipboardContent returns an expression:

In[10]:=
InputForm[Paste[]]
Out[10]=
In[11]:=
InputForm[ResourceFunction["ClipboardContent"][]]
Out[11]=

Neat Examples (1) 

Get an expression of the current notebook through the clipboard:

In[12]:=
SelectionMove[EvaluationNotebook[], All, Notebook, AutoScroll -> False];
FrontEndTokenExecute[EvaluationNotebook[], "Copy"];
ResourceFunction["ClipboardContent"][HoldForm]
Out[14]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 09 November 2018

Related Resources

License Information