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

Code to be evaluated! [010] Hyperlinks! …

LEGEND ,
Dec 11, 2016 Dec 11, 2016

Copy link to clipboard

Copied

Hi Scripters!

A code with a very strange behavior! [for me, of course!!  ]

It's about a glossary!

On the double page below, the glossary text is on the 2nd page, the current text on the 1rst one.

The deal is to find in the current text all the occurrences of each word or words group we have in red in the glossary [grep style // the word(s), a " – " and its definition/explanation], see paras in blue. And finally create hyperlinks on all these words with the glossary as target!

Capture d’écran 2016-12-12 à 00.24.45.png

When I launch the script, all seems to go right: the script finds the 4 words groups in the current text and mark them! …

But, as indicated in the code, the alert "alert(mySources.length)" gives "zero" at each loop, even if the script finds and treats something!

Capture d’écran 2016-12-12 à 00.24.12.png

Really no idea! 

As usual, many thanks for your help!

app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Glossary! …"); 

 

function main() 

{

var myDoc = app.activeDocument;

var myCondition = myDoc.conditions.item("GlossaryDone");

if (!myCondition.isValid)  myCondition = myDoc.conditions.add ({name: "GlossaryDone", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.appliedCharacterStyle = "Glossary";

app.changeGrepPreferences.appliedConditions = [myCondition];

myDoc.changeGrep();

var myAnchors = myDoc.findGrep();

for (var A = 0; A < myAnchors.length; A++)

    {

        var myAnchor = myAnchors;

        myDestination = myDoc.hyperlinkTextDestinations.add(myAnchor, {name: myAnchor.contents});

        app.findGrepPreferences = app.changeGrepPreferences = null;

        app.findGrepPreferences.findWhat = "(?i)\\b" + myAnchor.contents + "s?\\b";

        app.findGrepPreferences.appliedConditions = [];

        app.changeGrepPreferences.appliedConditions = [myCondition];

        myDoc.changeGrep();

       

        var mySources = myDoc.findGrep();

       

        alert(mySources.length) // -----------------------------------  VERY WEIRD!!!  mySources.length systematically = 0!  😞  So, no hyperlinks created!!!

       

        for (var S = 0; S < mySources.length; S++)

            {

                var mySource = myDoc.hyperlinkTextSources.add(mySources, {name: "Source_" + S}); 

                var myHyperlink = myDoc.hyperlinks.add(mySource, myDestination, {name: "Hyperlink_" + myDestination.name + "_" + S}); 

            } 

    }

}

(^/)  [ … a Jedi very tired tonight! ]

TOPICS
Scripting

Views

225

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
LEGEND ,
Dec 12, 2016 Dec 12, 2016

Copy link to clipboard

Copied

… To try to help me, here's the "test" InDesign file [CC 2015]! 

Dropbox - Glossary_Test-AF.indd.zip

(^/)

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
LEGEND ,
Dec 12, 2016 Dec 12, 2016

Copy link to clipboard

Copied

LATEST

Fixed! 

(^/)

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