【问题标题】:Javascript: How to loop through filesJavascript:如何循环文件
【发布时间】:2014-05-19 13:10:41
【问题描述】:

我正在制作一个 Illustrator CS6 Javascript,它执行以下操作:

  1. 打开一个包含 Illustrator 文件的文件夹
  2. 打开文件夹中的每个文件(这些文件称为源文件)
  3. 选择源文件的所有内容
  4. 复制源文件的内容
  5. 创建一个新的目标文件将这些内容粘贴到目标文件中作为一个新层
  6. 确保新层与旧源文件同名

我的脚本可以正常工作,只是它不能正确循环源文件夹中的文件。相反,它在第一个源文件上运行良好。但随后它会无休止地将第二个源文件粘贴到目标文档中(即它不会移动到任何其他源文件)。它只是无休止地粘贴,所以我不得不强制退出!

我怎样才能让它正确地遍历文件夹然后移动到下一个文件。

这是我的代码:

// 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 ReviewGraphic Design 中发布此内容,但我认为堆栈溢出是发布此内容的最佳位置,因为这是关于javascript 循环的一般问题,所以我希望这是正确的地方。

【问题讨论】:

    标签: javascript loops adobe-illustrator


    【解决方案1】:

    您似乎在每个循环中都对变量使用“i”,从而在也使用相同变量的其他循环中为其提供了一系列意外值。我会尝试为每个循环使用一个单独的变量。例如。对于 j=0、对于 k=0、对于 l=0 等

    【讨论】:

      猜你喜欢
      • 2015-03-11
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 2020-05-18
      • 2015-07-29
      • 1970-01-01
      • 2017-12-14
      • 2020-08-08
      相关资源
      最近更新 更多