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

How to change position of the automatic filename numbering with javascript as InDesign exports files?

这是我为 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.");
    }
}

您可能无法更改 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+)/, ""));
}

重命名 returns 一个布尔值,以便您可以在需要时记录有关任何失败的信息。