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 😉
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.t
...
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
Copy link to clipboard
Copied
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
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
Copy link to clipboard
Copied
That did it, @Mike Bro ! Thank you very much!