11 Replies Latest reply on Oct 17, 2017 6:01 AM by williamadowling

    Lot and expiry date script

    garethw17

      Hi I was wondering if anyone could help me, I have zero experience with writing / using scripts but I need one for our printed documentation in work, i have saw similar requests on the forum but as ive said i have no experience so could not change these to suit our purpose.

       

      The format is below, the document will be printed and shipped with the product, I need the LOT and EXPIRY date to update when the file is opened to be printed

       

      LOT number = today's date (Format - ddmmyy)

       

      Expiry number = Lot number + 5 years – 1 month (Format yyyy-mm)

       

      I.e.

      LOT – 150617

      EXP – 2022-05

       

      CE Insert script.jpg

       

      If anyone could help me out with this it would be much appreciated

        • 1. Re: Lot and expiry date script
          williamadowling Level 4

          Ok, first things first. Welcome to the world of scripting illustrator.. You'll laugh. You'll cry.. you'll learn a lot, or perhaps you'll run for the hills. Either way, we're here to help.

           

          Right off the bat, I'm going to break your heart. There's really no easy way to have a script run when a specific file is opened. Illustrator doesn't have event listeners for that kind of thing. So Anything we come up with here will need to be executed manually (or as part of a batch script that explicitly opens, updates, saves and closes files).

           

          Anywho, i'm a little bit confused about what you're asking for, particularly this line:

          Expiry number = Lot number + 5 years – 1 month (Format yyyy-mm)

          I think i know what you mean, but my interpretation and your example don't match this description, so for now, i'll assume i know what you're talking about and you can let me know if i misunderstood. Here's a snippet that gets the date info you're looking for and formats it in the way you showed in your examples.

           

          function container()
          {
            function getDate()
            {
                 var result = {};
                 var date = new Date();
          
                 var m = date.getMonth() +1;
                 if(m<10){m = "0" + m};
          
                 var d = date.getDate();
                 if(d<10){d = "0" + d};
          
                 var y = date.getFullYear().toString();
                 y = y.substring(2,4);
          
                 var fy = date.getFullYear();
                 fy += 5;
          
                 result.lot = m + d + y;
                 result.exp = fy + "-" + m;
          
                 return result;
            }
          
            var blah = getDate();
          
            $.writeln(blah.lot);
            $.writeln(blah.exp);
          }
          container();
          
          • 2. Re: Lot and expiry date script
            Silly-V Adobe Community Professional

            William, you should come to the Creative Developer Summit next year!

            • 3. Re: Lot and expiry date script
              Silly-V Adobe Community Professional

              A CEP extension could be created to run a script when a file open event happens, but I have yet to test.

              • 4. Re: Lot and expiry date script
                Ten A Adobe Community Professional

                CEP dosen't have document open event. However, We can use "documentAfterActivate" event.

                It is triggered when "create a new document", "Open a new document" and "Switch to an open document". So, we need to solve ourselves what oparation handled.

                1 person found this helpful
                • 5. Re: Lot and expiry date script
                  garethw17 Level 1

                  Sorry for the delayed response, i have been sidelined with different projects

                   

                  i cant seem to get the script to run to update the textbox?

                   

                  its probably something simple that im doing wrong

                  • 6. Re: Lot and expiry date script
                    williamadowling Level 4

                    No worries, Gareth.

                     

                    So, here's the deal, the script i posted above was an illustration of how to get the date, format it and display it to the console. As far as changing the contents of specific text frames, I need to know more information about the files you will be manipulating.

                     

                    Text frames are targeted either by name, or by index. The index can vary depending on scope. for example, if your target text frame was the only text frame on a layer called "My Layer", then the text frame could be accessed like this:

                    app.activeDocument.layers["My Layer"].textFrames[0];

                    but if you were looking in the document scope, the index number could be quite different, depending upon how many other text frames are in the document, like so:

                    app.activeDocument.textFrames[45];

                     

                    An easy way to get an answer to your question is to select the text frames you want to update, then take a screenshot of your layers panel such that it shows the entire heirarchy from top level layer, down to the text frame you want to change. That way I can help you build the path to the item so that we can change it via the script. So consider your text frame was nested two layers deep inside of a layer called "My Layer". the hierarchy may look something like this:

                     

                    My Layer (layer)

                         My Sub Layer (layer)

                              My Text Frames (layer)

                                   My Text Frame 1 (textFrame)

                                  Expiry (textFrame)

                                  Lot (textFrame)

                                   My Text Frame 3 (textFrame)

                              My Shapes (layer)

                                   My Shape 1 (pathItem)

                                   My Shape 2 (pathItem)

                     

                    so in this example, the syntax to change the contents of "Expiry" and "Lot" is as follows:

                     

                    app.activeDocument.layers["My Layer"].layers["My Sub Layer"].layers["My Text Frames"].textFrames["Expiry"].contents = blah.exp;

                    app.activeDocument.layers["My Layer"].layers["My Sub Layer"].layers["My Text Frames"].textFrames["Lot"].contents = blah.lot;

                    • 7. Re: Lot and expiry date script
                      garethw17 Level 1

                      Hi William,

                       

                      the document only contains a single text frame, which I have simply typed into at the moment to illustrate the date formats shown above. It will be located within the grey box shown in my original post

                       

                      Illustrator layers.png

                       

                      Thanks

                      • 8. Re: Lot and expiry date script
                        williamadowling Level 4

                        ok give this a shot..

                         

                        function container()
                        {
                            function getDate()
                            {
                                var result = {};
                                var date = new Date();
                        
                                var m = date.getMonth() +1;
                                if(m<10){m = "0" + m};
                        
                                var d = date.getDate();
                                if(d<10){d = "0" + d};
                        
                                var y = date.getFullYear().toString();
                                y = y.substring(2,4);
                        
                                var fy = date.getFullYear();
                                fy += 5;
                        
                                result.lot = m + d + y;
                                result.exp = fy + "-" + m;
                        
                                return result;
                            }
                        
                            var docRef = app.activeDocument;
                            var info = getDate();
                        
                            var frame = docRef.textFrames[0];
                            frame.contents = info.lot + "\n" + info.exp;
                        }
                        container();
                        
                        • 9. Re: Lot and expiry date script
                          garethw17 Level 1

                          Brilliant, thats exactly what i need it to do

                           

                          Thanks for your help !

                          • 10. Re: Lot and expiry date script
                            garethw17 Level 1

                            Hi William

                             

                            im having some problems with the script, its been working perfectly until this month

                             

                            and today for example (16-10-2017) the LOT number is being returned as 2617

                             

                            i was wondering if you had any idea what it could be

                             

                            Thanks again

                            • 11. Re: Lot and expiry date script
                              williamadowling Level 4

                              My apologies for that oversight. Turns out when i was adding the leading '0' for months less than 10, it was creating a string, but since it is now october, and that zero was not being concatenated, the result was a number, so then when i determined the lot number, it was doing a mathematical operation on the numbers rather than a concatenation. This should fix it up.

                               

                               

                              function container()  
                              {  
                                  function getDate()  
                                  {  
                                      var result = {};  
                                      var date = new Date();  
                                
                                      var m = date.getMonth() +1;  
                                      if(m<10){m = "0" + m};  
                                
                                      var d = date.getDate();  
                                      if(d<10){d = "0" + d};  
                                
                                      var y = date.getFullYear().toString();  
                                      y = y.substring(2,4);  
                                
                                      var fy = date.getFullYear();  
                                      fy += 5;  
                                
                                      result.lot = "" + m + d + y;  
                                      result.exp = fy + "-" + m;  
                                
                                      return result;  
                                  }  
                                
                                  var docRef = app.activeDocument;  
                                  var info = getDate();  
                                
                                  var frame = docRef.textFrames[0];  
                                  frame.contents = info.lot + "\n" + info.exp;  
                              }  
                              container();