Skip to main content
Adirai Maji
Inspiring
Feb 18, 2020 5:47 AM UTC
Answered

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

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.

Tomas Sinkunas
Tomas SinkunasCorrect answer
Legend
Feb 19, 2020 8:10 PM UTC

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);
}

10 replies

Hey Evgenii
Inspiring
May 8, 2025 4:56 PM UTC

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);
    }
  };
Translate
Tomas Sinkunas
Tomas SinkunasCorrect answer
Legend
Feb 19, 2020 8:10 PM UTC

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);
}
Translate
Adirai Maji
Inspiring
Feb 20, 2020 2:50 AM UTC

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.

Translate
Mylenium
Legend
Feb 18, 2020 7:47 AM UTC

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

 

Mylenium

Translate
Adirai Maji
Inspiring
Feb 18, 2020 10:54 AM UTC

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?

Translate
Mylenium
Legend
Feb 18, 2020 11:24 AM UTC

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

Translate