9 Replies Latest reply on Oct 16, 2009 7:21 AM by Jeremy bowmangraphics-DQuh1B

    Insert Autodate


      I need help with the following:

      I need a script that generates todays date +5 days in a textbox.

      For example; if todays date is 2009-10-15 i want it to show 2009-10-20.

      Is this possible? Can anyone help me or give me some kind of clue of how to do this?


      Thanks in advance!

        • 1. Re: Insert Autodate
          Thomas B. Nielsen Level 3

          This JS will add todays date (+5 days) to any textframes with the label date:


          var myDocument = app.activeDocument;
          var myItem = myDocument.pageItems.item('date');
          try {
          var toDay = new Date();
          myItem.contents = "" + toDay.getFullYear() + " - " + (toDay.getMonth()+1) + " - " + (toDay.getDate()+5);
          } catch (e) {}


          Not very pretty but it works. Ofc you could add an on event handler, to do the update on save for instance.




          Thomas B. Nielsen


          • 2. Re: Insert Autodate
            pyk Level 1

            Thank you very much!! works like a charm so far anyway

            • 3. Re: Insert Autodate
              pyk Level 1

              although, a day later when i open the indd.document i discover that the date does not automatically update.

              I must manually double click the script for that to happen and i don´t want that.

              Any help and solution or hint would be great!


              • 4. Re: Insert Autodate
                [Jongware] Most Valuable Participant

                Thomas said so in his post. The script types the date, and InDesign is not Word -- it doesn't "do" stuff to text you type.

                • 5. Re: Insert Autodate
                  Jeremy bowmangraphics-DQuh1B Level 3

                  You might try using a text variable such as "Modification date", which will update when you modify the document, or "Output date" perhaps.

                  • 6. Re: Insert Autodate
                    pyk Level 1


                    Since i´m not a "code-guy", how would i insert that into the code posted earlier?

                    • 7. Re: Insert Autodate
                      Jeremy bowmangraphics-DQuh1B Level 3

                      This came up a few days ago on this forum in a thread called "Insert text variable".


                      I'm a beginner, but I think the following line of code will put a "Creation Date" text variable at the insertion point of myTextFrame (change the words 'Creation Date' to 'Modification Date' or 'Output Date' as required):


                      myTextFrame.insertionPoints.item(-1).textVariableInstances.add({associatedTextVariable:myD ocument.textVariables.item("Creation Date")});

                      (Make sure that's all on one line with no spaces except between the words 'Creation' and 'Date'-- the forum software tends to add line breaks.)

                      • 8. Re: Insert Autodate
                        Jeremy bowmangraphics-DQuh1B Level 3

                        While we're on the subject, can anyone explain to me why the following code puts '16/10/2009' (i.e. the European format of today's date) into a text frame in CS4 and CS3 on my Mac, but '10/16/2009' (i.e. the American format of today's date) in CS3 on my client's PC running Windows XP?


                        Doesn't the script explicitly say which order to write the day and month in?


                        (If we switch the occurrences of 'myDay' and 'myMonth' in the last line, it writes them in the European order again!)


                        var myDocument = app.documents.item(0);

                        myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;

                        myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;

                        var myPage = app.activeWindow.activePage;

                        var myTextFrame = myPage.textFrames.add({geometricBounds:[100, 100, 120, 200]});

                        var myDate = new Date();

                        var myDay=myDate.getDate();

                        var myMonth=myDate.getMonth() + 1;

                        var myYear=myDate.getFullYear();

                        myTextFrame.insertionPoints.item(-1).contents = myDay + "/" + myMonth + "/" + myYear;

                        • 9. Re: Insert Autodate
                          Jeremy bowmangraphics-DQuh1B Level 3

                          Scrap that last request! It's "not doing it any more" (ahem).