So below is a script which opens ai files in a series of folder inside a main folder and saves them somewhere as a PDF. The script works in that it loops through the available files and PDFs them. when i run this step by step it, it reaches the last file in the last folder and ends. When I just run it, it gets done with looping through and starts looping through again but not at the command of extendscript. I press stop script and the script stops but illustrator keeps looping endlessly though without extendscript. I have to force close Illustrator. But as i said the loop has a stop in it. it works fine run step by step. If anyone has any suggestions? Here is the script(not very pretty): //where to save to var topFolder =Folder.selectDialog("Where?"); // Change me. Currently the script's folder. x = 0 var up = false const afolders var descended = false forEachDescendantFile(topFolder, doStuffIfdocument, up); // Don't change this line. //checks that the file is ai and ends with TF. if so it is opened function doStuffIfdocument(oFile) { var checkName = oFile.fsName if (checkName.search(".ai") > 1 && checkName.search("TF") > 1) { var document = app.open(oFile); try { doStuff(document); } catch (err){ document.close(SaveOptions.DONOTSAVECHANGES); } finally{ if (x < afolders){ up = true forEachDescendantFile(topFolder, doStuffIfdocument, up) } } } } //saves it as a PDF function doStuff(document) { var aiApp = app.activeDocument; saveName ="C:\\Users\\user\\Desktop\\TF PDF" + "\\" + aiApp.name; saveName = saveName.substring(0, saveName.length - 3); saveName = saveName + "_183" dest = new File (saveName); saveOpts = new PDFSaveOptions(); saveOpts.pDFPreset = 'dane'; aiApp.saveAs(dest, saveOpts); aiApp.close(SaveOptions.DONOTSAVECHANGES); } // loops though folders and files function forEachDescendantFile(folder, callback, up) { var aChildren = folder.getFiles(); if (this.descended == false){ afolders = aChildren.length } for (var i = 0; i < aChildren.length; i++) { if (up == true){ i = x up = false } var child = aChildren; if (child instanceof File) { callback(child); } else if (child instanceof Folder) { x += 1 this.descended = true if (child.name != "CNX") { this.forEachDescendantFile(child, callback); } else if (x > afolders){ //this should kill the script. throw new Error("Something went badly Right!"); } else{ up = true } } else { throw new Error("The object at \"" + child.fullName + "\" is a child of a folder and yet is not a file or folder."); } } }
... View more