Skip to main content
Inspiring
November 5, 2014
Answered

Is it possible to paste the copy content to the text item via script?

  • November 5, 2014
  • 4 replies
  • 1431 views

Hi All,

I am looking into the solution of pasting the copied content (by cntrl + c) to the text item.

Following is my scenario,

- Have the text (copied from cntrl + c).

- Now I want to create a text item on the current doc.

- Paste the content to that text item. ( Looking script for this step).

How can we accomplish this?

Thanks for any help.

This topic has been closed for replies.
Correct answer _VG

There is no problem to get/set the content of text layer. But here I want to get the content (copied from CNTRL+C command from notepad) present in the memory and set it to the text item.

I guess it's a little confussing. Let me put this in another way,

- Create a shape layer and select it

- Execute the following script,

var idcopyLayerCSS = stringIDToTypeID( "copyLayerCSS" );

    var desc111 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref31 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref31.putEnumerated( idLyr, idOrdn, idTrgt );

    desc111.putReference( idnull, ref31 );

executeAction( idcopyLayerCSS, desc111, DialogModes.NO );

After executing the script, the css data of the selected layer is stored in the memory.

Now my problem is to get the content of this css data.

How I am going to do this?

4 replies

Inspiring
December 20, 2023

Having issues with formatting but here's a basic way if there's only one line of text. Edit directories accordingly. This is for windows OS: 

Make a Bat file "RunPowershell.bat":

@4628292 off
powershell -command "& {Get-Clipboard}" > "I:\output.txt"

Jsx file: 

// Function to execute a system command and capture its output
function executeCommand(command) {
  var fileObj = new File(command);
  fileObj.execute();
}

// Get clipboard data using the Batch script
var clipboardData = "";
try {
  executeCommand('I:\\RunPowershell.bat');
  
  // Read the output file
  var outputFile = new File('I:\\output.txt');
  outputFile.open('r');
  clipboardData = outputFile.read();
  outputFile.close();
  outputFile.remove();
} catch (e) {
  alert("Error executing Batch file: " + e);
}

// Create a new text layer in Photoshop
var textLayer = app.activeDocument.artLayers.add();
textLayer.kind = LayerKind.TEXT;
textLayer.textItem.contents = clipboardData;



matias.kiviniemi
Legend
November 7, 2014

Ok, now I get it Accessing clipboard directly is probably hard, at least I don't know a way.

Exceptions "not currently available" often mean "option in menu is disabled". A good reference is trying to do same through PS menus. In the case of having raw text (from apps like notepad) put text as ascii to clipboard and PS seems not to support it. If copy from Word you can actually paste because Word puts text and image and PS will paste the graphics as layer (i.e. you'd not get the text data).

I'd try creating a Javascrip dialog window with a text area and pasting there. You could even detect when it gets text and automatically close eliminating an OK-button.

matias.kiviniemi
Legend
November 6, 2014

If the layer is a text layer, you can use layer.textItem.contents to get & set the text. Before setting you might have to sanitize line changes like my_text = my_text.replace(/\r\n/g, "\\r").replace(/\n/g, "\\r")

_VGAuthorCorrect answer
Inspiring
November 6, 2014

There is no problem to get/set the content of text layer. But here I want to get the content (copied from CNTRL+C command from notepad) present in the memory and set it to the text item.

I guess it's a little confussing. Let me put this in another way,

- Create a shape layer and select it

- Execute the following script,

var idcopyLayerCSS = stringIDToTypeID( "copyLayerCSS" );

    var desc111 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref31 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref31.putEnumerated( idLyr, idOrdn, idTrgt );

    desc111.putReference( idnull, ref31 );

executeAction( idcopyLayerCSS, desc111, DialogModes.NO );

After executing the script, the css data of the selected layer is stored in the memory.

Now my problem is to get the content of this css data.

How I am going to do this?

Inspiring
November 6, 2014

Don't know about Photoshop, but InDesign has app.paste(). This creates a text item in the active doc and pastes the content from clipboard.

I guess there should be a similar method in the PS APIs.

_VGAuthor
Inspiring
November 6, 2014

Yes, there is an API in Ps,

app.activeDocument.paste()

But it's not working here. I am getting exception every time "The command "Paste" is not currently available". I don't know why it behave like this.