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

Array List with a Italic or Roman opposite to the Paragraph text

Engaged ,
Apr 22, 2018 Apr 22, 2018

Copy link to clipboard

Copied

Hi, sorry I am new with the Scripting.

Inside of my document, are many CODES, and I need check with Indesign Script:

  • all CODES are in ITALIC if the paragraph text is in REGULAR
  • all CODES are in REGULAR if the paragraph text is in ITALIC

For example this is a example of my document with the 2 different cases:

All human beings are born free and equal in dignity and rights. (CODE 001) They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.

Everyone has the right to life, (CODE 002) liberty and security of person.




Maybe I need an Array, because my CODES are many, like:

“CODE 001”

“CODE 002”

“CODE 003”

“CODE 004”

...

Thanks so much for your help.

TOPICS
Scripting

Views

1.1K

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 Expert ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

1. If your codes match a pattern, look for them using a GREP search such as CODE\s\d+

2. Iterate through the found items. If an item's style is Regular, make it Italic, otherwise (i.e. if it's Italic) make it Regular.

P.

Votes

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
Engaged ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Hi Peter,

sorry no the codes not match a pattern, I use for create a simple example. The code are like:

"STR 94583"

"TRe87 090"

"MR 840EOA"

Votes

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 Expert ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Hi Manuel,

If you cannot create a GREP pattern that will find your codes you need a list of all possible codes and do a search with every item of the list. Or if you are lucky you'll find another unique formatting property for all used codes and you are searching with that…

Regards,
Uwe

Votes

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 Expert ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

In that case you'll have to list them in an array. Your script would be along these lines (untested):

codes = ["STR 94583", "TRe87 090", "MR 840EOA"];

app.findTextPreferences = null;

for (i = 0; i < codes.length; i++) {

  app.findTextPreferences.findWhat = codes;

  found = app.documents[0].findText();

  for (j = 0; j < found.length; j++) {

    if (found.fontStyle == 'Italic') {

      found.fontStyle == 'Regular';

    } else {

      found.fontStyle == 'Italic';

    }

  }

}

P.

Votes

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
Engaged ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Looks works, but I added a alert with else if, for show when is a error. But now don't work. Please can you check, or let me know how need I see the errors.

Thanks!

codes = ["STR 94583", "TRe87 090", "MR 840EOA"];

app.findTextPreferences = null;

for (i = 0; i < codes.length; i++) {

  app.findTextPreferences.findWhat = codes;

  found = app.documents[0].findText();

  for (j = 0; j < found.length; j++) {

    if (found.fontStyle == 'Italic') {

      found.fontStyle == 'Regular';

    } else if (found.fontStyle == 'Italic'){

    } else{alert("ERROR: some styles are wrong")}

  }

}

Votes

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 Expert ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Your code makes little sense. It says 'If style is italic, apply regular else if style is italic, do nothing else show an error. So when some found item is regular, the iteration always falls through to the error line.

I now see that I had some operators wrong. In my original script, in lines 8 and 10, replace == with =

Votes

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
Engaged ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Hi Peter,

I use again your code and I did the changes you indicate, but have some issues.

Looks for example if I run the script with this text:

ORIGINAL TEXT:

All human beings are born free and equal in dignity and rights STR 94583.

Everyone has the right to life. TRe87 090 liberty and security of person.

AFTER APPLY SCRIPT, THE RESULT IS CORRECT IN THIS CASE:

All human beings are born free and equal in dignity and rights STR 94583.

Everyone has the right to life. TRe87 090 liberty and security of person.

BUT IF FOR ANY REASON THE ORIGINAL TEXT, HAVE ALREADY APPLY SOME FONT STYLE LIKE THIS:

All human beings are born free and equal in dignity and rights STR 94583.

Everyone has the right to life. TRe87 090 liberty and security of person.

AFTER APPLY THE SCRIPT IS INCORRECT:

All human beings are born free and equal in dignity and rights STR 94583.

Everyone has the right to life. TRe87 090 liberty and security of person.

Thanks for help me.

Votes

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
Engaged ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Also, Is a bucle, each time you run the script do a different thing

