Copy link to clipboard
Copied
Hi,
Preface: I have a very rough understanding of javascript, so bear with me if my questions are very simple.
I have a bunch of documents I need to edit. For all of them, I know I need to change a font (with two different syles), and I need to do a colour replacement.
Here is some code I patched together:
for (var i = app.documents.length-1; i >= 0; i--) {
var doc = app.documents;
fontChange();
function fontChange(){
step1();
}
function step1(){
//Tuffy Regular > Roboto Regular
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.appliedFont = "Tuffy";
app.findTextPreferences.fontStyle = "Regular";
app.changeTextPreferences.appliedFont = "Roboto";
app.changeTextPreferences.fontStyle = "Regular";
app.activeDocument.pageItems.everyItem().locked = false;
app.activeDocument.stories.everyItem().changeText();
app.findChangeTextOptions.includeMasterPages = true;
}
fontChangeTwo();
function fontChangeTwo(){
step2();
}
function step2(){
//Tuffy Bold > Roboto Bold
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.appliedFont = "Tuffy";
app.findTextPreferences.fontStyle = "Bold";
app.changeTextPreferences.appliedFont = "Roboto";
app.changeTextPreferences.fontStyle = "Bold";
app.activeDocument.pageItems.everyItem().locked = false;
app.activeDocument.stories.everyItem().changeText();
app.findChangeTextOptions.includeMasterPages = true;
}
}
Problem 1) I thought the first two lines would allow me to run this in all my documents but it doesn't work.
Problem 2) For some reason, my InDesign refuses to recognise Roboto when I run the script. It replaces the font with "Roboto (otf)" and highlights the text in red. It works fine with other fonts. If I manually replace the text in red with "Roboto", it works fine.
I've had issues with Roboto for a while in InDesign, the only way I found to have it work is to NOT activate it on Adobe fonts and have it installed on my machine instead.
Problem 3) The easiest one, I don't know how to do a colour replacement. I need to replace Adobe's pure black with another colour from my palette.
I would be super grateful if anyone could help with any of these problems.
Thanks in advance.
Copy link to clipboard
Copied
You waver between a number of different approaches. I you want to iterate through the open documents and use app.activeDocument, you must activate (i.e.bring to the front) each document in turn. But there's no need for that (and it's slow bussiness). Instead you can talk to documents without activating them, see your amended script. below.
Some other comments: resetting the preferences is needed only once, you do that before the loop because doing it inside the loop is a waste of time. And to make a change in a document, there's no need to address its stories: simply target the document itself.
Hope this helps.
Peter
app.findTextPreferences = app.changeTextPreferences = null;
app.findChangeTextOptions.includeMasterPages = true;
for (var i = app.documents.length-1; i >= 0; i--) {
app.documents[i].pageItems.everyItem().locked = false;
step1 (app.documents[i]);
step2 (app.documents[i]);
}
function step1 (doc) {
//Tuffy Regular > Roboto Regular
app.findTextPreferences.appliedFont = "Tuffy";
app.findTextPreferences.fontStyle = "Regular";
app.changeTextPreferences.appliedFont = "Roboto";
app.changeTextPreferences.fontStyle = "Regular";
doc.changeText();
}
function step2 (doc) {
//Tuffy Bold > Roboto Bold
app.findTextPreferences.appliedFont = "Tuffy";
app.findTextPreferences.fontStyle = "Bold";
app.changeTextPreferences.appliedFont = "Roboto";
app.changeTextPreferences.fontStyle = "Bold";
doc.changeText();
}
Copy link to clipboard
Copied
And there is a version that doesn't use a loop:
app.findTextPreferences = app.changeTextPreferences = null;
app.findChangeTextOptions.includeMasterPages = true;
app.documents.everyItem().pageItems.everyItem().locked = false;
app.findTextPreferences.appliedFont = "Tuffy\tRegular";
app.changeTextPreferences.appliedFont = "Roboto\tRegular";
app.documents.everyItem().changeText();
app.findTextPreferences.appliedFont = "Tuffy\tBold";
app.changeTextPreferences.appliedFont = "Roboto\tBold";
app.documents.everyItem().changeText();
Copy link to clipboard
Copied
Thank you Peter, it works perfectly and it even solved my font problem! I'll do some research about changing font colours another time, I'm sure I can figure it out 🙂
Copy link to clipboard
Copied
It looks like I can't quite figure out how to change the colour everywhere in my document...
I added to the code to replace font colours:
app.findTextPreferences = app.changeTextPreferences = null;
app.findChangeTextOptions.includeMasterPages = true;
app.documents.everyItem().pageItems.everyItem().locked = false;
app.findTextPreferences.appliedFont = "Tuffy\tRegular";
app.changeTextPreferences.appliedFont = "Roboto\tRegular";
app.documents.everyItem().changeText();
app.findTextPreferences.appliedFont = "Tuffy\tBold";
app.changeTextPreferences.appliedFont = "Roboto\tBold";
app.documents.everyItem().changeText();
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.strokeColor = "Black";
app.changeTextPreferences.strokeColor = "[other colour]";
app.documents.everyItem().changeText();
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.fillColor = "Black";
app.changeTextPreferences.fillColor = "[other colour]";
app.documents.everyItem().changeText();
This works perfectly. However, I'd like to also replace pure black from shapes and table cells. I tried "
Copy link to clipboard
Copied
For cells you do something different because they're not objects (not objects that you can search anyway). Do something like this:
app.documents[0].tables.everyItem().cells.everyItem().fillColor = 'some fill';