Skip navigation
Currently Being Moderated

Find text with different appliedLanguage

Jul 27, 2012 6:47 AM

Tags: #cs5 #script #text #javascript #indesign #scripting

So..

 

All the documents i'm working on must be set on a specific language

(eg. "German (2006 reform)"). However, sometimes during text import, mistakes happen and texts "jump" on a different language (i had english, romanian, swiss etc).

 

i have to build a tool (a fast one), that will find any text that has a different than "German" language applied on it, and reports on what page it appears.

Other than iterating each paragraph, each tabel and each footnote i have no idea. And since the documents usualy have 1000+ full of text, it will take quite a while.. can anyone give me a different idea?

 

Thanks a lot!

 
Replies
  • Currently Being Moderated
    Jul 27, 2012 8:09 AM   in reply to Vamitul

    A potentially destructive way, so make sure you don't accidentally save:

     

    1. Create a new color swatch and call it 'Gut Text'.

    2. Find the text with your German (2006) language applied and replace with your new swatch.

    3. Search for remaining text in black.

     

    Pro: well it's fast and should be 100% accurate.

     

    Con: It's 'destructive' because it changes the text color. Of course you can throw away the "Gut Text" swatch and replace it with Black, but if you used any other text colors than black, it will remove these as well. Also, text in a color other than black will *not* be checked.

     

    (You don't have to use a color swatch, you can use a couple of other text attributes. You cannot use *any* text attribute to weed out bad text -- for instance, suppose you decide to change your 'good' text to a baseline shift of 0.001pt. But then you'd have to search for "Baseline shift not 0.001pt", and InDesign cannot do that -- you'd have to search for a baseline of 0pt, and hope no-one did funny things with it.

     

    The very best would be if you can find a pure binary text attribute -- one that is either ON or OFF -- of which you are totally sure no-one used it anywhere.)

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 8:14 AM   in reply to [Jongware]

    [Jongware] wrote:

     

    The very best would be if you can find a pure binary text attribute -- one that is either ON or OFF -- of which you are totally sure no-one used it anywhere.)

     

    Got one: Character color, Overprint fill.

     

    .. At least, for me that one would work O_o as I've never ever used this particular function myself.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 8:54 AM   in reply to [Jongware]

    Conditions are useful for this, and they leave colour alone. Plus you could leave the applied conditions in place because you see them only in preview mode.

     

    Peter

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 10:13 AM   in reply to Peter Kahrel

    Hah -- another rare function I live to see put to good use :D

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 28, 2012 12:12 PM   in reply to Vamitul

    @Vamitul – if you know that all your text should be in a specific language, why don't you apply that language to all stories in the document?
    Why do you have to report the instances of a not properly set language?

     

    The following example is not perfect (it will not get tables nested in tables), but otherwise would apply a specific language to all stories, tables (not nested! in tables) and footnotes of a given document. Do not use the string I present here for "German (2006 reform)", because its dependent of my German InDesign:

     

    var myLanguage = /***For German InDesign, only:***/"Deutsch: Rechtschreibreform 2006"; //Perhaps use: "German (2006 reform)" 
    //All Stories:
    app.documents[0].stories.everyItem().appliedLanguage = myLanguage;
    //All Tables:
    app.documents[0].stories.everyItem().tables.everyItem().cells.everyItem().texts[0].appliedLanguage = myLanguage;
    //All Footnotes
    app.documents[0].stories.everyItem().footnotes.everyItem().texts[0].appliedLanguage = myLanguage;
    //All Tables in Footnotes:
    app.documents[0].stories.everyItem().footnotes.everyItem().texts[0].tables.everyItem().cells.everyItem().texts[0].appliedLanguage = myLanguage;
     
    //CAUTION !! Not supported here: 
    //1. Tables nested in tables
    

     

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 29, 2012 3:17 PM   in reply to Vamitul

    Try this.

     

    Make a copy of your document and on the COPY run this script.

     

    myDoc=app.documents[0];
    var myLanguage = /***For German InDesign, only:***/"Deutsch: Rechtschreibreform 2006"; //Perhaps use: "German (2006 reform)" // this line stolen from Uwe 
     
    uc = (app.activeDocument.conditions.item("My Unique Condition That I'm Pretty Sure Does Not Come Up In The Document (23145871834gs to be extra safe)").isValid) 
        ? app.activeDocument.conditions.item("My Unique Condition That I'm Pretty Sure Does Not Come Up In The Document (23145871834gs to be extra safe)") 
        : app.activeDocument.conditions.add
    ({name: "My Unique Condition That I'm Pretty Sure Does Not Come Up In The Document (23145871834gs to be extra safe)", indicatorColor: UIColors.YELLOW, indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
    app.changeGrepPreferences = app.findGrepPreferences = null;
    app.findGrepPreferences.appliedLanguage= myLanguage;
    app.changeGrepPreferences.appliedConditions = [uc];
    finds=[];
    finds=myDoc.changeGrep();
    uc.visible=0;
    

     

    The German text will not be visible (you can make it visible by clicking on the conditional text panel)

    The text you are left with is all non-German.

     

    You might wan't to add the the find preferences; tables, footnotes, etc.

     

    There is a professional program Language Lamp that you might like if you do this stuff a lot.

    http://www.mindsteam.com/products/languagelamp/index.html

    The pro version which includes scripting and preflight is http://www.mindsteam.com/products/languagelamppro/index.html

     

    Good luck

     

    Trevor

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 30, 2012 8:02 AM   in reply to Trevorׅ

    The mindsteam version is a lot more proffesional (I have it) but this also makes your copy nice and pretty.

     

    myDoc=app.documents[0];
    langs=myDoc.languages;
    con=[];
    app.changeGrepPreferences = app.findGrepPreferences = null;
    for (c=0;c<langs.length;c++){
        app.findGrepPreferences.appliedLanguage=langs[c];
        finds=new Array;
        finds=myDoc.findGrep();
        if (finds.length>0){
     
        r=Math.floor((Math.random()*255));
        g=Math.floor((Math.random()*255));
        b=Math.floor((Math.random()*255));
     
        con[c] = (app.activeDocument.conditions.item(langs[c].name).isValid) 
        ? app.activeDocument.conditions.item(langs[c].name) 
        : app.activeDocument.conditions.add
        ({name: langs[c].name, indicatorColor: [r,g,b], indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
     
        app.changeGrepPreferences.appliedConditions = [con[c]];
        finds=myDoc.changeGrep();
    }
    }
    app.changeGrepPreferences = app.findGrepPreferences = null;
    

    ScreenShot047.png

    One can see that the : after the UK and French are Hebrew, That would be very hard to spot without this type of script

     

    After testing on a longer document I see the script is a bit on the slow side Oh well.

     

    I thought it might be nice to assign a constant color scheme to the language instead of a random one so if anybodies interested change the lines after if (finds.length>0){ to this

     

      if (finds.length>0){
        cc=0
    for (d=0;d<langs[c].name.length;d++) {
        cc+=langs[c].name.charCodeAt(d)*235;
        }
        
        r=cc % 255;
        g=(5*(13*cc-7*r)) % 255;
        b=(8*(9*cc-5*r-3*g) )% 255;
    

     

    Message was edited by: ~ Trevor ~

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 30, 2012 9:11 AM   in reply to Trevorׅ

    Instead of

     

    langs=myDoc.languages

     

    try

     

     

    langs=myDoc.languages.everyItem().getElements()

     

    which might speed things up on long documents.

     

    Peter

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 30, 2012 10:50 AM   in reply to Peter Kahrel

    Hi Peter,

     

    I'm surprised by your answer, can't see why that should make a difference.

    On testing, it does  seem to make a bit of a difference but it's hard to tell.

    But I found the REAL bug "for (c=0;c<langs.length;c++){" should become

    "for (c=0;c<langs.length-1;c++){"

    Quite a silly mistake

     

    The script works really quite nicely now

    (If one doesn't like the color of your favorite languages then experiment with the r g b formula)

     

    Regards

     

    Trevor


    myDoc=app.documents[0];
    langs=myDoc.languages.everyItem().getElements();
    con=[];
    for (c=0;c<langs.length-1;c++){
        app.changeGrepPreferences = app.findGrepPreferences = null;
        app.findGrepPreferences.appliedLanguage=langs[c];
        finds=new Array;
        finds=myDoc.findGrep();
        if (finds.length>0){
        cc=0
    for (d=0;d<langs[c].name.length;d++) {
        cc+=langs[c].name.charCodeAt(d)*235;
        }
        
        r=cc % 255;
        g=(5*(13*cc-7*r)) % 255;
        b=(8*(9*cc-5*r-3*g) )% 255;
        
        con[c] = (app.activeDocument.conditions.item(langs[c].name).isValid) 
        ? app.activeDocument.conditions.item(langs[c].name) 
        : app.activeDocument.conditions.add
        ({name: langs[c].name, indicatorColor: [r,g,b], indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
     
        app.changeGrepPreferences.appliedConditions = [con[c]];
        finds=myDoc.changeGrep();
    }
    }
    app.changeGrepPreferences = app.findGrepPreferences = null;
    

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 30, 2012 11:18 AM   in reply to Trevorׅ

    Trevor,

     

    In your original script, every time c < langs.length is evaluated, the collection myDoc.languages is evaluated too. When you use langs = myDoc.languages.everyItem().getElements(), you create an array and that line is not evaluated later. So the timing difference is caused by InDesign accessing its model repeatedly (your original) or not (the everyItem() route).

     

    Peter

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 30, 2012 11:29 AM   in reply to Peter Kahrel

    Peter

     

    Thanks for the explanation, very clear. Good to learn some good practices.

     

    Trevor

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 30, 2012 3:22 PM   in reply to Trevorׅ

    ~ Trevor ~ wrote:

     

    But I found the REAL bug "for (c=0;c<langs.length;c++){" should become

    "for (c=0;c<langs.length-1;c++){"

    Quite a silly mistake

     

    The script works really quite nicely now

    Oh dear, oh dear, oh dear!

    Now I feel really silly, the "real" bug was not a bug and

    "for (c=0;c<langs.length;c++){" was correct.

    The -1 took off English: United States (don't know what's the difference between that and English: USA, as a Brit I never looked into it.)

    the -1 did make a difference, my computer was feeling moody and getting caught up with the splwow64.exe thread.

     

    Anyway It seems to have cheered up and now come to terms with the American independence.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 1:54 AM   in reply to Vamitul

    @Vamitul – about "everyItem().getElements()" there are two blog posts by Marc Autret at www.indiscripts.com:

     

    http://www.indiscripts.com/post/2010/06/on-everyitem-part-1

     

    http://www.indiscripts.com/post/2010/07/on-everyitem-part-2

     

    Uwe

     

    Message was edited by: Laubender

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 2:28 AM   in reply to Vamitul

    > but i can't find it

     

    That script isn't useful for your purposes (but it's here: http://www.kahrel.plus.com/indesign/goto_line.jsx just in case you want to convince yourself). All you need is a page number and/or a line number on that page. In CS5 and later, getting page numbers of found items is easy. Line numbers are messier:

     

    myFinds = myDoc.findGrep();

    for (i = 0; i < myFinds.length; i++)

       {

       $.write ('Found ' myFinds[i].contents + ' on page ' + myFinds[i].parentTextFrames[0].parentPage.name);

       $.writeln ('at line ' + myFinds[i].parentTextFrames[0].insertionPoints.itemByRange(0, myFinds[i].index).lines.length);

       }

     

    This can be formulated more efficiently, no doubt, but you get the picture. Naturally, this makes sense only in one-frame pages such as novels -- footnotes and tables aren't catered for.

     

    Peter

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 2, 2012 10:02 AM   in reply to Peter Kahrel
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points