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

标签 javascript adobe-indesign extendscript

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

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

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+)/, "$2$1"));
}

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

关于javascript - InDesign 导出文件时如何使用 javascript 更改自动文件名编号的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38988611/

相关文章:

adobe - 将网站数据导入 Adob​​e InDesign

javascript - 在 InDesign (Javascript) 的所有奇数页中选择所有文本框架并右对齐

javascript - 数组在遍历它时表现得很奇怪

javascript - 使用 JavaScript 更改 CSS 时出现问题

javascript - Adobe InDesign CS6 "undefined is not an object"

javascript - Canvas drawImage 返回错误

javascript - 使用 basiljs 交换项目

adobe-indesign - 多状态对象中的图像

javascript - 使用标准验证不同类型的表单输入

javascript - 组合下拉列表不使用 javascript jquery getJSON 从文本文件生成数据