6
$\begingroup$

How to copy a string to the clipboard from a script?

I want the wolfram script to copy a string to the clipboard.

Unfortunately the following script fails to do this. The same code works perfectly in the notebook. But CopyToClipboard[] seems not to work in the Ubuntu terminal.

#! /home/conor/mathematica/Executables/wolframscript -script;

string = "word";
CopyToClipboard[x = string];
Print[x];

I think I am looking for an alternative to CopyToClipboard[] that works in scripts.

$\endgroup$
1
  • 3
    $\begingroup$ I think that the function CopyToClipboard[] requires the GUI FrontEnd, but 'wolframscript' communicates directly to the Kernel. $\endgroup$
    – Somos
    Commented Feb 13, 2019 at 18:26

4 Answers 4

6
$\begingroup$

CopyToClipboard[] requires a FrontEnd to work.

Proof:

In: Trace[CopyToClipboard["hi"]]

Out: {CopyToClipboard[hi],MathLink`CallFrontEnd(FrontEnd`CopyToClipboard(System`FEDump`makeCopyBoxes(hi))),{{System`FEDump`makeCopyBoxes(hi),hi},FrontEnd`CopyToClipboard(hi)},MathLink`CallFrontEnd(FrontEnd`CopyToClipboard(hi)),Null}

However, you can (sort of roundaboutly) run the shell inside wls and copy it using your system's clipboard tool.

#!/usr/bin/env wolframscript

string = "word";
RunProcess[{"bash", "-c", "echo '" <> string <> "'|pbcopy"}]

This copies string to the clipboard.
Additionally, Paste[] requires the FrontEnd, but there's a similar workaround. All together (name from @ConorCosnett):

#!/usr/bin/env wolframscript

string = "Programming is fun!";

copyToClipboardFromString[string_]:=RunProcess[{"bash", "-c", "echo '" <> string <> "'|pbcopy"}]
getClipboard[]:=RunProcess[{"bash", "-c", "pbpaste"}, "StandardOutput"]

copyToClipboardFromString[string]
Print[getClipboard[]]

Implemented on macOS. See here for implementations in Ubuntu (thanks to @ConorCosnett).

$\endgroup$
2
$\begingroup$

Ubuntu implementation of Max Coplan's solution

Install xclip

$ sudo apt-get install xclip

zsh

copyToClipboardFromScript[string_] := RunProcess[{"zsh", "-c", "echo " <> string <> "| xclip -selection clipboard"}];

bash

copyToClipboardFromScript[string_] := RunProcess[{"bash", "-c", "echo " <> string <> "| xclip -selection clipboard"}];
$\endgroup$
1
  • 1
    $\begingroup$ Trying to copy "$PATH", or "hey; sudo ls /" will have undesirable results. We should have used literal strings $\endgroup$
    – Max Coplan
    Commented Feb 14, 2019 at 19:47
2
$\begingroup$

Windows 10 implementation

To simply steal from the answers of @conorcosnett and @maxcoplan the following seems to work for Windows:

string = "word";
Run["echo " <> string <> " | clip"];
$\endgroup$
2
$\begingroup$

Wrapping the CopyToClipboard[] in a UsingFrontEnd[] allows us to use it in scripts:

#!/usr/bin/env wolframscript

string = "Pshhhkkkkkrrrrkakingkakingkakingtshchchchchchchchchdingdingding";
UsingFrontEnd[CopyToClipboard[string]];

If a FrontEnd is already running you may have to quit it, and then rerun this script

$\endgroup$

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.