Photoshop JSX -- How do I copy arbitrary text to the clipboard?
Asked Answered
L

3

5

Right now I am using the prompt command as a workaround to copy arbitrary text out of my JSX script in Photoshop.

prompt("to copy",(horizontalcenter.toString()) + ", " + verticalcenter.toString());

And that is giving me the information that I want. The "to copy" just gives the title, then the information I want to copy out of Photoshop is in the prompt box already selected. So all I have to do is hit control C, go to Notepad++ where I need this information, and hit control V.

It works, but it could be faster. There has to be a way to copy this information out of Photoshop straight to the clipboard, no?

Lapin answered 1/11, 2010 at 1:28 Comment(2)
Possible solution -- write the string to a text layer on the image. Then use the limited clipboard scripting methods in Photoshop. For my own reference later, they work on objects ArtLayer / Selection / Document. But ArtLayer and Selection are the only ones with copy. See page 54 of Adobe CS4 Scripting Guide for more info: adobe.com/content/dam/Adobe/en/devnet/photoshop/pdfs/…Lapin
Anyone have a solution that works on the Mac?Miserly
M
6

Photoshop 13.1 (the latest Creative Cloud release of Photoshop CS6) now has a hook allowing you to do this directly. Here's a sample function:

function copyTextToClipboard( txt )
{
    const keyTextData         = app.charIDToTypeID('TxtD');
    const ktextToClipboardStr = app.stringIDToTypeID( "textToClipboard" );

    var textStrDesc = new ActionDescriptor();

    textStrDesc.putString( keyTextData, txt );
    executeAction( ktextToClipboardStr, textStrDesc, DialogModes.NO );
}

Please note this won't work in versions of Photoshop prior to 13.1

Microelectronics answered 21/12, 2012 at 1:54 Comment(0)
L
5

Found the answer on a Photoshop scripting forum.

http://ps-scripts.com/bb/viewtopic.php?f=9&t=3097&p=15324&hilit=clipboard&sid=1b1cc023023b9f91ab46e30e48e2ab53#p15324

function copyTextToClipboard(text)
{
   var folderForTempFiles = Folder.temp.fsName;

   // create a new textfile and put the text into it
   var clipTxtFile =new File(folderForTempFiles + "/ClipBoard.txt"); 
   clipTxtFile.open('w'); 
   clipTxtFile.write(text); 
   clipTxtFile.close();

   // use the clip.exe to copy the contents of the textfile to the windows clipboard
   var clipBatFile =new File(folderForTempFiles + "/ClipBoard.bat"); 
   clipBatFile.open('w'); 
   clipBatFile.writeln("cat \"" + folderForTempFiles + "/ClipBoard.txt\"|clip"); 
   clipBatFile.close(); 
   clipBatFile.execute();
}

It's placing the text you want to copy in a temp text file, then copying it from that text file. I didn't even know you could place text into a text file. Apparently the javascript capabilities in Photoshop are much more powerful than I realized!

Lapin answered 3/11, 2010 at 5:51 Comment(1)
It's worth noting that this answer is windows only, though you could do something similar on OS X with pbcopy.Draghound
M
0
function addToClipBoard(text) {
    app.system("echo " + text + "| clip");
}
Motorist answered 8/4, 2024 at 16:43 Comment(3)
Warning: Doing something like that is dangerous. For example, if the text was Hi ; some bad command, your approach could execute that command which could mess up the system in various ways.Morly
ya , thank for the warnMotorist
Also I think your approach is platform dependent. To me, it looks like clip is specific to Windows.Morly

© 2022 - 2025 — McMap. All rights reserved.