Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script for finding double-applied italics

Explorer ,
Feb 14, 2020 Feb 14, 2020

I'm in need of a script to try and solve a worfklow issue. We work with tagged text files poured into InDesign, in which editors tag what should be italic for emphasis or grammar (like a book title). However, if a designer chooses to set the containing paragraph style as italic, those italic tagged words need to be retagged as a roman style. This is hard to catch in every instance in a long document. The current workflow is to do a Find/Change for every word with an italic character style contained within the paragraph style that was defined as italic, but it has to be done for each paragraph style because you can't search for more than one paragraph style at a time. I thought there has to be a way to script this, but I'm not great at scripting.

 

This is basically what I would need:

If [character style: ITAL] is found in [Any Paragraph Style] that has [Italic] defined in Font Style, then replace [character style: ITAL] with [character style: roman]

 

Would anyone have any ideas on how to script this? Any help would be appreciated.

TOPICS
Scripting
1.9K
Translate
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

Community Expert , Feb 17, 2020 Feb 17, 2020

This ought to do it. You need to fill in the actual name of the character style (unless "ITALIC" is right?) and the name of the regular font style, in case it's something like "55 Medium" (I'm looking at you, Adrian Frutiger!). Then just run the script.

It only changes the character style's text to Roman if (1) its paragraph style uses that same style name for [Italic], and (2) there is no other override that changes the font style (such as a manually applied override Bold). That ought to make i

...
Translate
Advisor ,
Feb 15, 2020 Feb 15, 2020

Hi nwarn,

Give the below code a try for your needs........

Note: the script will only change the Font Style from Italic to Roman when available as part of the font family.

Array.prototype.unique = function (){
    var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++){
        for(var x = 0, y = r.length; x < y; x++){
            if(r[x]==this[i]) continue o;}
        r[r.length] = this[i];}
    return r;
}
 
Array.prototype.findIn = function(search){
    var r = Array();
    for (var i=0; i<this.length; i++)
        if (this[i].indexOf(search) != -1){
            r.push(this[i].substr(this[i].indexOf("\t") + 1, this[i].length));
        }
    return r;
}

var myFonts = app.fonts.everyItem();
var pstyles = app.activeDocument.allParagraphStyles;

for (var i = 1; i < pstyles.length; i++) {
 var myFont = pstyles[i].appliedFont.fontFamily;
 var myFontStyles = myFonts.name.findIn(myFont);
 if (pstyles[i].fontStyle == 'Italic' && (String(myFontStyles).match("\\b" +'Roman'+ "\\b"))) {
 pstyles[i].fontStyle = 'Roman';

 }
}

Regards,

Mike

Translate
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
Explorer ,
Feb 17, 2020 Feb 17, 2020

Thank you, Mike! This is really helpful and kind of you. However, this changes the paragraph style to Roman/Regular. I need to find instances where the italic character style <ITAL> is applied to text within any paragraph style defined as Italic in Font Style, and switch that character style to a roman character style <roman>.  The paragraph style needs to stay untouched. Both character styles are already defined in the document.

 

So, for instance, where paragraph styles <p1> and <p3> have Italic defined in Font Style, green indicates <ITAL> character style, pink indicates <roman> character style (which is defined as Regular/Roman already).

Before:

<p1>The quick brown fox jumps over the lazy dog.</p1>

<p2>The quick brown fox jumps over the lazy dog.</p2>

<p3>The quick brown fox jumps over the lazy dog.</p3>

After:

<p1>The quick brown fox jumps over the lazy dog.</p1>

<p2>The quick brown fox jumps over the lazy dog.</p2>

<p3>The quick brown fox jumps over the lazy dog.</p3>

Translate
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 Expert ,
Feb 17, 2020 Feb 17, 2020

This ought to do it. You need to fill in the actual name of the character style (unless "ITALIC" is right?) and the name of the regular font style, in case it's something like "55 Medium" (I'm looking at you, Adrian Frutiger!). Then just run the script.

It only changes the character style's text to Roman if (1) its paragraph style uses that same style name for [Italic], and (2) there is no other override that changes the font style (such as a manually applied override Bold). That ought to make it quite safe to run.

 

// Set up variables
romanFont = 'Regular';
italicStyle = app.activeDocument.characterStyles.item("ITALIC");
italicFont = italicStyle.fontStyle;

// Find all instances of ITALIC
app.findTextPreferences = null;
app.findTextPreferences.appliedCharacterStyle = italicStyle;
list = app.activeDocument.findText();

// Check instances and change if necessary
numChanges = 0;
for (i=0; i<list.length; i++)
{
	if (list[i].fontStyle == italicFont && list[i].appliedParagraphStyle.fontStyle == italicFont)
	{
		list[i].fontStyle = romanFont;
		numChanges++;
	}
}
alert (numChanges+' changes made');

 

(After posting: my, grandmother, what large font you have for code! Also: the syntax coloring is strangely different in the post than in the post editor ...)

 

Translate
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
Explorer ,
Feb 17, 2020 Feb 17, 2020
LATEST

BRILLIANT!! Thank you so much!!!! I did edit the code for my purposes (much like a monkey with a typewriter). The original code applies Regular font style as an override, my edited version replaces the character style <ITALIC> with the character style <ROMAN>.  I would never have figured this out without your help. Thank you!!

 

// Set up variables
italicStyle = app.activeDocument.characterStyles.item("ITALIC");
italicFont = italicStyle.fontStyle;
romanStyle = app.activeDocument.characterStyles.item("ROMAN");

// Find all instances of ITALIC
app.findTextPreferences = null;
app.findTextPreferences.appliedCharacterStyle = italicStyle;
list = app.activeDocument.findText();

// Check instances and change if necessary
numChanges = 0;
for (i=0; i<list.length; i++)
{
	if (list[i].fontStyle == italicFont && list[i].appliedParagraphStyle.fontStyle == italicFont)
	{
		list[i].appliedCharacterStyle = romanStyle;
		numChanges++;
	}
}
alert (numChanges+' changes made');
Translate
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