用于在子目录中打开多个文件的Javascript代码

问题描述 投票:0回答:1

我是一个相当新的程序员。我正在尝试一些非常先进的东西,但我喜欢挑战。 :〜)我正在使用Adobe的ESTK(ExtendScript Toolkit)为InDesign CS6编写复杂的脚本。我已经完成了大部分的教程并且学到了很多东西,但我现在遇到了一堵墙。

我需要脚本来检测某个文件夹是否符合某个条件,如果是,则深入研究该文件夹,计算其所有子文件夹,然后打开每个子文件夹中的每个.indd文件,执行每个人的任务。我今天刚刚开始编写脚本,这是我到目前为止所做的:

var getDataDialog = app.dialogs.add({name:"Job Info"});
with(getDataDialog){
    // Add a dialog column.
    with(dialogColumns.add()){
        // Create the order number text edit box.
        var orderNumTextEditBox = textEditboxes.add({minWidth:80});
        }
    }
// Show the dialog box.
var dialogResult = getDataDialog.show();
if(dialogResult == true){
    // Get the order number and put it in the variable "orderNum".
    var orderNum = orderNumTextEditBox.editContents;
    // Get the first three numbers of the order number and put it in "orderPrefix".
    var orderPrefix = orderNum.substr(0,3);
    // Remove the dialog box from memory.
    getDataDialog.destroy();
    // Store the folder prefix into "folderPrefix".
    var folderPrefix = "/g/ ArtDept/JOBS/" + orderPrefix + "000-" + orderPrefix + "999/"
    // Open the document with the order number.
    var myDoc = app.open(File(folderPrefix + orderNum + " Folder/" + orderNum + ".indd"), true);
    }
else{
    getDataDialog.destroy();
    }

因此,如果输入的订单号是“405042”,它将在“405000-405999”文件夹中查找,然后进入名为“405042 Folder”的打包文件夹,然后在其中打开.indd文件。麻烦的是,我们有时在一个文件夹中有几个包。例如,我们可能有:

405000-405999/405007/405007_N10/405007_N10.indd
405000-405999/405007/405007_C12/405007_C12.indd
405000-405999/405007/405007_Orange/405007_Orange.indd

我希望脚本依次打开每个文件,并对它们执行一些任务。我确信它是可能的,但我只需要知道如何编码它。

javascript adobe directory adobe-indesign extendscript
1个回答
2
投票

如果我理解你的问题,有两个部分:

第1部分:查找符合特定条件的特定文件夹。 (看起来你已经覆盖了这个。)

第2部分:对于作为此文件夹后代的每个InDesign文档,打开它并对其进行一些处理。

在下面的示例中,我已经标记了应该添加查找顶部文件夹的代码以及操作每个文档的代码的位置。如果按原样运行示例,它将使用脚本的父文件夹作为顶级文件夹,对于每个后代文档,它将只记录其名称。

// TODO: (Part 1) Put code here that determines the top folder.
var topFolder = (new File($.fileName)).parent; // Change me. Currently the script's folder.
forEachDescendantFile(topFolder, doStuffIfdocument); // Don't change this line.

/**
 * Does stuff to the document.
 */
function doStuff(document) {
    // TODO: (Part 2) Put code here to manipulate the document.
    $.writeln("Found document " + document.name);
}

/**
 * Opens the given file if it ends in ".indd". Then calls doStuff().
 * 
 * @param {File} oFile
 */
function doStuffIfdocument(oFile) {
    if (matchExtension(oFile, "indd")) {
        var document = app.open(oFile);
        try {
            doStuff(document);
        }
        finally {
            document.close(SaveOptions.YES);
        }
    }
}

/**
 * Calls the callback function for each descendant file of the specified folder.
 * The callback should accept a single argument that is a File object.
 * 
 * @param {Folder} folder The folder to search in.
 * @param {Function} callback The function to call for each file.
 */
function forEachDescendantFile(folder, callback) {
    var aChildren = folder.getFiles();
    for (var i = 0; i < aChildren.length; i++) {
        var child = aChildren[i];
        if (child instanceof File) {
            callback(child);
        }
        else if (child instanceof Folder) {
            this.forEachDescendantFile(child, callback);
        }
        else {
            throw new Error("The object at \"" + child.fullName + "\" is a child of a folder and yet is not a file or folder.");
        }
    }
}

/**
 * Returns true if the name of the given file ends in the given extension. Case insensitive.
 *
 * @param {File} iFile
 * @param {String} sExtension The extension to match, not including the dot. Case insensitive.
 * @return {boolean}
 */
function matchExtension(iFile, sExtension) {
    sExtension = "." + sExtension.toLowerCase();
    var displayName = iFile.displayName.toLowerCase();
    if (displayName.length < sExtension.length) {
        return false;
    }
    return displayName.slice(-sExtension.length) === sExtension;
}
© www.soinside.com 2019 - 2024. All rights reserved.