• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Lot and expiry date script

Community Beginner ,
Jun 20, 2017 Jun 20, 2017

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

1.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 02, 2017 Aug 02, 2017

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.activeDocum

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 20, 2017 Jun 20, 2017

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 20, 2017 Jun 20, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 20, 2017 Jun 20, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 20, 2017 Jun 20, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 02, 2017 Aug 02, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2017 Aug 02, 2017

Copy link to clipboard

Copied

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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 02, 2017 Aug 02, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2017 Aug 02, 2017

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 07, 2017 Aug 07, 2017

Copy link to clipboard

Copied

Brilliant, thats exactly what i need it to do

Thanks for your help !

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 16, 2017 Oct 16, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 17, 2017 Oct 17, 2017

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

Hi William, Sorry to bother you with this again but a colleague just noticed something i had overlooked and explained badly above

the Expiry date = PLUS 5 years - MINUS 1 month

ie. if the LOT number today was 240118

the expiry would be  2022-12

if the LOT number was 240218

the expiry would be 2023-01

I have attempted to do this below based on your script but have been unsuccessful


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;

     var em = date.getMonth() +1;
      if(em<10){em = "0" + em};
      em -=1;

        result.lot = "" + d + m + y; 
        result.exp = fy + "-" + em
 
        return result; 
    } 
 
    var docRef = app.activeDocument; 
    var info = getDate(); 
 
    var frame = docRef.textFrames[0]; 
    frame.contents = info.lot + "\n" + info.exp; 

container();

Any help would be much appreciated

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

LATEST

Morning, Gareth.

Not a bother at all. that was another silly oversight on my part. I guess i didn't fully understand what was needed when i first wrote this. My apologies.

try this one on for size. I fixed it so that if the month is january, month will be set to "12" and the year will be decremented.

To help catch any other potential issues, I also included some simulation "tools". Using these, you can simulate any date you want and verify that you receive the expected results. Let me know if you have questions about that, or anything else.

function container()

{

    function getDate()

    {

        var result = {};

        var date = new Date();

      

        //uncomment this section to experiment with different dates

        //to ensure the correct lot and expiry date will be

        //generated in different scenarios.

            //simulate the month. remember, this is zero

            //indexed, so 2 means March and 11 means december

            // date.setMonth(2);

            //simulate the day of the month.

            //see zero based index msg above

            // date.setDate(15);

            //simulate the year

            //this one is NOT zero based. just put in

            //the year you want to simulate

            // date.setFullYear(2018);

        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;

        //set the lot number

        result.lot = "" + d + m + y;

  

        //subtract 1 from the month

        //if "m" == 1, make it 12 and decrement year

        if(m==="01")

        {

            m = 12;

            fy -=1;

        }

        else

        {

            m = "0" + date.getMonth();

        }

        //set the expiry date

        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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines