Skip to main content
Adirai Maji
Inspiring
February 18, 2020
Answered

How Can I Copy String in a variable to clipboard in extendScript?

  • February 18, 2020
  • 3 replies
  • 5239 views

I want to copy a string variable to Windows clipboard. How can I do that in extendScript?

 

It would be better if you can tell me to do the same for MacOS in addition.

 

Thanks In Advance.

Correct answer Tomas Sinkunas

There you go. Works for Win and OSx:

function copyToClipboard(string) {
	var cmd, isWindows;

	string = (typeof string === 'string') ? string : string.toString();
	isWindows = $.os.indexOf('Windows') !== -1;
	
	cmd = 'echo "' + string + '" | pbcopy';
	if (isWindows) {
		cmd = 'cmd.exe /c cmd.exe /c "echo ' + string + ' | clip"';
	}

	system.callSystem(cmd);
}

3 replies

Hey Evgenii
Inspiring
May 8, 2025

For those host apps that don't have system method (ppro, ai, etc) - I was able to send my text from jsx to cep and copy to clipboard via this workaround: 

const fallbackCopy = (text) => {
    const textArea = document.createElement("textarea");
    textArea.value = text;

    // Make the textarea out of viewport
    textArea.style.position = "fixed";
    textArea.style.left = "-999999px";
    textArea.style.top = "-999999px";
    document.body.appendChild(textArea);

    textArea.focus();
    textArea.select();

    try {
      const successful = document.execCommand("copy");
    } catch (err) {
      console.error("execCommand failed:", err);
    } finally {
      document.body.removeChild(textArea);
    }
  };
Tomas Sinkunas
Tomas SinkunasCorrect answer
Brainiac
February 19, 2020

There you go. Works for Win and OSx:

function copyToClipboard(string) {
	var cmd, isWindows;

	string = (typeof string === 'string') ? string : string.toString();
	isWindows = $.os.indexOf('Windows') !== -1;
	
	cmd = 'echo "' + string + '" | pbcopy';
	if (isWindows) {
		cmd = 'cmd.exe /c cmd.exe /c "echo ' + string + ' | clip"';
	}

	system.callSystem(cmd);
}
Adirai Maji
Inspiring
February 20, 2020

Thank you so much.

That works great....!! Only thing I've to modify is in the cmd variable. I need to remove the space in

' | clip"';
//into
'| clip"';

 to avoid additional space in the actual String value.

Mylenium
Brainiac
February 18, 2020

You have to call the respective menu commands of the apps.

 

Mylenium

Adirai Maji
Inspiring
February 18, 2020

No. I wasn't talking about Copy Menu Command.. I'm talking about copy to clipboard function in Extendscript itself. Like say we have a edittext and a button. whenever I press the button. The text in editttext should be copied to clipboard. How can I achieve that?

Mylenium
Brainiac
February 18, 2020

There is no such thing. A clipboard is a system-level mechanism that involves managing memory addresses and such, none of which a script can do. You have to use the host program's features and implement respective stuff. The only other option would be to create your own temporary files and manipulate it, but this will inevitably cause issues if users don't have the respective preferences enabled and security tools interfere.

 

Mylenium