2013-05-13 58 views
0

我是一個相當新的程序員,所以請耐心等待。我正在嘗試一些相當先進的東西,但我喜歡挑戰。 :〜)我正在使用Adobe的ESTK(ExtendScript Toolkit)爲InDesign CS6編寫一個複雜的腳本。我經歷了大部分的教程,並且學到了很多東西,但我現在已經遇到了一堵牆。Javascript代碼打開子目錄中的幾個文件

我需要腳本來檢測某個文件夾是否符合某個標準,如果是,則深入該文件夾,計算其所有子文件夾並打開每個子文件夾中的每個.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文件夾」的打包文件夾,然後打開裏面的.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 

我希望腳本依次打開每個文件,然後對它們執行一些任務。我確信這是可能的,但我只需要知道如何編碼它。如果您需要更多信息,請告訴我,我很樂意提供。謝謝!

+1

您的主題已經非常複雜。所以沒有簡單的答案。你應該看看這些東西: JavaScript中的遞歸 http://www.codecademy.com/courses/javascript-lesson-205 ExtendScript中的文件 http://jongware.mit.edu/Js/pc_File。 html ExtendScript中的文件夾 http://jongware.mit.edu/Js/pc_Folder.html 希望能有所幫助。 – fabianmoronzirfas 2013-05-14 06:30:00

+0

你可以畫出更多的目錄結構嗎? – 2013-05-14 13:15:33

+0

謝謝你的鏈接,@fabiantheblind;我會盡快檢查出來,希望能夠學到更多。是的,這絕對是一個複雜的劇本,所以這是我的試火。 – Sturm 2013-05-15 14:24:18

回答

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; 
} 
+0

作爲JavaScript/ExtendScript的完全新手,我需要仔細研究你的代碼,看看我是否可以在我盲目地將其粘貼到我的程序之前加以注意。 – Sturm 2013-05-15 13:52:36

+0

@Sturm總是一個明智的決定。建議:不要試圖從開始到完成的代碼路徑,而是單獨查看每個函數,並嘗試驗證它是否完成評論所說的內容。這樣更簡單。 – dln385 2013-05-15 14:15:34

+0

雖然我有很多問題,但我現在只是從一個開始:在第3行中,調用函數「forEachDescendantFile」時的第二個參數是一個函數。這很好,但是不應該將參數傳遞給_that_函數嗎?如果我沒有弄錯的話,它期望得到一個'{File}'類型的參數。 – Sturm 2013-05-16 17:34:27