Simple script request

Community Beginner ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

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 😉

TOPICS
Scripting

Views

84

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
community guidelines

correct answers 1 Correct answer

Advocate , Jun 09, 2022 Jun 09, 2022
Hello @Rita247900390kon, Give this a try.... without the "0" being added we need to convert myFileIndex to a stringvar 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.t...

Likes

Translate

Translate
Advocate ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

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

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
community guidelines
Community Beginner ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

Thank you so much for your prompt answer, @mikeb41294032 

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

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
community guidelines
Advocate ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

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

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
community guidelines
Community Beginner ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

LATEST

That did it, @mikeb41294032 ! Thank you very much!

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
community guidelines