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!

Scripting Photoshop scripting - how to insert a layer from another PSD


bzemer

Member
Messages
12
Likes
1
Hi,
I'm fairly new to PS scripting, and I tried my best to do this on my own or find an existing script, but with no luck.​
I want to insert a layer (or group) from another PSD file, into the current document. I want to provide the layer/group name and PSD filename in the script itself.​
I tried to do this with the script listener plugin, but it just created a script that opens the other PSD.​
Is it possible to script this?​
Can anyone help and provide a script?​
Thanks!​
 

Paul MR

The Script Master
Messages
382
Likes
206
I have arrived Steve:)

To give any help I would need to have a bit more information.
You say you want to put some layer(s)/groups(s) from another psd to the active document.
Where is this other PSD, is it opened, does it need to be selected and opened.
If opened you would need to know it's name to be able to select it.
Once the psd has been selected, how do you propose to select the layer(s)/group(s)
Do you have the names? or do you want to copy all layers/groups to the active document.
Most things can be done, it's just a matter of working out what needs to be done, before starting to code.

Hopefully we get a result in the end :)
 

bzemer

Member
Messages
12
Likes
1
Hi,
Thank you for responding! :)

All the PSD files are located at a specific directory, something similar to "d:\documents\projects\PS"

I basically, at the moment, have 3 different files that i normally use when i edit my photographs.

First, i have a PSD called watermark.psd.
I want to be able to pool the group (just called Group 1, but i can rename if needed for the code) into my active document, because i want to preserve all the layers, adjustment layers, opacity and blending settings etc.

The same thing goes with a copyright.psd, but it only contains a layer name "Boaz Zemer" (my name). This layer has a layer style applied to it, and also opacity setting.

And a third file called signature.psd, and it just has a background layer with no special settings.

Depending on the type of photograph i'm editing, i will use one more of these PSD files. I usually open them manually into a separate window and simply pull the layer or group right into the edited photo's canvas.

I appreciate your help!
Thanks...
 

Paul MR

The Script Master
Messages
382
Likes
206
Ok here is a bit of code to be going on with.
This first bit of code assumes you have the document open where you want the layerset "Group 1"
You could hard code the filename, but this examples lets you pick the file.
It opens the PSD selects layerset "Group 1" and duplicates it into your original document then closes the PSD, leaving your original document open...
Code:
main();
function main(){
if(!documents.length) return; //no documents open
var originalDoc = activeDocument;
var origName = decodeURI(activeDocument.name);
var newFile = null;
//select psd file
while(newFile == null){ newFile =File.openDialog("Please select PSD.","PSD File:*.psd");}
open(newFile);
try{
activeDocument.activeLayer  = activeDocument.layerSets.getByName("Group 1"); 
}catch(e){
    alert("Group 1 does not exist in this psd");
}
dupLayers(origName);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function dupLayers(aDoc) {
  function cTID(s) { return app.charIDToTypeID(s); };
    var desc153 = new ActionDescriptor();
        var ref61 = new ActionReference();
        ref61.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
    desc153.putReference( cTID('null'), ref61 );
        var ref62 = new ActionReference();
        ref62.putName( cTID('Dcmn'), aDoc );
    desc153.putReference( cTID('T   '), ref62 );
    desc153.putInteger( cTID('Vrsn'), 2 );
    executeAction( cTID('Dplc'), desc153, DialogModes.NO );
};

This example is generic again you can hard code your file but this code lets you select the required psd and "Place the selected document" into your open document.
(Same as File - Place).
This is handy for Watermarks etc...

Code:
main();
function main(){
if(!documents.length) return; //no documents open
var newFile = null;
//select psd file
while(newFile == null){ newFile =File.openDialog("Please select PSD.","PSD File:*.psd");}
//place psd file
placeFile(newFile);
}
function placeFile( file) {
    var desc = new ActionDescriptor();
    desc.putPath( charIDToTypeID('null'), file );
    desc.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
        var offsetDesc = new ActionDescriptor();
        offsetDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
        offsetDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
    desc.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), offsetDesc );
    executeAction( charIDToTypeID('Plc '), desc, DialogModes.NO );
};

Please let me know if you have any questions.
 

jacks_bfa

New Member
Messages
2
Likes
0
Hi Paul MR

Thanks for this script, but it is working only for one document. is it possible to work for all opened document.
 

vishok

Member
Messages
5
Likes
0
Ok here is a bit of code to be going on with.
This first bit of code assumes you have the document open where you want the layerset "Group 1"
You could hard code the filename, but this examples lets you pick the file.
It opens the PSD selects layerset "Group 1" and duplicates it into your original document then closes the PSD, leaving your original document open...
Code:
main();
function main(){
if(!documents.length) return; //no documents open
var originalDoc = activeDocument;
var origName = decodeURI(activeDocument.name);
var newFile = null;
//select psd file
while(newFile == null){ newFile =File.openDialog("Please select PSD.","PSD File:*.psd");}
open(newFile);
try{
activeDocument.activeLayer  = activeDocument.layerSets.getByName("Group 1"); 
}catch(e){
    alert("Group 1 does not exist in this psd");
}
dupLayers(origName);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function dupLayers(aDoc) {
  function cTID(s) { return app.charIDToTypeID(s); };
    var desc153 = new ActionDescriptor();
        var ref61 = new ActionReference();
        ref61.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
    desc153.putReference( cTID('null'), ref61 );
        var ref62 = new ActionReference();
        ref62.putName( cTID('Dcmn'), aDoc );
    desc153.putReference( cTID('T   '), ref62 );
    desc153.putInteger( cTID('Vrsn'), 2 );
    executeAction( cTID('Dplc'), desc153, DialogModes.NO );
};

This example is generic again you can hard code your file but this code lets you select the required psd and "Place the selected document" into your open document.
(Same as File - Place).
This is handy for Watermarks etc...

Code:
main();
function main(){
if(!documents.length) return; //no documents open
var newFile = null;
//select psd file
while(newFile == null){ newFile =File.openDialog("Please select PSD.","PSD File:*.psd");}
//place psd file
placeFile(newFile);
}
function placeFile( file) {
    var desc = new ActionDescriptor();
    desc.putPath( charIDToTypeID('null'), file );
    desc.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
        var offsetDesc = new ActionDescriptor();
        offsetDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
        offsetDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
    desc.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), offsetDesc );
    executeAction( charIDToTypeID('Plc '), desc, DialogModes.NO );
};

Please let me know if you have any questions.

how can i set file to open without dialog box? I'm on a mac and if i set var newFile = "/Users/someone/something.psd"; i get an error
 

Paul MR

The Script Master
Messages
382
Likes
206
This a string not a file
Code:
var newFile = "/Users/someone/something.psd"

It should be...
Code:
var newFile = new File ("/Users/someone/something.psd");
 

Top