Skip to main content
Known Participant
October 4, 2021
Question

Stop a loop and go to next task

  • October 4, 2021
  • 3 replies
  • 301 views

Hi everyone, Im working on a script where it unlocks and hide all layers on a loop. After the loop, it will show the ARTWORK and Dieline layer and perform the next task. However, Im stuck on the loop and wasnt able to execute the next task (starting line 16) 

 

thank you in advance!

 

main();

function main() {
var myDoc=app.activeDocument;
var layerCount=myDoc.layers.length;
for (var ii = layerCount - 1; ii >= 0; ii--) {
var currentLayer = myDoc.layers[ii];
currentLayer.locked = false;
currentLayer.visible = false;
if ((currentLayer.locked == true) || (currentLayer.visible == true)){
currentLayer.locked = false;
currentLayer.visible = false;
}
}
};
// IM STUCK HERE!! it always go back to the loop
if (layerCount = 1) {
app.documents.everyItem().layers.itemByName("ARTWORK").visible = true;
app.documents.everyItem().layers.itemByName("Dieline").visible = true;
app.activeDocument.groups.everyItem().ungroup();
alert('Please Select Object "DIECUT"');
var doc = app.activeDocument;
var al = doc.layers.itemByName("Dieline");
var dl = doc.layers.itemByName("ARTWORK");
var col = doc.swatches.itemByName("PRINT FILE WHITE");
 
try {
var sel = app.selection[0];
sel.strokeColor = col;
sel.strokeWeight = "0.0125 pt";
sel.itemLayer = dl;
app.documents.everyItem().layers.itemByName("Dieline").visible = false;
} catch(e) { }
 
if (!col.isValid) {
col = doc.colors.add({
colorValue: [0,0,0,0],
space: ColorSpace.CMYK,
name: "PRINT FILE WHITE",
});
}
};
This topic has been closed for replies.

3 replies

BarlaeDC
Community Expert
Community Expert
October 4, 2021

Hi,

 

At the bit where you want the loop to stop, just call break;

a "bad example" follows

 

for ( var i =0; i < 10; i++) {

if (i==3) {break;}

}

 

Peter Kahrel
Community Expert
Community Expert
October 4, 2021

By the way, this fragment makes no sense (it has nothing to do with the problem you asked about):

currentLayer.locked = false;
currentLayer.visible = false;
if ((currentLayer.locked == true) || (currentLayer.visible == true)){
  currentLayer.locked = false;
  currentLayer.visible = false;
}

You set locked and visible to false, then you check whether either is true.

Known Participant
October 4, 2021

Hi @Peter Kahrel@BarlaeDC is there a way that the script will no longer execute the loop once the loop task is met?

 

if you'll notice, I have an "alert" where it instruct to manualy select an object, and it will perform the next task. However when I manually select the object and click the script for the second time, the script will again perform the loop.

BarlaeDC
Community Expert
Community Expert
October 5, 2021

HI,

 

I think you have an extra bracket in your code, aroudn line 13 there is a close bracket that closes the main functions, I am not sure that is what you are wanting to do.

 

So the code from "if (( layerCount == 1)) { ....} is not in the main function, to fix you need to place the } from line 13 at the end of the code.

 

 

Peter Kahrel
Community Expert
Community Expert
October 4, 2021

This line:

 

if (layerCount = 1) {

 

sets 'layerCount to 1. To compare its value, use this:

 

if (layerCount == 1) {