Skip to main content
Inspiring
June 9, 2022
Answered

Simple script request

  • June 9, 2022
  • 1 reply
  • 354 views

I nedd a simple script that will open all files in a book and write a text variable with a consecutive number. Ex: Text var in file 1of the book will be var 01; Text var in file 2 of the book will be var 02; Text var in file 3 of the book will be var 03 and so on. I'm just a poor designer  so I wonder if any smart programmer could help me 😉

This topic has been closed for replies.
Correct answer Mike Bro

Hello @Rita247900390kon,

 

Give this a try.... without the "0" being added we need to convert myFileIndex to a string

var book = app.activeBook;
for (var i=0; i<book.bookContents.length; i++) {
app.open (book.bookContents[i].fullName);
var myFileIndex = book.bookContents[i].index + 1;
var myTextVariable = myFileIndex.toString();
var Doi = "DoiNumber"
try {
if (myFileIndex < 10){
app.activeDocument.textVariables.item(Doi).variableOptions.contents = "0" + myTextVariable;
} else {
app.activeDocument.textVariables.item(Doi).variableOptions.contents = myTextVariable;
}
}
catch (e) { }
app.activeDocument.save(book.bookContents[i].fullName);
app.activeDocument.close(SaveOptions.NO);
}

Regards,

Mike

1 reply

Legend
June 9, 2022

Hello @Rita247900390kon,

Give the below script a try, it will create a new Custom Text Variable/Text: named per your request.

If you do not want the book files to be closed after the variables are added just remove the line "app.activeDocument.close(SaveOptions.NO);" from the code.

var book = app.activeBook;

for (var i=0; i<book.bookContents.length; i++) {
   app.open (book.bookContents[i].fullName);
   var myFileIndex = book.bookContents[i].index + 1;
   var myTextVariable = "0" + myFileIndex;

 try { app.activeDocument.textVariables.add({name: myTextVariable, variableType:VariableTypes.CUSTOM_TEXT_TYPE,});
         app.activeDocument.textVariables.item(myTextVariable).variableOptions.contents = myTextVariable;
        } catch (e) {
    }

    app.activeDocument.save(book.bookContents[i].fullName);
    app.activeDocument.close(SaveOptions.NO);
}

 

Regards,

Mike

Inspiring
June 9, 2022

Thank you so much for your prompt answer, @Mike Bro 

I already have the text variable in my files so I changed a little your script.  

And I usually have more than 9 files in a book, so I made this:

var book = app.activeBook;
for (var i=0; i<book.bookContents.length; i++) {
app.open (book.bookContents[i].fullName);
var myFileIndex = book.bookContents[i].index + 1;
var myTextVariable = myFileIndex;
var Doi = "DoiNumber"
try {
if (myFileIndex < 10){
app.activeDocument.textVariables.item(Doi).variableOptions.contents = "0" + myTextVariable;
} else {
app.activeDocument.textVariables.item(Doi).variableOptions.contents = myTextVariable;
}
}
catch (e) { }
app.activeDocument.save(book.bookContents[i].fullName);
app.activeDocument.close(SaveOptions.NO);
}

It works, but only up to 9.  Would you be able to detect what I did wrong?

Once more, thanks a lot

Regards

Rita

Mike BroCorrect answer
Legend
June 9, 2022

Hello @Rita247900390kon,

 

Give this a try.... without the "0" being added we need to convert myFileIndex to a string

var book = app.activeBook;
for (var i=0; i<book.bookContents.length; i++) {
app.open (book.bookContents[i].fullName);
var myFileIndex = book.bookContents[i].index + 1;
var myTextVariable = myFileIndex.toString();
var Doi = "DoiNumber"
try {
if (myFileIndex < 10){
app.activeDocument.textVariables.item(Doi).variableOptions.contents = "0" + myTextVariable;
} else {
app.activeDocument.textVariables.item(Doi).variableOptions.contents = myTextVariable;
}
}
catch (e) { }
app.activeDocument.save(book.bookContents[i].fullName);
app.activeDocument.close(SaveOptions.NO);
}

Regards,

Mike