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

Contributor ,
Feb 17, 2020 Feb 17, 2020

Copy link to clipboard

Copied

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.

TOPICS
How to , Resources , Scripting

Views

1.9K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Feb 19, 2020 Feb 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); }

Likes

Translate

Translate
Adobe Community Professional ,
Feb 17, 2020 Feb 17, 2020

Copy link to clipboard

Copied

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

 

Mylenium

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 18, 2020 Feb 18, 2020

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 18, 2020 Feb 18, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 19, 2020 Feb 19, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 19, 2020 Feb 19, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 12, 2020 Jun 12, 2020

Copy link to clipboard

Copied

thanks..

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 12, 2020 Jun 12, 2020

Copy link to clipboard

Copied

LATEST

Hi Tomas 
This is nice and works very good, thanks for that.
But for me it works only with non-linebreaked strings.

"test\rtest" outputs "testtest"
"test\ntest" doesn´t change the clipboard at all.
I want to copy the position value of a layer to the clipboard, but to paste it correctly it has to be in this format:

Adobe After Effects 8.0 Keyframe Data

Units Per Second 30
...
...

And I think because this is several lines it doesn´t work with your script.
Do you have idea how to correct that, or even better, how to  copy the position value of a layer directly to the clipboard?
Thanks so much

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines