Script stops after one cycle
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.
