Wolfram Research

Function Repository Resource:

ClickToCopy

Source Notebook

Make an expression that is copied to the clipboard when clicked

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["ClickToCopy"][expr]

returns a version of expr that can be copied to the clipboard by clicking on it.

ResourceFunction["ClickToCopy"][disp,copy]

displays as disp, but copies to the clipboard as copy when clicked.

ResourceFunction["ClickToCopy"][disp,copy,int]

displays as disp, copies to the clipboard as copy when clicked, and interpreted as int when evaluated.

Details and Options

ResourceFunction["ClickToCopy"] is just a formatting wrapper, it has no special evaluation rules.
To create a click-to-copy version of expr without evaluating expr, use ResourceFunction["ClickToCopy"][Defer[expr]].
ResourceFunction["ClickToCopy"][disp,copy,int] is effectively equivalent to Interpretation[ResourceFunction["ClickToCopy"][disp,copy],int].

Examples

Basic Examples

Create a click-to-copy expression:

In[1]:=
ResourceFunction["ClickToCopy"][a + b]
Out[1]=

Click the above expression then paste the result:

In[2]:=
Paste[]
Out[2]=

Mousing over the output of ClickToCopy displays a tooltip:

Clicking changes the tooltip display:


Display an expression in StandardForm, but copy as InputForm when clicked:

In[3]:=
ResourceFunction["ClickToCopy"][2^Sqrt[2], InputForm[2^Sqrt[2]]]
Out[3]=

Click the above expression then paste the result:

In[4]:=
Paste[]

Display an expression in StandardForm, copy as InputForm when clicked, but interpret numerically if the entire output is copied:

In[5]:=
ResourceFunction["ClickToCopy"][2^Sqrt[2], InputForm[2^Sqrt[2]], N[2^Sqrt[2]]]
Out[5]=

Copy the previous output, paste in a new cell and evaluate:

In[6]:=
2.665144142690225
Out[6]=

Scope

Create a list of click-to-copy expressions:

In[7]:=
ResourceFunction[
 "ClickToCopy"] /@ {"hello world", myFunction[x], Entity["Species", "Species:FelisCatus"][
   EntityProperty["Species", "Image"]]}
Out[7]=

Use Defer to prevent evaluation:

In[8]:=
ResourceFunction["ClickToCopy"][Defer[1 + 1]]
Out[8]=

Click the above expression then paste the result:

In[9]:=
Paste[]
Out[9]=

Copying a cell expression preserves that expression exactly on the clipboard:

In[10]:=
ResourceFunction["ClickToCopy"][Sqrt[2], ExpressionCell[Sqrt[2], "Output", CellFrame -> 1]]
Out[10]=

Click the above expression then paste the result:

In[11]:=
Paste[]
Out[11]=

Shorten the display of a large expression, but copy the entire thing when clicked:

In[12]:=
expr = Expand[(1 + x + y)^12];
ResourceFunction["ClickToCopy"][Short[expr], expr]
Out[11]=

Click the above expression then paste the result:

In[13]:=
Paste[]
Out[13]=

Use Unevaluated to delay evaluation until clicked:

In[14]:=
ResourceFunction["ClickToCopy"]["Click for random color", Unevaluated[Echo[RandomColor[]]]]
Out[14]=

Click the above expression then paste the result:

In[15]:=
Paste[]
Out[15]=

Click it again and then paste to get a different color:

In[16]:=
Paste[]
Out[16]=

Compare to using Defer:

In[17]:=
ResourceFunction["ClickToCopy"]["Click for unevaluated random color", Defer[Echo[RandomColor[]]]]
Out[17]=

Click the above expression then paste the result:

In[18]:=
Paste[]
Out[18]=

Properties and Relations

ClickToCopy is just a formatting wrapper:

In[19]:=
ResourceFunction["ClickToCopy"][a + b] // InputForm
Out[19]=
In[20]:=
ToBoxes[ResourceFunction["ClickToCopy"][a + b]] // Shallow
Out[20]=

Applications

Create a click-to-copy input cell:

In[21]:=
Cell[BoxData[
   ToBoxes[ResourceFunction["ClickToCopy"][
     Defer[myFunction[x_] := x + 1]]]], "Input"] // CellPrint

The cell can be clicked to copy the input as well as be evaluated normally:


Make all the input cells in a notebook click-to-copy:

In[22]:=
makeInputsClickToCopy[nb_Notebook] := nb /. Cell[BoxData[a_], "Input", b___] :> Cell[BoxData[
      ToBoxes[ResourceFunction["ClickToCopy"][RawBoxes[a], RawBoxes[a], Unevaluated[ToExpression[a]]]]], "Input", b];
In[23]:=
nb = NotebookPut[
  makeInputsClickToCopy[Import["ExampleData/document.nb"]]]
Out[23]=

The notebook can be evaluated normally:

In[24]:=
NotebookEvaluate[nb, InsertResults -> True]
Out[24]=
In[25]:=
NotebookClose[nb]

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information