Skip to main content
Obi-wan Kenobi
Legend
December 11, 2016
Question

Code to be evaluated! [010] Hyperlinks! …

  • December 11, 2016
  • 1 reply
  • 284 views

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!

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!

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! ]

This topic has been closed for replies.

1 reply

Obi-wan Kenobi
Legend
December 12, 2016

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

Dropbox - Glossary_Test-AF.indd.zip

(^/)

Obi-wan Kenobi
Legend
December 12, 2016

Fixed! 

(^/)