What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Open JPG as a new Layer in the existing Document.


polarwoc

Guru
Messages
1,576
Likes
1,421
I have an open document on PS. I need to create a new layer and import a JPG file into this new layer. This is the script I have:

//Creating a new variable that references a newly created layer and calling it "newLayerRef"
var newLayerRef = app.activeDocument.artLayers.add();
//Creating a variable to store the path of the JPG file that I want to open.
var img = new File("C:\\Users\\Master\\Desktop\\PS Requests\\1616-OtherBanner-text banner large size.jpg");
//Assigning the JPG file to the
newLayerRef.image = img;

At this point, I am expecting the JPG file to show up in the new layer of the document open in PS. But this does not happen. What am I doing wrong? How do I open a JPG into a new layer using PS JavaScript?
Assistance would be appreciated.
 
Hi polarwoc
I am not an expert on PS javascripting (only dabbled) yet I am pretty sure there is no ".image" property or method associated with the Layer Object.
Have you considered using the ScriptListener Plugin, running through the desired steps in PS, and then just using the code that is logged as the basis for creating your script.
Hopefully a PS script expert will respond to you post.
John Wheeler
 
see if this is good for you

// script
var desc35 = new ActionDescriptor();
var desc36 = new ActionDescriptor();
desc35.putInteger( charIDToTypeID( "Idnt" ), 5 );
desc35.putPath( charIDToTypeID( "null" ), new File( "~/Desktop/IMAGE PROVA/ASER.jpg" ) ); // Enter the path of your photo
desc35.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ));
desc36.putUnitDouble( charIDToTypeID( "Hrzn" ), charIDToTypeID( "#Pxl" ), 0.000000 );
desc36.putUnitDouble( charIDToTypeID( "Vrtc" ), charIDToTypeID( "#Pxl" ), 0.000000 );
desc35.putObject( charIDToTypeID( "Ofst" ), charIDToTypeID( "Ofst" ), desc36 );
executeAction( charIDToTypeID( "Plc " ), desc35, DialogModes.NO ); // If instead of NO, type ALL, the window opens to choose a photo of your choice
 
Thanks for the response, John
I am not an expert on PS javascripting (only dabbled) yet I am pretty sure there is no ".image" property or method associated with the Layer Object.
I have only started dabbling in scripts while attempting to serve a request here. One of the previous attempts was very challenging.
It makes sense that there is no ".image" property to a Layer Object Handle. No wonder the script does not open the file.
I got that line from one of the scripts online - maybe it is unsuitable to JS on PS.

Have you considered using the ScriptListener Plugin, running through the desired steps in PS, and then just using the code that is logged as the basis for creating your script.
Thanks for the pointer. I will try this. I tried taking this direction, but could not locate the path where the action is stored. I will give it a more serious attempt this time.
 
see if this is good for you
Preston,
That script did the magic! It opened the JPG file as a Smart Object. This is exactly what I am looking for. Now the next biggest challenge is to understand the LOGIC behind this MAGIC so I can apply it better.

Any pointers on where I can learn more about this?
 
I'm glad it works
I used the ScriptListener plug-in
you can download it from this link
DOWNLOAD

If you want to know how I did it
I proceeded in this way:
I selected the file I wanted to insert

Schermata 2019-11-11 alle 16.58.24.png

DONE
 

Back
Top