Highlighted

A script I am running to create a custom text variable stops running intermittently, please help

New Here ,
Mar 16, 2020

Copy link to clipboard

Copied

I am running a javascript (inserted below) to create custom text variables to create a 'next month' and 'next issue' variable for a magazine I work on.

 

For reasons unknown to me, it stops running correctly intermittently and when I open a template with the text variables on it reverts to showing the current month, not month + 1.

 

If i run the script before each template or document I open it seems to work okay, but this almost makes it pointless to have the script and leaves room for errors to be missed.

 

If anyone knows what could be going wrong, I'd appreciate any help or advice.

 

Many thanks,

 

Tom

 

 

 

#targetengine 'usernameVariable'
 
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 );
 
//Adding XMP serverURL to text variables
createTextVariable(doc, "Next Issue", getNextIssue(), true );
 
}
 
function getNextMonth() {
var date = new Date();
var month = date.getMonth();
var Months = ["January-February",
"January-February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October-November",
"October-November",
"December"];
 
return Months[month+1];
 
}
 
 
 
function getNextIssue() {
var date = new Date();
var month = date.getMonth();
var Months = ["Vol.21 Issue 1",
"Vol.21 Issue 1",
"Vol.21 Issue 2",
"Vol.21 Issue 3",
"Vol.21 Issue 4",
"Vol.21 Issue 5",
"Vol.21 Issue 6",
"Vol.21 Issue 7",
"Vol.21 Issue 8",
"Vol.21 Issue 9",
"Vol.21 Issue 9",
"Vol.21 Issue 10"];
 
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;
}
 
 
//Add listeners to update file when opened.
app.addEventListener('afterOpen', addVariables);
app.addEventListener('beforeSave', addVariables);
app.addEventListener('afterSelectionChanged', addVariables);
app.addEventListener('afterNew', addVariables);

 

Adobe Community Professional
Correct answer by Sunil Yadav | Adobe Community Professional

Try to return your months like this:

if(month < 11){
    return Months[month+1]; 
}
else{
    return Months[0]; 
}

 

Best

Sunil

TOPICS
Scripting

Views

477

Likes

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

A script I am running to create a custom text variable stops running intermittently, please help

New Here ,
Mar 16, 2020

Copy link to clipboard

Copied

I am running a javascript (inserted below) to create custom text variables to create a 'next month' and 'next issue' variable for a magazine I work on.

 

For reasons unknown to me, it stops running correctly intermittently and when I open a template with the text variables on it reverts to showing the current month, not month + 1.

 

If i run the script before each template or document I open it seems to work okay, but this almost makes it pointless to have the script and leaves room for errors to be missed.

 

If anyone knows what could be going wrong, I'd appreciate any help or advice.

 

Many thanks,

 

Tom

 

 

 

#targetengine 'usernameVariable'
 
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 );
 
//Adding XMP serverURL to text variables
createTextVariable(doc, "Next Issue", getNextIssue(), true );
 
}
 
function getNextMonth() {
var date = new Date();
var month = date.getMonth();
var Months = ["January-February",
"January-February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October-November",
"October-November",
"December"];
 
return Months[month+1];
 
}
 
 
 
function getNextIssue() {
var date = new Date();
var month = date.getMonth();
var Months = ["Vol.21 Issue 1",
"Vol.21 Issue 1",
"Vol.21 Issue 2",
"Vol.21 Issue 3",
"Vol.21 Issue 4",
"Vol.21 Issue 5",
"Vol.21 Issue 6",
"Vol.21 Issue 7",
"Vol.21 Issue 8",
"Vol.21 Issue 9",
"Vol.21 Issue 9",
"Vol.21 Issue 10"];
 
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;
}
 
 
//Add listeners to update file when opened.
app.addEventListener('afterOpen', addVariables);
app.addEventListener('beforeSave', addVariables);
app.addEventListener('afterSelectionChanged', addVariables);
app.addEventListener('afterNew', addVariables);

 

Adobe Community Professional
Correct answer by Sunil Yadav | Adobe Community Professional

Try to return your months like this:

