Does anyone know if it's possible to style text a certain way using ExtendScript, only when a certain condition is met for the paragraph in question?
I've cobbled together an ExtendScript script that I use in InDesign to fix text styles when they aren't fixed properly by the Paragraph Style tool, and it works well so far - changing Courier and Arial Unicode text to Times New Roman and fixing the font size - but i would really like to include a function that changes Times New Roman Italic to Times New Roman Bold Italic - but ONLY when the paragraph it appears in has a first letter that is set in Univers. Is there a way I can include an 'if' statement that will only trigger this style change in those circumstances?
Here's my existing code:
var mydoc = app.activeDocument;
var theFontSize = [
'Courier New','16','Courier New','8.75',
'Times New Roman','16','Times New Roman','8.75',
];
for (i = 0; i < (theFontSize.length/4); i++) {
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedFont = theFontSize[i*4];
if (theFontSize[(i*4)+1] != ''){
app.findTextPreferences.pointSize = theFontSize[(i*4)+1];
};
app.changeTextPreferences.appliedFont = theFontSize[(i*4)+2];
if (theFontSize[(i*4)+3] != ''){
app.changeTextPreferences.pointSize = theFontSize[(i*4)+3];
};
mydoc.changeText();
};
var theFontReplacements = [
'Courier New','Regular','Times New Roman','Regular',
'Courier New','Italic','Times New Roman','Italic',
'Courier New','Bold','Times New Roman','Bold',
'Courier New','Bold Italic','Times New Roman','Bold Italic',
'Courier New','75 Black','Univers','75 Black',
'Arial Unicode MS','Regular','Times New Roman','Regular',
];
for (i = 0; i < (theFontReplacements.length/4); i++) {
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedFont = theFontReplacements[i*4];
if (theFontReplacements[(i*4)+1] != ''){
app.findTextPreferences.fontStyle = theFontReplacements[(i*4)+1];
};
app.changeTextPreferences.appliedFont = theFontReplacements[(i*4)+2];
if (theFontReplacements[(i*4)+3] != ''){
app.changeTextPreferences.fontStyle = theFontReplacements[(i*4)+3];
};
mydoc.changeText();
};
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
This should get you started. It will be slower than using changeText
, but I don’t think you can do what you’re asking with changeText
.
// Change applied font of text set in Times New Roman Italic in paragraphs whose first character is set in Univers to Times New Roman Bold Italic
app.findTextPreferences.appliedFont = 'Times New Roman';
app.findTextPreferences.fontStyle = 'Italic';
var foundItems = app.activeDocument.findText(); // Set foundItems to all results of search according to app.findTextPreferences
for (var i = 0; i < foundItems.length; i++) { // For each result found
var item = foundItems[i];
if (item.paragraphs[0].characters[0].appliedFont.name.indexOf('Univers') == 0) { // If item’s first enclosing paragraph’s first character’s font name starts with “Univers”
item.appliedFont = 'Times New Roman\tBold Italic'; // Set item’s font to Times New Roman italic
}
};