Votes

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 Expert ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Manuel -- The behaviour you see is consistent with the script: it changes regular to italic and italic to regular. The script doesn't check whether the change is necessary. Why not? Because that's not what you asked. You can change the script so that it doesn't make the change if it's not necessary. Maybe by checking the font style of the applied paragraph style. Good one for you to figure out!

Votes

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
Guide ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Hi,

Just for help! …

var myDoc = app.activeDocument,

myCodes = ["STR 94583", "TRe87 090", "MR 840EOA"],  C = myCodes.length,  c;

app.findTextPreferences = null;  

for ( c = 0; c < C; c++) {  

    app.findTextPreferences.findWhat = myCodes;  

    myFound = myDoc.findText();

    var F = myFound.length,  f;

    for ( f = 0; f < F; f++) myFound.appliedParagraphStyle.fontStyle == 'Italic' ? myFound.fontStyle = 'Regular' : myFound.fontStyle = 'Italic';  

}

app.findTextPreferences = null;  

Best,

Michel, from FRIdNGE

Votes

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
Engaged ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Hi Michel,

thanks but didn't work. I am using Indesign CC 2018.

Nothing changes with your script.

Thanks so much.

Votes

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
Guide ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Manuel,

The second loop wasn't so cool I thought! …

This code is easier to read:

    var myDoc = app.activeDocument, 

    myCodes = ["STR 94583", "TRe87 090", "MR 840EOA"],  C = myCodes.length,  c; 

    app.findTextPreferences = null;

    for ( c = 0; c < C; c++) {    

        app.findTextPreferences.findWhat = myCodes;    

        myFound = myDoc.findText(); 

        var F = myFound.length,  f;

        for ( f = 0; f < F; f++) {

            if (myFound.appliedParagraphStyle.fontStyle == 'Roman') myFound.appliedCharacterStyle = 'Italic';

            else if (myFound.appliedParagraphStyle.fontStyle == 'Italic') myFound.appliedCharacterStyle = 'Roman';

            else if (myFound.appliedParagraphStyle.fontStyle == 'Bold') myFound.appliedCharacterStyle = 'Bold Italic';

            else if (myFound.appliedParagraphStyle.fontStyle == 'Bold Italic') myFound.appliedCharacterStyle = 'Bold';

        }

    }

    app.findTextPreferences = null;

The code checks the font style used by the para style applied to the para and applies the "contrary" char style as:

Roman => Italic,

Italic => Roman,

Bold => Bold Italic,

Bold Italic => Bold.

Best,

Michel

Votes

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
Guide ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

However, note that the first code given works for me and this variant too!

… If the game is limited to a "roman (or regular) vs. italic" fight! 

var myDoc = app.activeDocument,

myCodes = ["STR 94583", "TRe87 090", "MR 840EOA"],  C = myCodes.length,  c; 

app.findTextPreferences = null;    