if(month < 11){
    return Months[month+1]; 
}
else{
    return Months[0]; 
}

 

Best

Sunil

TOPICS
Scripting

Views

478

Likes

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
Mar 16, 2020 0
Adobe Community Professional ,
Mar 16, 2020

Copy link to clipboard

Copied

Try to use at the end of both of your function:

return Months[month];
//not return Months[month+1];
Just remove +1 from the index, because .getMonth() is giving index of month not month number like:
If month is march you will get 2 instead of 3, if month is April you will get 3.
 
Best
Sunil

Likes

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
Reply
Loading...
Mar 16, 2020 0
New Here ,
Mar 17, 2020

Copy link to clipboard

Copied

Hi Sunil,

 

Thanks very much for your reply.

 

I am trying to run the script in order to put the next month in as the date of creation of the pages is always 1 month before publication, except in our dual month issues for which I have created the "months" 'January-February' and 'October-November'.

 

Will removing the +1 not just make it show the current month, unless you mean to remove the +1 and then shift all the months back by one?

 

Many thanks,

 

Tom

Likes

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
Reply
Loading...
Mar 17, 2020 0
Most Valuable Participant ,
Mar 17, 2020

Copy link to clipboard

Copied

Correct. getMonth returns a number from 0 to 11, so '0' corresponds with "January" and a "+1" should return "February". However, to prevent an error in Dcember, you still need to add another "January/Februari" at the end.

 

Can you verify if the open/save etc. events actually fire when needed? All it needs is a line

 

alert ('addVariables is run')

 

inside that function. You also may need to copy the function four times, with a different alert each time, for each of your 4 event handlers. That way you can hopefully pin-point the problem.

Likes

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
Reply
Loading...
Mar 17, 2020 0
New Here ,
Mar 17, 2020

Copy link to clipboard

Copied

Thank you very much for yout response!

 

However, you lost me in the second paragraph. Which open/save events do you mean?

 

Where do insert the line 'alert ('addVariables is run')'?

 

And do you mean that in the script I need to follow each bit of script with the line 'alert ('addVariables is run')'?

 

Apologies for my ignorance - scripting is very new to me.

 

Many thanks,

 

Tom

Likes

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
Reply
Loading...
Mar 17, 2020 0
Adobe Community Professional ,
Mar 17, 2020

Copy link to clipboard

Copied

Try to return your months like this:

if(month < 11){
    return Months[month+1]; 
}
else{
    return Months[0]; 
}

 

Best

Sunil

Likes

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
Reply
Loading...
Mar 17, 2020 0
New Here ,
Oct 21, 2020

Copy link to clipboard

Copied

@Sunil Yadav I'm trying to get the month 13 month forwards.
I tried the following, but failed and now I stumbled across your answer, so I hope you might have an idéa.

	return Months[month+2];

 

Likes

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
Reply
Loading...
Oct 21, 2020 0
Adobe Community Professional ,
Oct 21, 2020

Copy link to clipboard

Copied

Hi @Buster15A9

To have your range of month/s, first you need to make the mapping array.

Anyway, if you elaborate your query, then i might give a better idea or solution if I have.

 

Sunil

Likes

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
Reply
Loading...
Oct 21, 2020 0
New Here ,
Oct 21, 2020

Copy link to clipboard

Copied

Im using the same script as above. But I just realized that adding 1 month is the same as adding 13.. doh

 

Likes

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
Reply
Loading...
Oct 21, 2020 0
Adobe Community Professional ,
Oct 22, 2020

Copy link to clipboard

Copied

@Buster15A9 , There are total 12 months only in real world. So this mapping array also OP has created upto 12 only. OP also has trouble getting first month after 12th month. I am assuming that you know about Array, so to get first element from an array you need to pass index as zero like wise if you need 12th element from that array you need to pass index 11(that means length-1). So When it comes to 13th element, which is nothing but 1st element, this is the reason I put this codition of returning. Because OP as made total 11 elements only instead of 12 elements.

 

Please elaborate your concern...!!!

 

Best

Sunil

Likes

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
Reply
Loading...
Oct 22, 2020 0