我正在制作一个执行以下操作的Illustrator CS6 Javascript:
我的脚本有效,但它没有正确地遍历源文件夹中的文件。相反,它在第一个源文件上运行正常。但是它无休止地只是将第二个源文件粘贴到目标文档中(即它不会移动到任何其他源文件中)。它只是无休止地粘贴,所以我必须强行退出!
如何让它正确循环浏览文件夹,然后转到下一个文件。
这是我的代码:
// JavaScript Document
//Set up vairaibles
var destDoc, sourceDoc, sourceFolder, newLayer;
// Select the source folder.
sourceFolder = Folder.selectDialog('Select the folder with Illustrator files that you want to mere into one', '~');
destDoc = app.documents.add();
// If a valid folder is selected
if (sourceFolder != null) {
files = new Array();
// Get all files matching the pattern
files = sourceFolder.getFiles();
if (files.length > 0) {
// Get the destination to save the files
for (i = 0; i < files.length; i++) {
sourceDoc = app.open(files[i]); // returns the document object
var myLayers = sourceDoc.layers; // Select All layers in Active Document
//Go through all layers of source document and copy artwork
for (i = 0; i < myLayers.length; i++) {
myLayers[i].hasSelectedArtwork = true;
};
with(sourceDoc) {
var count = pageItems.length;
for (var i = 0; i < count; i++) {
pageItems[i].selected = true;
}
redraw();
copy();
for (var i = 0; i < count; i++) {
pageItems[i].selected = false;
}
}
//Create a new title variable that has the title of the source document
var title = sourceDoc.name;
var title = title.substring(0, title.length - 4); //(remove extension from name)
//Close the Source Document
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
//Open the Destination Document and create a new layer in it that is named after the title variation
newLayer = destDoc.layers.add();
newLayer.name = title;
//Paste into this new layer
newLayer = app.paste();
}
}
else {
alert('No matching files found');
}
}
聚苯乙烯。我不确定是否应该在Code Review或Graphic Design发布此内容,但我认为Stack溢出是发布此内容的最佳位置,因为它是关于javascript循环的一般问题,所以我希望这是正确的地方。
答案 0 :(得分:5)
似乎您在每个循环中使用“i”作为变量,在其他循环中为其提供一系列意外值,这些循环也使用相同的变量。我会尝试为每个循环使用一个单独的变量。例如。对于j = 0,对于k = 0,对于l = 0等,