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

Expiration Date Script or Custom Text Variable

New Here ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Is there a way to create a Custom Text Variable that would give you a date in the future. Perhaps the end of the next month. I've written the javascript but I'm not sure how to get it into InDesign. I'm more of a designer than a programmer so forgive me if I've placed this in the wrong area. 

Is there a way I can get a small script like this to work in either the custom text area or as a regular script? 

var today = new Date();
var mm = today.getMonth()+2;
var yyyy = today.getFullYear();
var lastday = function(y,m){
return  new Date(y, m, 0).getDate();
}
today = mm+'/'+lastday(yyyy,mm)+'/'+yyyy;

console.log(today);

 

 

TOPICS
Scripting

Views

535

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 , Jun 21, 2021 Jun 21, 2021

Here you go. It will make a "expire" custom text variable if none exists in the document with your date. If the variable exists, it will update the date. 

var today = new Date();
var mm = today.getMonth()+2;
var yyyy = today.getFullYear();
var lastday = function(y,m){
   return  new Date(y, m, 0).getDate();
}
today = mm+'/'+lastday(yyyy,mm)+'/'+yyyy;

var expDateVar = app.activeDocument.textVariables.itemByName("expire");
if (!expDateVar.isValid) {
    app.activeDocument.textVariables.add({
    
...

Votes

Translate

Translate
Community Expert ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Here you go. It will make a "expire" custom text variable if none exists in the document with your date. If the variable exists, it will update the date. 

var today = new Date();
var mm = today.getMonth()+2;
var yyyy = today.getFullYear();
var lastday = function(y,m){
   return  new Date(y, m, 0).getDate();
}
today = mm+'/'+lastday(yyyy,mm)+'/'+yyyy;

var expDateVar = app.activeDocument.textVariables.itemByName("expire");
if (!expDateVar.isValid) {
    app.activeDocument.textVariables.add({
        name: "expire",
        variableType: VariableTypes.CUSTOM_TEXT_TYPE,
    });
}
expDateVar.variableOptions.contents = today;

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
New Here ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

You are the best. Thank you so much. Forgive my ignorance. I'm new to all of this. How do I go about getting this script into the custom text variable area. Do I need to save it as a particular file format? Or will it work more as a script? From the scripts panel. Again forgive all the questions. You rock!

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 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

You're welcome. You save what I wrote as a plain text with .jsx extension into your Scripts Panel. More info here: https://creativepro.com/how-to-install-a-script-in-indesign-that-you-found-in-a-forum-or-blog-post/

Then you run the script whenever you want to update the date in your active (open) document.

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
New Here ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

LATEST

You are the best. If I knew you personally I would buy you a nice cold drink. Thanks so much. I really appreciate it. 

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