Script to update a page number and file name.

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to update a page number and file name.

Viewing 1 reply thread
  • Author
    Posts
    • #84421
      Glen Hart
      Member

      Hi I wonder if you guys might be able to help. I have been learning scripting over the past couple of weeks so i’m still really new to this and tying to get my head around everything. I’m currently trying to develop a script that i can run through a batch processor though to update the page number and file name.

      example. filename “0198_Catelogue1.indd” is page 198. But i want to change this to:
      filename “0201_Catelogue1.indd” is page 201.

      I have managed to create a script to open a folder of .indd files, change the page numbers and then save and close the files and this works succesfully with the script below but I cannot work out a way to update the filename. Is this actually possible? I can do this separately if need be but it would be nice to have it all work as one seamless script.

      //Adjust the variable of x to reflect the number you want to add/subtract from the current catelogue page
      // Set the operator for the variable “pgNum” to either a + or – depending on whether you are adding or subtracting pages.

      var x = 25 ;
      var y = (app.activeWindow.activePage.name) – 0;
      var pgNum = y + x;

      app.activeDocument.sections[0].pageNumberStart = pgNum;

      Another thing I would like to do is to try and implement is a prompt alert at the start. Then once the script has been actioned, it will ask the user to select the number of pages they need to amend the document by and there will be two buttons to add or remove that variable from the current page number. Ideally this would only need to be done once though so i don’t know if it would need to be done separately from a batch processor.

    • #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

Viewing 1 reply thread
  • You must be logged in to reply to this topic.
>