Here's a js script for InDesign I wrote that exports layouts from a book file and saves them in a desktop folder. It works fine, except that I want to change the position of the automatic numbering. I have it set up now so that it adds an underscore between the filename and the automatic numbering to avoid confusion, because the end of the filenames all contain a number.
The script outputs files named like this: SampleStory_FL164.jpg. The number 4 (right after "FL16" which is part of the filename)is the automatic page number, so in this case this is page number 4 from a multipage indd document. I'd like to move the 4 from its current position to right before the underscore, so that the file would be renamed like this: SampleStory4_FL16.jpg. I can do this outside of InDesign (ignoring the exporting automation) with javascript, as seen here:
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;
But, with InDesign the tacking on of a number as the files are saved seem out of reach and not available to manipulate. I'm thinking here of the exportFile method or maybe the File object. Is there a way to do this? Here's the code I have working now:
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.");
}
}
You may not be able to alter InDesign's built-in naming convention, but you could always rename them after you export them using the File object's rename method. Something like this would work (placed after your for loop):
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"));
}
Rename returns a boolean value so you could log information on any failures if you needed to.