for ( c = 0; c < C; c++) {    

    app.findTextPreferences.findWhat = myCodes;    

    myFound = myDoc.findText(); 

    var F = myFound.length,  f; 

    for ( f = 0; f < F; f++) myFound.appliedParagraphStyle.fontStyle == 'Roman' ? myFound.appliedCharacterStyle = 'Italic' : myFound.appliedCharacterStyle = 'Roman';    

app.findTextPreferences = null;

Best,

Michel

Votes

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
Engaged ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Hi Michel!

thanks for help me. I let you know I get a Error, when I apply Both Scripts.

But, is very IMPORTANT, the script can know when the codes have already applied a styles. I let you know because is very common to receive text with a mistakes.

For example sometimes the translators give a document and some codes are in italic, and sometimes in Roman, because they do a mistakes, and apply manually the styles and other times they forget apply the right style. Like you can see here in this text the translators apply the right Italic in the first code STR 94583 but they forget apply in the second STR 94583 if the script is not smarty won't be work:

All human beings are born free and equal in dignity and rights. (STR 94583) They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood STR 94583.

Everyone has the right to life, (TRe87 090) liberty and security of person.

Please see the images for more information.  I use the first code because is more clear.

Screen Shot 2018-04-24 at 06.37.33.png

i3.png

Screen Shot 2018-04-24 at 06.38.10.png

Thank you very much

Votes

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
Guide ,
Apr 24, 2018 Apr 24, 2018

Copy link to clipboard

Copied

Hi,

First, the scripts don't take in account how are styled the occurrences found.

they check their para style applied to the para where the occurrence is found. So the errors will be corrected.

Then, I've used a font in the 2 last scripts that has no "Regular" font style, but "Roman".

Change this word and, normally, they could work! …

Best,

Michel

Votes

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
Engaged ,
Apr 24, 2018 Apr 24, 2018

Copy link to clipboard

Copied

Hi Michel!

sorry for the mistake, I just add the right character style name.

Screen Shot 2018-04-24 at 12.06.50.png

But If you try to run on the paragraph with Italic, like this, you will see the code didn't change, and need also change to Roman:

Everyone has the right to life, (STR 94583) liberty and security of person.

thanks so much!

Votes

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
Guide ,
Apr 24, 2018 Apr 24, 2018

Copy link to clipboard

Copied

Well! …

Javascript seems to have some problems with "Regular/Roman" font styles! …

This works better:

1/ including a reset,

2/ placing the treatment of the problem in an "else" at the end of the "if" chain.

Capture d’écran 2018-04-24 à 15.03.40.png

var myDoc = app.activeDocument, 

myCodes = ["STR 94583", "TRe87 090", "MR 840EOA"],  C = myCodes.length,  c; 

app.findTextPreferences = null;

for ( c = 0; c < C; c++) {    

    app.findTextPreferences.findWhat = myCodes;    

    myFound = myDoc.findText(); 

    var F = myFound.length,  f;

    for ( f = 0; f < F; f++) {

        myFound.appliedCharacterStyle = myDoc.characterStyles[0];

        if ( myFound.appliedParagraphStyle.appliedFont.fontFamily == 'Avenir' ) {

            if ( myFound.appliedParagraphStyle.appliedFont.fontStyleName == 'Oblique' ) myFound.appliedCharacterStyle = 'Avenir Roman -- blue';

            else if ( myFound.appliedParagraphStyle.appliedFont.fontStyleName == 'Black' ) myFound.appliedCharacterStyle = 'Avenir Black Oblique -- blue';

            else if ( myFound.appliedParagraphStyle.appliedFont.fontStyleName == 'Black Oblique' ) myFound.appliedCharacterStyle = 'Avenir Black -- blue';

            else myFound.appliedCharacterStyle = 'Avenir Oblique -- blue';

        }

        if ( myFound.appliedParagraphStyle.appliedFont.fontFamily == 'Arial' ) {

            if ( myFound.appliedParagraphStyle.appliedFont.fontStyleName == 'Italic' ) myFound.appliedCharacterStyle = 'Arial Regular -- red';

            else if ( myFound.appliedParagraphStyle.appliedFont.fontStyleName == 'Bold' ) myFound.appliedCharacterStyle = 'Arial Bold Italic -- red';

            else if ( myFound.appliedParagraphStyle.appliedFont.fontStyleName == 'Bold Italic' ) myFound.appliedCharacterStyle = 'Arial Bold -- red';

            else myFound.appliedCharacterStyle = 'Arial Italic -- red';

        }

    }

}

Votes

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
Engaged ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

LATEST

Hi Michel,

wow! looks pretty nice. But I was testing deeply, and I find one issue.

For example, sometimes the beginners Editors when they need apply the italics to a paragraph P_Avenir_Roman they use directly the Character Style Avenir_Oblique -- blue instead of the Paragraph Style P_Avenir_Oblique. In this case when the Script run, we get a error, because the code will appear with Avenir_Oblique -- blue instead of the Avenir_Roman -- blue.

Look before and after run the script:

Screen Shot 2018-04-26 at 23.16.22.png

I expend hours to fix, but Is not working

if ( myFound.appliedParagraphStyle.appliedFont.fontStyleName == 'Roman' && myFound.appliedCharacterStyle.item == 'Avenir Oblique -- blue'){

Here the Indesign files

Big thank You!

Votes

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