16 Replies Latest reply on Sep 15, 2017 6:02 AM by attil

    Job Number Variable Script...

    attil Level 1

      Sorry for reposting, but i did so in the wrong forum.

       

      I found this script for adding Job Numbers using Indesign Variables, but i was wondering if anyone knows how to get it to read the first 9 characters (numbers and letters) of the filename instead of just the first 2? Any help would be greatly appreciated.

       

      Here's what i'm trying to acheive:

      I have an indesign slug with job information in it. I want the script/variable to update a "JobNumber" text field based on the first 9 characters in the filename. Eg. 12ABC1234_client_description

       

      Thanks in advance.

       

      app.addEventListener("afterSaveAs", updateJobNumber, false);

      function updateJobNumber ()
      {
        
      if (app.activeDocument.saved)
        
      {
        varRef
      = app.activeDocument.textVariables.item("JobNumber");
        
      if (varRef.isValid)
        
      {
        fileId
      = app.activeDocument.fullName.name.match(/^\d+/);
        
      if (fileId)
        
      {
        
      if (varRef.variableOptions.contents != fileId[0])
        
      {
        varRef
      .variableOptions.contents = fileId[0];
        app
      .activeDocument.save();
        
      }
        
      }
        
      }
        
      }
      }

        • 1. Re: Job Number Variable Script...
          jakec88782761 Level 2

          Hello,

           

          The line you want to change is this one:

           

          fileId = app.activeDocument.fullName.name.match(/^\d+/);

           

           

          The .match method at the end of the line is looking only for digits at the moment.

           

          So, you want to change it so it matches the first 9 digits and uppercase letters?

           

          Try using this grep:

          ^[\d\u]{9}

           

          You might need to use the findGrep() method.

          • 2. Re: Job Number Variable Script...
            attil Level 1

            Thank you for your response. I must be missing something because nothing happens when i run the script.

            • 3. Re: Job Number Variable Script...
              jakec88782761 Level 2

              Do you keep the first and last forward slash?

              .match(/put your grep here/);
              
              • 4. Re: Job Number Variable Script...
                attil Level 1

                Actually i got this error when i restarted the application:

                 

                Offending Text: ^

                • 5. Re: Job Number Variable Script...
                  attil Level 1

                  Ok, i tried this and i no longer get the error, but still nothing happens. Here's the script:

                   

                  app.addEventListener("afterSaveAs", updateJobNumber, false);

                   

                  function updateJobNumber ()

                  {

                     if (app.activeDocument.saved)

                     {

                    varRef = app.activeDocument.textVariables.item("JobNumber");

                     if (varRef.isValid)

                     {

                    fileId = app.activeDocument.fullName.name.match(/^[\d\u]{9}/);

                     if (fileId)

                     {

                     if (varRef.variableOptions.contents != fileId[0])

                     {

                    varRef.variableOptions.contents = fileId[0];

                    app.activeDocument.save();

                     }

                     }

                     }

                     }

                  }

                  • 6. Re: Job Number Variable Script...
                    jakec88782761 Level 2

                     

                     

                    Also, do you have a text variable called "JobNumber"?

                     

                    Try doing:

                     

                    app.activeDocument.textVariables.itemByName("JobNumber");
                    
                    • 7. Re: Job Number Variable Script...
                      attil Level 1

                      Yes, i have a text variable called JobNumber. See attached.Screen Shot 2017-09-13 at 10.18.42 AM.pngScreen Shot 2017-09-13 at 10.18.50 AM.png

                      • 8. Re: Job Number Variable Script...
                        jakec88782761 Level 2
                        fileId = app.activeDocument.fullName.name.match("^[\\d\\u]{9}");
                        
                        • 9. Re: Job Number Variable Script...
                          attil Level 1

                          Hey, Jake. Thanks again for all the help, but still nothing is happening. So i tried the script on another mac and i got this error message:

                          Screen Shot 2017-09-14 at 9.22.26 AM.png

                          • 10. Re: Job Number Variable Script...
                            jakec88782761 Level 2

                            Hi Attil,

                             

                            I think it's got something to do with the EventListener.

                             

                            When I test your script it works for me.

                             

                            Just run this:

                             

                            function updateJobNumber () {
                            
                              if (app.activeDocument.saved) {
                            
                                varRef = app.activeDocument.textVariables.itemByName("JobNumber");
                                    if (varRef.isValid) {
                                        
                                      fileId = app.activeDocument.fullName.name.match("^[\\d\\u]{9}");
                                      alert(fileId);
                                      
                                        if (fileId) {
                                            if (varRef.variableOptions.contents != fileId[0]) {
                                              varRef.variableOptions.contents = fileId[0];
                                              app.activeDocument.save();
                                            }
                                        }
                                    }
                                }
                            }
                            updateJobNumber ();
                            
                            • 11. Re: Job Number Variable Script...
                              attil Level 1

                              Still nothing. Just FYI, i'm running Indesign CC 2017 on a Mac OS X Yosemite 10.10.5.

                              • 12. Re: Job Number Variable Script...
                                jakec88782761 Level 2

                                Do you get an alert?

                                • 13. Re: Job Number Variable Script...
                                  jakec88782761 Level 2
                                  function updateJobNumber () {  
                                  
                                  varRef = app.activeDocument.textVariables.itemByName("JobNumber");  
                                  if (varRef.isValid) {
                                       fileId = app.activeDocument.fullName.name.match("^[\\d\\u]{9}");  
                                            alert(fileId); 
                                  if (fileId) {  
                                  if (varRef.variableOptions.contents != fileId[0]) {  
                                                    varRef.variableOptions.contents = fileId[0];  
                                                    app.activeDocument.save(); 
                                              } 
                                          } 
                                      } 
                                  } 
                                  updateJobNumber (); 
                                  
                                  • 14. Re: Job Number Variable Script...
                                    attil Level 1

                                    This is the only alert i get upon restarting Indesign:

                                     

                                    Screen Shot 2017-09-14 at 11.36.43 AM.png

                                    • 15. Re: Job Number Variable Script...
                                      Marc Autret Level 4

                                      There are a number of interesting issues here.

                                       

                                      • First, as you are using a startup script purposed to set an event listener, you usually need a #targetengine directive (to keep the function available in memory.)

                                       

                                      • The updateJobNumber function is designed as an event handler, that is, it is only triggered by a save-as event and won't work if called directly from the script. In particular, a startup script won't find any document available, of course. (Which explains the very last error you've encountered.)

                                       

                                      • To me, there is no reason to attach the process to 'afterSaveAs' (which occurs once the document file has been saved on disk.) Regarding the task of updating a text variable before saving the doc, 'beforeSaveAs' seems the way to go. The problem with save-as events is that you can't use doc.save() commands within the handler, as InDesign is already committed in saving the file! 'beforeSaveAs' allows you to provide final refinements to the document just before the system actually writes the file—but the process is already underway, so you don't need doc.save() at all in that context.

                                       

                                      • Last point: the metacharacter \u is GREP-specific and has no meaning in JS RegExp syntax. Use the class [A-Z] instead (at least, as a first approximation.)

                                       

                                      Now, the really interesting question is the following:

                                       

                                      From within a beforeSaveAs handler, how can we identify the final file name of the document which, at this specific moment, still owns its previous fullName property (including invalid cases where the doc hasn't be saved yet) ?

                                       

                                      The answer is, you cannot retrieve the incoming file name from the document itself

                                      BUT

                                      save-as events expose a (hidden?) property that fills the gap: event.fullName

                                       

                                      Here we go:

                                       

                                      // The present code can be used as a startup script.
                                      
                                      #targetengine 'UpdateJobNumber'
                                      
                                      const updateJobNumber = function(/*Event*/ev,  doc,m,t)
                                      // -------------------------------------
                                      // BEFORE_SAVE_AS event handler.
                                      // Reset the `JobNumber` text variable to the 9 first characters of the
                                      // doc file name iff they are formed of uppercase letters and/or digits.
                                      // ---
                                      // Tested conditions:
                                      // 1. The event type MUST be 'beforeSaveAs' (prevent wrong listener.)
                                      // 2. The event MUST provide a valid `fullName` prop (File),
                                      //    since one can't rely on document.fullName yet!
                                      // 3. The save-as File name MUST match /^[A-Z\d]{9}/.
                                      // 4. The target MUST be a Document instance (who knows!)
                                      // 5. It MUST have a `JobNumber` TextVariable...
                                      // 6. ...of the kind 'Custom text.'
                                      // 7. No need to rewrite the variable if already set as expected.
                                      {
                                          ( 'beforeSaveAs' == ev.eventType )
                                          && ( m=ev.properties.fullName )
                                          && ( m=m.name.match(/^[A-Z\d]{9}/) )
                                          && ( (doc=ev.target) instanceof Document )
                                          && ( (t=doc.textVariables.itemByName('JobNumber')).isValid )
                                          && ( (t=t.variableOptions) instanceof CustomTextVariablePreference )
                                          && ( m[0] != t.contents )
                                          && ( t.contents=m[0] );
                                      };
                                      
                                      app.addEventListener('beforeSaveAs', updateJobNumber);
                                      

                                       

                                      Hope that helps.

                                       

                                      @+

                                      Marc

                                      2 people found this helpful
                                      • 16. Re: Job Number Variable Script...
                                        attil Level 1

                                        WOW! This is awesome. Thank you to both of you for all your help! Just one more question. Is there a way to have the script run as soon as you open an existing document without having to use the "save as" command? Most of my jobs are already created and just need updated by a regular "save" command.