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
Adding a layer with an image
Adding a layer with an image
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 );
};
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 );
};
Adding a layer with an image
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
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
Adding a layer with an image
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.
Adding a layer with an image
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.
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.
Adding a layer with an image
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");
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");