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);
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({
...
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;
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!
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.
Copy link to clipboard
Copied
You are the best. If I knew you personally I would buy you a nice cold drink. Thanks so much. I really appreciate it.