Copy link to clipboard
Copied
Hello All
I created a (my first) script which should loop the construct() function until my layer named "END" has been reached. But it always runs just one cycle.
My main part looks like this:
var end_reached = false;
// Check if last layer named "END" in "Front.psd" have been reached and stop script / otherwise repeat function "construct".
for(var i = 0; i < designDoc.layers.length; i++){
var layer = designDoc.layers
if(currentLayer.name.toLowerCase().indexOf("end") >= 0){
alert ('All Designs have been Applied', 0);
end_reached = true
break;
}
}
if (!end_reached) {
construct()
}
The complete script looks like that:
//================================= OPTIONS =================================
// jpg options.
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 9;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
// PNG options.
pngOptions = new PNGSaveOptions()
pngOptions.compression = 0
pngOptions.interlaced = false
//================================= VARIABLES =================================
// Variables for the "Paste in Place" function
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
// Checks which one is the Mockup.psd file (as my names differ but allways contain "Mockup")
var docs = app.documents;
for(var i = docs.length - 1; i >= 0; i--){
if(docs.name.indexOf('Mockup') > 0){
var mockupDoc = docs;
}
}
// Setting variable for layerset "GTO Background" and set it to visible
var gtoBG = mockupDoc.layerSets.getByName("GTO Background");
// This stores Front.psd file's name so Photoshop can look for the active layers in the correct document.
var designDoc = app.documents.getByName("Front.psd");
var currentLayer = designDoc.activeLayer;
// Getting the name and location of Mockup.psd;
var mockupDocName = mockupDoc.name;
if (mockupDocName.indexOf(".") != -1) {var basename = mockupDocName.match(/(.*)\.[^\.]+$/)[1]}
else {var basename = mockupDocName};
// Getting the location of Mockup.psd;
var mockupDocPath = mockupDoc.path
//================================= MAIN =================================
var end_reached = false;
// Check if last layer named "END" in "Front.psd" have been reached and stop script / otherwise repeat function "CreateItems".
for(var i = 0; i < designDoc.layers.length; i++){
var layer = designDoc.layers
if(currentLayer.name.toLowerCase().indexOf("end") >= 0){
alert ('All Designs have been Applied', 0);
end_reached = true
break;
}
}
if (!end_reached) {
construct()
}
//================================= FUNCTION TO EXPORT PNG and JPG FROM EACH DESIGN (var designDoc aka FRONT.psd) =================================
function construct() {
// Sets "FRONT.psd" aka designDoc as active document.
app.activeDocument = mockupDoc;
// Toggles layerset "GTO BBACKGROUND" to visible for .jpg version export.
gtoBG.visible = true;
// Sets "FRONT.psd" aka designDoc as active document.
app.activeDocument = designDoc;
// This selects and makes next Layer in Front.psd visible (only this one)
for(i=0; i < designDoc.layers.length; )
{
if(designDoc.layers==currentLayer)
{
a=i;
//alert(a);
i = designDoc.layers.length;
}
else{ i++; }
}
try
{
var nextLayer = designDoc.layers[a+1];
var check = nextLayer.visible;
}
catch(e)
{
var nextLayer = designDoc.layers[0];
var check = nextLayer.visible;
}
designDoc.activeLayer = nextLayer;
if (check == false)
designDoc.activeLayer.visible = false;
// Makes only selected layer in "Front.psd" aka designDoc visible.
toggleVisibility();
// Saves "Front.psd" to original destination and updates embeded content (smart object) in "Mockup.psd" aka mockupDoc
var designDocPath = designDoc.path
var Name = designDoc.name.replace(/\.[^\.]+$/, '');
designDoc.saveAs(File(designDocPath + "/" + Name + ".psd"));
// Make "Mockup.psd" aka mockupDoc file active document
app.activeDocument = mockupDoc;
// Checks if current Design is a "FLEX" print or not.
if(designDoc.activeLayer.name.toLowerCase().indexOf("flex") >= 0){
// Hides Texture Overlays
Selecttexures();
}
// Creates a Sharpness Layer for the jpg version.
createSharpnessLayer ()
// Saves the composited image from Mockup.psd containing Filename minus "Mockup", Name of the visible layer "Colors" layerset and the Name of the active layer in Front.psd
mockupDoc.saveAs((new File(mockupDocPath+'/' + basename + ' ' + designDoc.activeLayer.name +'.jpg')),jpegOptions,true);
// Remove OLD Sharpness Layer
mockupDoc.layers.getByName('Sharpness').remove();
// Toggles layerset "GTO BBACKGROUND" to hidden for .png version export.
gtoBG.visible = false;
// Creates a Sharpness Layer for the png version.
createSharpnessLayer ()
// Saves the composited image from Mockup.psd containing Filename minus "Mockup", Name of the visible layer "Colors" layerset and the Name of the active layer in Front.psd
mockupDoc.saveAs((new File(mockupDocPath+'/' + basename + ' ' + designDoc.activeLayer.name +'.png')),pngOptions,true);
// Remove OLD Sharpness Layer
mockupDoc.layers.getByName('Sharpness').remove();
} //END Create Items function
//================================= HELPERS =================================
// "Paste in Place" function.
function pasteInPlace(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
desc1.putBoolean(sTID("inPlace"), true);
desc1.putEnumerated(cTID('AntA'), cTID('Annt'), cTID('Anno'));
executeAction(cTID('past'), desc1, dialogMode);
};
// "Toggle Visibility" function is the same as Alt/Click on a layers eye.
function toggleVisibility() {
var desc = new ActionDescriptor();
var list1 = new ActionList();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );
list1.putReference( ref );
desc.putList( charIDToTypeID('null'), list1 );
desc.putBoolean( charIDToTypeID('TglO'), true );
executeAction( charIDToTypeID('Shw '), desc, DialogModes.NO );
};
// Create "Sharpness" Layer.
function createSharpnessLayer () {
mockupDoc.selection.selectAll();
mockupDoc.selection.copy(true);
pasteInPlace();
mockupDoc.activeLayer.name = "Sharpness"
mockupDoc.activeLayer.move( mockupDoc, ElementPlacement.PLACEATBEGINNING );
mockupDoc.activeLayer.applyHighPass(0.5)
mockupDoc.activeLayer.blendMode = BlendMode.LINEARLIGHT;
mockupDoc.activeLayer.opacity = 50;
};
// Select all print texture overlays in "Mockuo.psd" aka mockupDoc
function Selecttexures() {
// Select
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.putName(cTID('Lyr '), "LIGHT FABRIC TEXTURE");
desc1.putReference(cTID('null'), ref1);
desc1.putBoolean(cTID('MkVs'), false);
var list1 = new ActionList();
list1.putInteger(43);
desc1.putList(cTID('LyrI'), list1);
executeAction(cTID('slct'), desc1, dialogMode);
};
// Select Linked Layers
function step2(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);
executeAction(sTID('selectLinkedLayers'), desc1, dialogMode);
};
// Hide
function step3(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var list1 = new ActionList();
var ref1 = new ActionReference();
ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
list1.putReference(ref1);
desc1.putList(cTID('null'), list1);
executeAction(cTID('Hd '), desc1, dialogMode);
};
step1(); // Select
step2(); // Select Linked Layers
step3(); // Hide
};
Any help highly appreciated as i'm a beginner and as it seems, i can't find the reason for the issue myself.
Ok, seems i have found the issue. My Loop was not executing as it should.
Changed to the following and it works now :
var create = true;
while (create) {
//Do main function
if(designDoc.activeLayer.name.indexOf("END") >= 0){
var create = false; //While loop stops when my IF argument has become true
}
}
Copy link to clipboard
Copied
Try ExtendSript Toolkit, paste your code here, set breakpoint on the first line and run script. Then click on next button again and again to see what is going on in each line of code. Don't forget look inside variables. E.g. length of your layers.
Copy link to clipboard
Copied
Hello Jarda
I opened the script in Extended Script Toolkit and did run it. It throes no errors and plays the main function one time but does not start again from the beginning?
Probably my understanding of how a function is executed is completely wrong. Does a function not being repeated over and over again if no "break" (or other) argument has been executed/reached?
What tells the last line of function code to start again from the beginning?
Sorry if this is a stupid question, but i'm just starting my scripting experience
Copy link to clipboard
Copied
To process all layers in a document I normally see recursion being used to process layers in layer groups. I do not see anything like that in your script. Can your Photoshop contain layer groups?
Copy link to clipboard
Copied
Hello JJMack
no, the document to be processed contains only layers and no layersets. I will try Jerda's suggestion and check in The ES Toolkit why the action stops. Think the issue could be in these lines:
// This selects and makes next Layer in Front.psd visible (only this one)
for(i=0; i < designDoc.layers.length; )
{
if(designDoc.layers==currentLayer)
{
a=i;
//alert(a);
i = designDoc.layers.length;
}
else{ i++; }
}
try
{
var nextLayer = designDoc.layers[a+1];
var check = nextLayer.visible;
}
catch(e)
{
var nextLayer = designDoc.layers[0];
var check = nextLayer.visible;
}
designDoc.activeLayer = nextLayer;
if (check == false)
- designDoc.activeLayer.visible = false;
Copy link to clipboard
Copied
Ok, seems i have found the issue. My Loop was not executing as it should.
Changed to the following and it works now :
var create = true;
while (create) {
//Do main function
if(designDoc.activeLayer.name.indexOf("END") >= 0){
var create = false; //While loop stops when my IF argument has become true
}
}
Copy link to clipboard
Copied
This will fall into endless loop without layer "END"
Please read some documentation how loops works: JavaScript for Loop
If this code fail:
mockupDoc.layers.getByName('Sharpness').remove();
Wrap it into Try/catch
Copy link to clipboard
Copied
Jarda
Thanks for the heads up. I will look into it, but basically it should loop for a unlimited amount of cycles until the layer END is present. I tried with looping trough the documents layers length and letting the loop cycle for this amount. Unfortunately i could not get it working.
Copy link to clipboard
Copied
https://forums.adobe.com/people/johny+roger schrieb
…but basically it should loop for a unlimited amount of cycles until the layer END is present …
But please read carefully what Jarda Bereza wrote
https://forums.adobe.com/people/Jarda+Bereza schrieb
This will fall into endless loop without layer "END"
That why: check at first (before a while loop) if the layer "end" really in your document exists and if the script really can reach the layer in the while loop.
Otherwise: This will fall into endless loop without layer "END"