0

Is there any way to create a text variable that has a date in the future calculated from today's date?

What I need is: Todays date + 1 month.

I know you can have text variables with the output or modification date but I haven't been able to find a way of doing "math" on a variable.

2 Answers 2

2

Here is a full script. Its on german but you can change the names of the months in the script.

 #targetengine 'usernameVariable'

/***************************************************************

***************************************************************/

//This function adds static text variables in InDesign
//They won't be dynamically updated though.
//That's why we need the updating function below
function addVariables(openEvent){

    var doc = openEvent.parent;

    while ( doc.constructor.name != "Document" )
    {
        if ( doc.constructor.name == "Application" ){ return; }

       doc = doc.parent;
    }


    //Adding XMP serverURL to text variables
    createTextVariable(doc, "Next Month", getNextMonth(), true );


}

function getNextMonth() {
    var date = new Date();
    var month = date.getMonth();
    var Months = ["Januar",
    "Ferbruar",
    "März",
    "April",
    "Mai",
    "Juni",
    "Juli",
    "August",
    "September",
    "Oktober",
    "November",
    "Dezember"];

    return Months[month+1];
}

//Generic function to add static custom text variables
function createTextVariable(target, variableName, variableContents, bRewrite){

    var usernameVariable = target.textVariables.itemByName(variableName);
    if(!usernameVariable.isValid){ 
        usernameVariable = target.textVariables.add();
        usernameVariable.variableType = VariableTypes.CUSTOM_TEXT_TYPE;
        usernameVariable.name = variableName;
        usernameVariable.variableOptions.contents = variableContents;
    }
    bRewrite!==false && usernameVariable.variableOptions.contents = variableContents;
}



//Snippet for grabbing the  deep name of the computer
function getComputerName(){

    var APLScript = "get computer name of (system info)";
    var VBScript = "Dim wshShell\
    Set wshShell = CreateObject( \"WScript.Shell\" )\
    strComputerName = wshShell.ExpandEnvironmentStrings( \"%COMPUTERNAME%\" )\
    app.scriptArgs.SetValue \"computerName\", strComputerName";

    var scpt = (File.fs=="Windows")? VBScript : APLScript;
    var language = (File.fs=="Windows")? ScriptLanguage.visualBasic : ScriptLanguage.APPLESCRIPT_LANGUAGE;

    var scriptResult = app.doScript(scpt, language);

    var computerName = (File.fs=="Windows")? app.scriptArgs.getValue("computerName") : scriptResult;

    return computerName;
}

//Add listeners to update file when opened.
app.addEventListener('afterOpen', addVariables);
app.addEventListener('beforeSave', addVariables);
app.addEventListener('afterSelectionChanged', addVariables);
app.addEventListener('afterNew', addVariables);
1

Changing the - to a + in line 5 (setMonth(getMonth()-1)) of this script should do what you need.

1
  • The link died, any idea of what it was?
    – Luciano
    Oct 30, 2019 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.