hello that such
You see, I would like to make a script and would like to ask you for help to get it.
I would like to copy the properties from one layer to another, (width, height, position x, and position y).
Can you help me please?
How could I select the layer (to be copied properties), after running the script?
would greatly appreciate your help.
regards
copy the properties from one layer to another
-
pfaffenbichler
copy the properties from one layer to another
Conferring width and height may necessitate a resampling transformation – is that really your intention?
How could I select the layer (to be copied properties), after running the script?
There are several options.
• a dialog that lists all layers to select the target one
• storing the bounds interim and split the operation into two Scripts
• have the Script check for stored bounds, if none are found store them, if some are found apply them to the active Layer and erase the stored bounds
• have the Script operate on two selected Layers, taking the bounds of the one and apply them to the other
How could I select the layer (to be copied properties), after running the script?
There are several options.
• a dialog that lists all layers to select the target one
• storing the bounds interim and split the operation into two Scripts
• have the Script check for stored bounds, if none are found store them, if some are found apply them to the active Layer and erase the stored bounds
• have the Script operate on two selected Layers, taking the bounds of the one and apply them to the other
-
buga
copy the properties from one layer to another
Thank you very much for your answer.
I like the first three options you mention. I'm learning to write scripts and I would like to do so in three ways.
Could you please help me?
Do you know any post that explains something of the options you mention?
thanks
I like the first three options you mention. I'm learning to write scripts and I would like to do so in three ways.
Could you please help me?
Do you know any post that explains something of the options you mention?
thanks
-
pfaffenbichler
copy the properties from one layer to another
This should provide a dialog that lists all the layers in a file.
Code: Select all// 2012, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
////////////////////////////////////
// get document-path and -title;
var myDocument = app.activeDocument;
try {var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]
var myPath = myDocument.path}
catch (e) {var myDocName = myDocument.name
var myPath = "~/Desktop"};
var theLayers = collectLayers(myDocument, []);
////////////////////////////////////
// create the dialog;
var dlg = new Window('dialog', "select layer", [500,300,930,800]);
//create list for layer-selection;
dlg.layerRange = dlg.add('panel', [21,20,279,445], "select layer");
dlg.layerRange.layersList = dlg.layerRange.add('listbox', [11,20,240,405], '', {multiselect: false});
for (var q = 0; q < theLayers.length; q++) {
dlg.layerRange.layersList.add ("item", theLayers[q].name);
};
dlg.layerRange.layersList.items[0].selected = true;
// ok- and cancel-buttons;
dlg.buildBtn = dlg.add('button', [220,460,410,485], 'OK', {name:'ok'});
dlg.cancelBtn = dlg.add('button', [21,460,210,485], 'Cancel', {name:'cancel'});
dlg.center();
////////////////////////////////////
var myReturn = dlg.show ();
// in case of OK;
if (myReturn == 1) {
// get the number instead of the name;
for (var p = 0; p < dlg.layerRange.layersList.items.length; p++) {
if (dlg.layerRange.layersList.items[p].selected == true) {
var theTarget = theLayers[p];
}
};
alert (theTarget);
}
};
else {alert ("no document open")};
////////////////////////////////////
////// function collect all layers //////
function collectLayers (theParent, allLayers) {
if (!allLayers) {var allLayers = new Array}
else {};
for (var m = theParent.layers.length - 1; m >= 0;m--) {
var theLayer = theParent.layers[m];
// apply the function to layersets;
if (theLayer.typename == "ArtLayer") {
allLayers.push(theLayer)
}
else {
// this line includes the layer groups;
allLayers.push(theLayer);
collectLayers(theLayer, allLayers)
}
}
return allLayers
};
Edit: And as usual using Action Manager code instead of the Document Object Model should make the Script run faster, but make it less easily readable.
As for method 2 (»storing the bounds interim and split the operation into two Scripts«) and 3 (»have the Script check for stored bounds, if none are found …«) the information can be stored in a txt-file in some specific place or in a custom NameSpace in the metadata.
For the latter see
http://forums.adobe.com/message/4066555#4066555
where Paul Riggott uses that to store guide-information.
All in all I would think that method 3 would make the most sense; basically the Script could on every other run store the selected Layer’s bounds and then apply and remove them.
Code: Select all// 2012, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
////////////////////////////////////
// get document-path and -title;
var myDocument = app.activeDocument;
try {var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]
var myPath = myDocument.path}
catch (e) {var myDocName = myDocument.name
var myPath = "~/Desktop"};
var theLayers = collectLayers(myDocument, []);
////////////////////////////////////
// create the dialog;
var dlg = new Window('dialog', "select layer", [500,300,930,800]);
//create list for layer-selection;
dlg.layerRange = dlg.add('panel', [21,20,279,445], "select layer");
dlg.layerRange.layersList = dlg.layerRange.add('listbox', [11,20,240,405], '', {multiselect: false});
for (var q = 0; q < theLayers.length; q++) {
dlg.layerRange.layersList.add ("item", theLayers[q].name);
};
dlg.layerRange.layersList.items[0].selected = true;
// ok- and cancel-buttons;
dlg.buildBtn = dlg.add('button', [220,460,410,485], 'OK', {name:'ok'});
dlg.cancelBtn = dlg.add('button', [21,460,210,485], 'Cancel', {name:'cancel'});
dlg.center();
////////////////////////////////////
var myReturn = dlg.show ();
// in case of OK;
if (myReturn == 1) {
// get the number instead of the name;
for (var p = 0; p < dlg.layerRange.layersList.items.length; p++) {
if (dlg.layerRange.layersList.items[p].selected == true) {
var theTarget = theLayers[p];
}
};
alert (theTarget);
}
};
else {alert ("no document open")};
////////////////////////////////////
////// function collect all layers //////
function collectLayers (theParent, allLayers) {
if (!allLayers) {var allLayers = new Array}
else {};
for (var m = theParent.layers.length - 1; m >= 0;m--) {
var theLayer = theParent.layers[m];
// apply the function to layersets;
if (theLayer.typename == "ArtLayer") {
allLayers.push(theLayer)
}
else {
// this line includes the layer groups;
allLayers.push(theLayer);
collectLayers(theLayer, allLayers)
}
}
return allLayers
};
Edit: And as usual using Action Manager code instead of the Document Object Model should make the Script run faster, but make it less easily readable.
As for method 2 (»storing the bounds interim and split the operation into two Scripts«) and 3 (»have the Script check for stored bounds, if none are found …«) the information can be stored in a txt-file in some specific place or in a custom NameSpace in the metadata.
For the latter see
http://forums.adobe.com/message/4066555#4066555
where Paul Riggott uses that to store guide-information.
All in all I would think that method 3 would make the most sense; basically the Script could on every other run store the selected Layer’s bounds and then apply and remove them.
-
buga
copy the properties from one layer to another
Many thanks for your code and information. will be of great help.
I have a doubt.
The layers appear in reverse order
How could reverse the listing of the layers showing?
--
Another question aside in case anyone would be so kind.
There are several methods to copy the coordinates and measures from one layer to another, but I would like to use the transform tool.
How is it done?
I have created an action and I have become a script:
Code: Select allcTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
function Transformar(X,Y,W,H) {
x=(X/2)+X;
y=(Y/2)+Y;
w=W;
h=H;
// Transformar
function step1(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
desc1.putReference(cTID('null'), ref1);
desc1.putEnumerated(cTID('FTcs'), cTID('QCSt'), sTID("QCSAverage"));
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(cTID('Hrzn'), cTID('#Pxl'), x);
desc2.putUnitDouble(cTID('Vrtc'), cTID('#Pxl'), y);
desc1.putObject(cTID('Ofst'), cTID('Ofst'), desc2);
desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'),w);
desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), h);
executeAction(sTID('transform'), desc1, dialogMode);
};
step1(); // Transformar
};
The problem is that I have changed the code so that the width and height, pixels are used instead of percentage
desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'),w);
desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), h);
but does not work properly. Percentages used instead of pixels (only in width/Height) (X/Y work properly)
What is the error? How could I fix it? How do you use the transform tool using code?
Thank you very much in advance, and sorry for my English, I'm Spanish.
greetings
I have a doubt.
The layers appear in reverse order
How could reverse the listing of the layers showing?
--
Another question aside in case anyone would be so kind.
There are several methods to copy the coordinates and measures from one layer to another, but I would like to use the transform tool.
How is it done?
I have created an action and I have become a script:
Code: Select allcTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
function Transformar(X,Y,W,H) {
x=(X/2)+X;
y=(Y/2)+Y;
w=W;
h=H;
// Transformar
function step1(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
desc1.putReference(cTID('null'), ref1);
desc1.putEnumerated(cTID('FTcs'), cTID('QCSt'), sTID("QCSAverage"));
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(cTID('Hrzn'), cTID('#Pxl'), x);
desc2.putUnitDouble(cTID('Vrtc'), cTID('#Pxl'), y);
desc1.putObject(cTID('Ofst'), cTID('Ofst'), desc2);
desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'),w);
desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), h);
executeAction(sTID('transform'), desc1, dialogMode);
};
step1(); // Transformar
};
The problem is that I have changed the code so that the width and height, pixels are used instead of percentage
desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'),w);
desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), h);
but does not work properly. Percentages used instead of pixels (only in width/Height) (X/Y work properly)
What is the error? How could I fix it? How do you use the transform tool using code?
Thank you very much in advance, and sorry for my English, I'm Spanish.
greetings
-
pfaffenbichler
copy the properties from one layer to another
How could reverse the listing of the layers showing?
You could for example change the for-function to
Code: Select all for (var m = 0; m < theParent.layers.length; m++) {
instead of
Code: Select all for (var m = theParent.layers.length - 1; m >= 0;m--) {
Percentages used instead of pixels (only in width/Height) (X/Y work properly)
Calculate the percentages.
new width / old width * 100 etc.
You could for example change the for-function to
Code: Select all for (var m = 0; m < theParent.layers.length; m++) {
instead of
Code: Select all for (var m = theParent.layers.length - 1; m >= 0;m--) {
Percentages used instead of pixels (only in width/Height) (X/Y work properly)
Calculate the percentages.
new width / old width * 100 etc.
-
pfaffenbichler
copy the properties from one layer to another
Another thing: You could use one Script that acts differently depending on whether the shift-key is being pressed when starting it.
For example you could have the Script store the bounds of activeLayer if running with shift-key pressed, but apply the stored dimensions to the active Layer when running without.
I picked the technique up from Paul Riggott and Mike Hale some time ago.
Code: Select allif(ScriptUI.environment.keyboardState.shiftKey ){
alert ("shift")
}
else {
alert ("no shift")
};
For example you could have the Script store the bounds of activeLayer if running with shift-key pressed, but apply the stored dimensions to the active Layer when running without.
I picked the technique up from Paul Riggott and Mike Hale some time ago.
Code: Select allif(ScriptUI.environment.keyboardState.shiftKey ){
alert ("shift")
}
else {
alert ("no shift")
};
-
buga
copy the properties from one layer to another
Thanks, everything works perfect.
Thank you so much for your help.
regards
Thank you so much for your help.
regards
-
pfaffenbichler
copy the properties from one layer to another
So which method of selecting the target Layer did you use?
Or if you Scripted several versions which one performs most conveniently?
Or if you Scripted several versions which one performs most conveniently?
-
buga
copy the properties from one layer to another
The first method that you suggested. the layers list.
Thank you very much for your code.
I'd try the method 3 (but without external txt) storing metadata. But it's too complicated for me.
I'm not good programmer
regards
Thank you very much for your code.
I'd try the method 3 (but without external txt) storing metadata. But it's too complicated for me.
I'm not good programmer
regards