Adding a layer with an image

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

rioan

Adding a layer with an image

Post by rioan »

Hello all,
I have a script which creates a psd with a few text layers.
Now I would like to add an image (from a specified folder) to a new layer, in this psd, and I need to add it to a specific location in this layer. The image has same ppi as the main psd but smaller size so I would imagine I should be able to position it anywhere in the layer.
Being a newbie to this Javascripting in CS4 I am having a really hard time coding this last part of the script.
I would really appreciate any help.

Thanks in advance
tino

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Paul MR

Adding a layer with an image

Post by Paul MR »

As allways there are a few ways of doing things, here is one example.
Code: Select allmain();
function main(){
if(!documents.length) return;
var offsetX = 50; //top left of placed file in pixels
var offsetY = 50; //top of placed file in pixels
//You can hard code your file here
//var file = new File("/c/foldername/foldername/filename.jpg");
var file = File.openDialog("Please select file.","File:*.*");
if(file == null) return;
placeFile(file);
rasterLayer();
var LB = activeDocument.activeLayer.bounds;
//Move the layer to the offset.
activeDocument.activeLayer.translate(new UnitValue(offsetX - LB[0].as('px'),'px'),new UnitValue(offsetY - LB[1].as('px'),'px'));
}
function placeFile(placeFile) {
    var desc21 = new ActionDescriptor();
    desc21.putPath( charIDToTypeID('null'), new File(placeFile) );
    desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
        var desc22 = new ActionDescriptor();
        desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
        desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
    desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 );
    executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO );
};
function rasterLayer() {
    var desc9 = new ActionDescriptor();
        var ref4 = new ActionReference();
        ref4.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc9.putReference( charIDToTypeID('null'), ref4 );
    executeAction( stringIDToTypeID('rasterizeLayer'), desc9, DialogModes.NO );
};
rioan

Adding a layer with an image

Post by rioan »

Thank you very much Paul for your quick reply.
Now I see that I was off by a lot, in trying to code up this last part of the script.
Trying to understand "function placeFile()" it looks totally Greek to me. Is there any documentation anywhere that would be able to shed some light on what each line does.
At least I would like to understand how a new layer for the image is created.

Thanks a lot,
tino
larsen67

Adding a layer with an image

Post by larsen67 »

The code in Paul's 2 functions is Action Manager code and is what is output from the scriptlistener plug-in… In the documentation you should have ActionDescriptor, ActionList & ActionReference that you can look up.
GDrider77

Adding a layer with an image

Post by GDrider77 »

I tried this and it works fine if i use the window/select dialog box, but i want to hardcode in the file. When i do this and comment out the openDialog line, i get an error on this line at the end of the placeFile function.

executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO );

Here is my code.

Code: Select all    function main(){
    if(!documents.length) return;
    var offsetX = 50; //top left of placed file in pixels
    var offsetY = 50; //top of placed file in pixels
    //You can hard code your file here
    var file = new File("specBG.png");
  //  var file = File.openDialog("Please select file.","File:*.*");
    if(file == null) return;
    placeFile(file);
    rasterLayer();
    var LB = activeDocument.activeLayer.bounds;
    //Move the layer to the offset.
    activeDocument.activeLayer.translate(new UnitValue(offsetX - LB[0].as('px'),'px'),new UnitValue(offsetY - LB[1].as('px'),'px'));
    }
    function placeFile(placeFile) {
        var desc21 = new ActionDescriptor();
        desc21.putPath( charIDToTypeID('null'), new File(placeFile) );
        desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
            var desc22 = new ActionDescriptor();
            desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
            desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
        desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 );
        executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO );
    };
    function rasterLayer() {
        var desc9 = new ActionDescriptor();
            var ref4 = new ActionReference();
            ref4.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
        desc9.putReference( charIDToTypeID('null'), ref4 );
        executeAction( stringIDToTypeID('rasterizeLayer'), desc9, DialogModes.NO );
    };


main();

Thanks for any help.
xbytor

Adding a layer with an image

Post by xbytor »

Your problem is this:

Code: Select allvar file = new File("specBG.png");


You need to specify a more complete path, like this:

Code: Select allvar file = new File("/c/Users/xbytor/specBG.png");