如何使用javascript更改自动文件名编号的位置作为InDesign导出文件?

时间:2016-08-17 05:19:15

标签: javascript adobe-indesign extendscript

这是我写的InDesign的js脚本,它从书籍文件中导出布局并将它们保存在桌面文件夹中。它工作正常,除了我想改变自动编号的位置。我现在设置它,以便在文件名和自动编号之间添加下划线以避免混淆,因为文件名的末尾都包含一个数字。

脚本输出如下名称的文件:SampleStory_FL164.jpg。数字4(在“FL16”之后,它是文件名的一部分)是自动页码,因此在这种情况下,这是来自多页indd文档的页码4。我想将4从当前位置移动到下划线之前,以便将文件重命名为:SampleStory4_FL16.jpg。我可以使用javascript在InDesign之外执行此操作(忽略导出自动化),如下所示:

myDocument = "SampleStory_FL164.jpg"
//get position right after "FL16"
var firstIndex = myDocument.indexOf("FL16") + 4;
//get position right before ".jpeg"
var secondIndex = myDocument.indexOf(".jpg");
//find anything between firstIndex and secondIndex and assign it to a variable
var charsBetweenIndexes = myDocument.substring(firstIndex, secondIndex);
//search file name and replace anything between first and second index
var newFileName = myDocument.replace(charsBetweenIndexes, "");
//add what you deleted back right after the file name and before the underscore
newFileName = newFileName.replace("_", charsBetweenIndexes + "_");
//change myDocument to the value of newFileName before exporting
myDocument = newFileName;

但是,使用InDesign时,在保存文件时添加数字似乎遥不可及,无法操作。我在这里想的是exportFile方法或者File对象。有没有办法做到这一点?这是我现在使用的代码:

Main();
// If you want the script to be un-doable, comment out the line above, and remove the comment from the line below
// app.doScript(Main, undefined, undefined, UndoModes.ENTIRE_SCRIPT,"Run Script");

function Main() {
    // Check to see whether any InDesign documents are open.
    // If no documents are open, display an error message.
    if(app.documents.length > 0) {
        app.jpegExportPreferences.exportingSpread = false;  
app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.EXPORT_ALL;  
if (app.books.length != 1)  
     alert ("This only works when you have one (1) book open");  
else      
     for (b=0; b<app.books[0].bookContents.length; b++)  
     {      
          var myDocument = app.books[0].bookContents[b].fullName ;
          c = app.open(app.books[0].bookContents[b].fullName);  
          myDocument = myDocument.name.replace("indd","jpg");
          myDocument = myDocument.replace("FL16", "FL16_");
          c.exportFile (ExportFormat.JPG, File(Folder.desktop + "/EDIT_Jpgs/" + myDocument));      
     }  
    }
    else {
        // No documents are open, so display an error message.
        alert("No InDesign documents are open. Please open a document and try again.");
    }
}

1 个答案:

答案 0 :(得分:0)

您可能无法更改InDesign的内置命名约定,但在使用File对象的重命名方法导出它们之后,您始终可以重命名它们。这样的东西可以工作(放在你的for循环之后):

var myFiles = Folder(Folder.desktop + "/EDIT_Jpgs/").getFiles("*.jpg");
for (var i = 0; i < myFiles.length; i++){
    var myFile = myFiles[i];
    //Adjust this regular expression as needed for your specific situation.
    myFile.rename(myFile.name.replace(/(_FL16)(\d+)/, "$2$1"));
}

重命名返回一个布尔值,以便您可以根据需要记录任何失败的信息。