Reply To: Script to update a page number and file name.

#85083
Peter Kahrel
Participant

You say you’re new to scripting, so before you get into file renaming you should read chapter 3 in the JavaScript Tools guide, see Help > JavaScript Tools Guide CC (or CS6, etc.) in the ESTK.

You can’t rename an open document, but what you can is save it under a different name and delete the original. Here’s some code to get you started (including a simple dialog to get a page number):

(function () {
  // Get a page number from the user
  var page = prompt ('Page number: ', '0');
  // If the user pressed Escape or just closed the window, 'page' is null
  if (page !== null) {
    // Store the document's name
    var docPath = app.documents[0].fullName;
    // Create the new name by replacing the initial number with the user input
    var outfile = File (app.documents[0].filePath+'/'+app.documents[0].name.replace (/^\d+/, page));
    // Save the document using the new name
    app.documents[0].save (outfile);
    app.documents[0].close();
    // Delete the original document
    docPath.remove();
  }
}

Peter

This article was last modified on May 23, 2016

Comments (0)

Loading comments...