23 Replies Latest reply on Sep 28, 2006 2:24 AM by (Theresa_Verburg)

    Insert auto date

    PeterTucker
      Hi

      First, sorry if you've also read the InDesign Forum, I've cross posted this enquiry there as well.

      I need to insert a date/time when the IDCS2 document is printed, i've looked on the web and various other resources like this Forum.

      Peter

      Set to not expire because of active cross references elsewhere
        • 1. Re: Insert auto date
          Level 1
          Inserting and later updating a date is easy.

          Triggering it to a specific event is less easy. RogueSheep has a product that allows you to tie a script to an event. So, if you wrote a script to replace all instances of a character style named (say) CurrentDate with the current date and tied this to happen before a Print command was actually executed. That would do it.

          A JavaScript to do this would look something like this:
          //DESCRIPTION: Update Date in Active Document
          

          /*
            Updates all instances of current date in a document to reflect the
            actual date when the script is run. Depends on character style named
            "CurrentDate" -- does nothing if document lacks said style.
          */

          if (app.documents.length == 0) { exit() }
          myDoc = app.activeDocument; //Global
          var myStyle = myDoc.characterStyles.item("CurrentDate");
          if (myStyle == null) { exit () }

          // If we get here, we have work to do
          var myDate = getDateString();

          // Use Find/Change across document to update date:
          app.findPreferences = app.changePreferences = null;
          myDoc.search("", false, false, myDate, {appliedCharacterStyle:myStyle});

          function getDateString() {
            var today = new Date();
            var myDateString = today.toLocaleDateString();
            myParts = myDateString.split(" 0");
            if (myParts.length != 1) {
            myDateString = myParts[0] + " " + myParts[1];
            }
            return myDateString.slice(0,-5) + "," + myDateString.slice(-5);
          }
          To use the script, copy and paste to a text editor and save as a plain text file with an appropriate name in the form UpdateDate.jsx in the Scripts folder of the Presets folder of your InDesign CS2 folder (you could put it in a subfolder if you wish). Then to run the script, double-click its name in the Scripts palette.

          If you want to work it with RogueSheep's InEventScript, follow their directions on how to connect this to the Print command. If you want the date in a different format, change the function to provide the format -- this might require some research.

          Dave
          • 2. Re: Insert auto date
            PeterTucker Level 1
            Dave you're a star - no a big star - many thanks it works just fine.

            This has given me a head start on the date and gives food for thought about other things that can be done by automation.

            Cheers

            Peter
            • 3. Re: Insert auto date
              Level 1
              Hi Peter--

              While Dave's suggestion is great, why not turn on the Page Information option in the Marks and Bleed section of the Print dialog box? That will print the date below the bottom of page. If your page is the same size as the paper you're printing on, you can modify the location of the page information by creating a custom printer marks definition file (.mrk).

              Thanks,

              Ole
              • 4. Re: Insert auto date
                Olav,

                Is there a resource or link that shows how to create this (.mrk) file?
                • 5. Re: Insert auto date
                  PeterTucker Level 1
                  Hi

                  If you scroll up the page and find a heading: Messages then click show all you'll find the full descriptive message.

                  Peter
                  • 6. Re: Insert auto date
                    Level 1
                    Hi herrmueller--

                    The only place it's documented, as far as I know, is in my book "Real World Adobe InDesign CS" (or the CS2 version, which should be available any day now). I can send you an example file to play with, if you like (once I get back to the machine that has the file on it).

                    Thanks,

                    Ole
                    • 7. Re: Insert auto date
                      Level 1
                      Olav,

                      I would appreciate it if you could send me the file.
                      I will also purchase the book, if you would provide the link to it.

                      Thanks,
                      herrmueller
                      • 8. Re: Insert auto date
                        chadgb
                        Back to Dave's javascript (which works great by the way and is a clever solution to Adobe's lack of support for a special character for dates)

                        If I wanted my date to be formatted as only month, year...like "June, 2006" how would the last part of the script be modified?

                        Thanks
                        Chad
                        • 9. Re: Insert auto date
                          Level 1
                          Chad,

                          Date is a JavaScript object type, so you could either tackle this by picking up just the information you want using the appropriate method, see:

                          http://www.devguru.com/technologies/javascript/10585.asp

                          or you could process the string my script creates to eliminate the characters you don't want.

                          Dave
                          • 10. Re: Insert auto date
                            chadgb Level 1
                            Dave,

                            That was fast!

                            Since I don't know javascript and know only a fraction about applescript and scripting in InDesign, your second option sounds easier, but again... I am no programmer, more of a designer.

                            This is the result your script generated: Monday, June 12, 2006

                            So I would just need to elminate the Monday, 12?

                            Is that something simple to add? Could you float me a freebie add-on or just modify the java to produce the correct result?

                            Thanks again,
                            Chad
                            • 11. Re: Insert auto date
                              Level 1
                              Chad,

                              You have inspired me to add this script (and the variant you want) to my downloads area.

                              They're set up for use with InDesign CS2. If you need from for CS, you'll need to change the file extension names to ".js" instead of ".jsx".

                              I've gone off posting the text of scripts in the forum (except for small examples) because I've run into so many people who seem to be completely flumoxed by the installation instructions (particulary the part about plain text file). So, after you unzip the package, subject to the CS caveat above, the two scripts are ready to go. Just move or copy them to the Scripts folder.

                              I've added a copyright notice and included a link to my donations site to give people who wish to encourage my posting of scripts a vehicle for doing so. At some point soon, I'll update this package with a proper readMe that will include installation instructions and a caveat warning any users that they take any and all risks associated with using any of these scripts.

                              I also intend to add a catalog to the readMe to make it easier for people to find other scripts I've posted that could be of help.

                              The package is ready to download from:

                              http://pdsassoc.com/downloads/DateScripts.zip

                              Dave
                              • 12. Re: Insert auto date
                                chadgb Level 1
                                Dave,

                                You're a miracle worker!

                                That did it. I wish I knew Javascript that way in the future if I wanted to create custom date formats, I could edit the last part of the script. I looked at the code and none of it makes sense like applescript.

                                I thought, what if I wanted to take the comma out and just have: june 2006 (date month)

                                return myParts[1] + "," + myParts[3];

                                I thought, all I have to do is remove the comma between the two parts and of course that didn't work. I guess I just have to rely on brilliant guys like yourself.

                                Thanks again for all your help. I'll check out the donations page.
                                Chad
                                • 13. Re: Insert auto date
                                  Level 1
                                  If you don't want the comma, then:

                                  return myParts[1] + " " + myParts[3];

                                  should do the job.

                                  Dave
                                  • 14. Re: Insert auto date
                                    chadgb Level 1
                                    Dave,

                                    When I made the change I get a javascript error from InDesign,

                                    Error number: 5
                                    Error String: "Unterminated comment"
                                    Line: 4

                                    I am using InDesign CS and not CS2, I hope that doesn't make a differnence.

                                    Thanks again for all your work. I'll definetely keep you in mind for future enterprise wide scripting.

                                    Chad
                                    • 15. Re: Insert auto date
                                      chadgb Level 1
                                      Dave,

                                      Actually when I run the script the first time with newly character styled text it runs fine but still adds the comma, then when I click on the script again, I get the javascript error.

                                      ?

                                      Thanks
                                      chad
                                      • 16. Re: Insert auto date
                                        Level 1
                                        Please post the text of the script.

                                        Dave
                                        • 17. Re: Insert auto date
                                          chadgb Level 1
                                          Ôªø//DESCRIPTION: Update Date in Active Document

                                          /*
                                          ¬(c)Copyright 2006 PDS Associates

                                          This script must not be distributed without first obtaining written permission of:

                                          Dave Saunders, proprietor
                                          PDS Associates
                                          P.O. Box 127
                                          Allenhurst, NJ 07711-0127

                                          e-mail: davesaunders@pdsassoc.com

                                          donations gratefully received: http://order.kagi.com/?6CYQW&lang=en
                                          */

                                          if (app.documents.length == 0) { exit() }
                                          myDoc = app.activeDocument; //Global
                                          var myStyle = myDoc.characterStyles.item("CurrentDate");
                                          if (myStyle == null) { exit () }

                                          // If we get here, we have work to do
                                          var myDate = getDateString();

                                          // Use Find/Change across document to update date:
                                          app.findPreferences = app.changePreferences = null;
                                          myDoc.search("", false, false, myDate, {appliedCharacterStyle:myStyle});

                                          function getDateString() {
                                          var today = new Date();
                                          var myDateString = today.toLocaleDateString();
                                          myParts = myDateString.split(" ");
                                          return myParts[1] + " " + myParts[3];
                                          }
                                          • 18. Re: Insert auto date
                                            Level 1
                                            What is the crud immediately before the first characters of the script? That would certainly cause an error. Is this saved as Plain Text? I tried to get around that problem by distributing it already in the same form but then you edited it.

                                            Dave
                                            • 19. Re: Insert auto date
                                              chadgb Level 1
                                              Yes, that did it. I deleted the crud, resaved it, and now it works. I don't know why it was adding those extra characters. I didn't add them. I opened your script directly in MS WORD and then resaved it as a text only file.

                                              I should have used Apple TextEdit, but didn't know until know that you can change the default format to be plain text and not RTF. The crud doesn't show up in TextEdit. I should have known I would be in trouble if I used a microsoft product (no offense if you use them)

                                              Thanks again,
                                              Chad
                                              • 20. Re: Insert auto date
                                                Level 1
                                                Chad,

                                                Here is some AppleScript examples:

                                                Short date aka: 20.6.2006

                                                set theDateString to (short date string of (current date))
                                                
                                                tell application "Adobe InDesign CS2"
                                                tell document 1
                                                  set contents of selection to theDateString
                                                end tell
                                                end tell


                                                Regular date aka: tiistai 20. kesäkuu 2006 (propably in some other form in US computers)

                                                set theDateString to (date string of (current date))
                                                
                                                tell application "Adobe InDesign CS2"
                                                tell document 1
                                                  set contents of selection to theDateString
                                                end tell
                                                end tell


                                                You can also insert some other day than today. Here is example of seventh day from today aka: 27.6.2006

                                                property dueDate : 7 * days
                                                

                                                set theDateString to (short date string of ((current date) + dueDate))
                                                tell application "Adobe InDesign CS2"
                                                tell document 1
                                                  set contents of selection to theDateString
                                                end tell
                                                end tell


                                                Shane probably can tell you even more fancier examples.

                                                Kari
                                                • 21. Re: Insert auto date
                                                  Thanks for the great advice everyone.

                                                  I'm hoping that one of you may know the answer to this question.
                                                  I've been following the instructions for adding the date, but how do I format the text into a specific format like "08/06" for August 2006?

                                                  I keep getting error messages when I try to run the script. It's usually something along the lines of: "It was looking for a string and found a date instead..."

                                                  Thanks for your time!

                                                  TJV
                                                  • 22. Re: Insert auto date
                                                    Level 1
                                                    Theresa,

                                                    Sorry that I missed your message when you posted it. I had come back here to make an announcement about a change in the scripts posted at:

                                                    http://pdsassoc.com/downloads/DateScripts.zip

                                                    and so I belatedly found your message. The changes I've made were done because David Blatner and Anne-Marie Concepcion spoke at some length about the date insertion scripts covered in this topic.

                                                    I'd almost forgotten about them, to tell the truth, but the podcast caused me to revisit them. They come much closer to doing what you asked for, and I've included instructions on making the kind of change you requested.

                                                    There are still two scripts, but they work quite differently from before:

                                                    DateTimeSetup.jsx allows you to create two DateTimeFormat/Character style pairs (by means of a dialog) that allows you to have up to two date/time stamping formats in any of your documents.

                                                    Ive built-in 14 different formats and provided instructions on how to add more.

                                                    DateTimeInsert.jsx uses the settings for your document to insert the current date/time in place of any text that uses the character styles you assigned.

                                                    So, this second script is exactly as David and Anne-Marie described in the podcast, with the addition that it can now work with any of the 14 built-in formats and you can have two active at once in your document.

                                                    Ive written an eight-page user guide which explains how to install the scripts, how to operate them, and how to customize them.

                                                    To read more about the podcast or even to listen to it, follow this link:

                                                    http://indesignsecrets.com/indesignsecrets-podcast-031.php

                                                    Dave
                                                    • 23. Re: Insert auto date
                                                      Level 1
                                                      Well Dave, as usual, you come through with flying colors. I did manage to create something that would work for the time being, but your scripts are far more efficient than what I came up with.

                                                      Thank you so much. I certainly do appreciate all you and the other people on this forum do to help each other.

                                                      Thanks!

                                                      Theresa