Highlighted

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

Participant ,
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.

Adobe Community Professional
Correct answer by Tomas_Sinkunas | Adobe Community Professional

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);
}
TOPICS
How to, Resources, Scripting

Views

544

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

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

Participant ,
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.

Adobe Community Professional
Correct answer by Tomas_Sinkunas | Adobe Community Professional

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);
}
TOPICS
How to, Resources, Scripting

Views

545

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
Feb 17, 2020 0
Most Valuable Participant ,
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
Reply
Loading...
Feb 17, 2020 0
Participant ,
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
Reply
Loading...
Feb 18, 2020 0
Most Valuable Participant ,
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
Reply
Loading...
Feb 18, 2020 0
Adobe Community Professional ,
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
Reply
Loading...
Feb 19, 2020 2
Participant ,
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
Reply
Loading...
Feb 19, 2020 0
Community Beginner ,
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
Reply
Loading...
Jun 12, 2020 0
Community Beginner ,
Jun 12, 2020

Copy link to clipboard

Copied

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
Reply
Loading...
Jun 12, 2020 0