3 Replies Latest reply on Jan 13, 2016 7:48 AM by Ronald63

    Calculating a figure in a field based on the date the document is being opened

    grahamhbg

      I need to work on a large number of documents that contain biographies of staff members. On an annual basis their biogs need updated to reflect an extra year of experience in their department, so every January all the biogs need updated to +1 year of experience.

       

      Is there a way to have this number set by inputting a start date (in a field, table or text box) and then have the number of years experience calculated and displayed based on the date the document is being opened.

       

      i.e. Joe Bloggs started in 2009 so when I open the InDesign file today it shows 7 years experience. If I was to reopen the document in 2017 this figure would update automatically to show 8 years of experience.

       

      Thanks!

        • 1. Re: Calculating a figure in a field based on the date the document is being opened
          Ronald63 Level 4

          Hi,

           

          Try this way ... (put this script in the folder 'Startup Scripts')

           

          #targetengine "session"
          
          app.addEventListener('afterOpen', function(myEvent) {
                  if (myEvent.target.constructor.name == 'Document') {
                      var doc = myEvent.target;
                      /* get started date */
                      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 
                      app.findGrepPreferences.findWhat = '(?<=started in )\\d{4}'; // search a string like started in 2010
                      //app.findGrepPreferences.appliedParagraphStyle= findparastyle; /* you can add paragraphe style as search criteria */
                      //app.findGrepPreferences.appliedCharacterStyle= findcharstyle; /* you can add character style as search criteria */
                      var mFind  = doc.findGrep();
                      var mStartYear = parseInt (mFind[0].contents);
                      /* calculate experience */
                      var d = new Date();
                      var n = d.getFullYear();
                      var mExperienceYear = (n - mStartYear).toString();
                      /* set experience */
                      app.findGrepPreferences.findWhat = '\\d{1,2}(?= years of experience)'; // search a string like 3 years of experience
                      //app.findGrepPreferences.appliedParagraphStyle= findparastyle; /* you can add paragraphe style as search criteria */
                      //app.findGrepPreferences.appliedCharacterStyle= findcharstyle; /* you can add character style as search criteria */
                      app.changeGrepPreferences.changeTo = mExperienceYear;
                      doc.changeGrep();            
                  }
              }
          )
          
          

           

          Restart InDesign and open a document with a text frame contents "started in 2001 - 2 years of experience"

           

          Regards

          • 2. Re: Calculating a figure in a field based on the date the document is being opened
            grahamhbg Level 1

            Hi Ronald63

             

            Thank you for taking the time to reply, it's much appreciated.

             

            I tested this and it works great. Is there a way to have:

             

            • The start year 'hidden' somewhere in the document (as I don't want this to be viewable) - on the canvas? I'm unsure how you would connect the two pieces of information then.

            • Have multiple people on the same page and the calculation worked out for them all at the same time

            • Have two calculations carried out - one for the person's overall working experience and one for the amount of time they have been with the company?

             

            Joe Bloggs

            X years' experience

            Y years with Acme Inc.

             

            John Doe

            X years' experience

            Y years with Acme Inc.

             

            AN Other

            X years' experience

            Y years with Acme Inc.

             

            Would I need to tag the X years with a particular style and the Y years with another so they could be differentiated?

             

            Thanks in advance.

            • 3. Re: Calculating a figure in a field based on the date the document is being opened
              Ronald63 Level 4

              Hi,

               

              Try this  with ...

               

              http://we.tl/bUXB83Ve9s

               

              #targetengine "session"
              
              app.addEventListener('afterOpen', function(myEvent) {
                      if (myEvent.target.constructor.name == 'Document') {
                          var doc = myEvent.target;
                          var d = new Date();
                          var n = d.getFullYear();
                          //==years’ experience==\\
                          var mParaStyle = doc.paragraphStyles.itemByName('experience_work');
                          /* get started date */
                          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
                          app.findGrepPreferences.findWhat = '(?<=years’ experience\\s)\\d{4}';
                          app.findGrepPreferences.appliedParagraphStyle= mParaStyle; /* you can add paragraphe style as search criteria */
                          var mYear  = doc.findGrep();
              
                          for (var i = 0; i <mYear.length; i++) {
                              var mStartYear= parseInt (mYear[i].contents);
                              var mExperienceYear = (n - mStartYear).toString();
                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
                              app.findGrepPreferences.findWhat = '\\d{1,2}(?=\\syears’ experience)';
                              var mExperience  = doc.findGrep();
                              mExperience[i].contents = mExperienceYear;
                          }
                          //==years with company==\\
                          var mParaStyle = doc.paragraphStyles.itemByName('experience_company');
                          /* get started date */
                          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
                          app.findGrepPreferences.findWhat = '(?<=years with Acme Inc.\\s)\\d{4}';
                          app.findGrepPreferences.appliedParagraphStyle= mParaStyle; /* you can add paragraphe style as search criteria */
                          var mYear  = doc.findGrep();
              
                          for (var i = 0; i <mYear.length; i++) {
                              var mStartYear= parseInt (mYear[i].contents);
                              var mExperienceYear = (n - mStartYear).toString();
                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
                              app.findGrepPreferences.findWhat = '\\d{1,2}(?=\\syears with Acme Inc.)';
                              var mExperience  = doc.findGrep();
                              mExperience[i].contents = mExperienceYear;
                          }
                      }
                  }
              )