Skip to main content
Obi-wan Kenobi
Legend
December 12, 2016
Answered

Code to be evaluated! [011] Hyperlinks to be removed! …

  • December 12, 2016
  • 1 reply
  • 356 views

Hi Scripters,

Another issue about hyperlinks!

As shown in the screenshot below, destinations are missing! So, the deal is to remove the concerned hyperlinks!

I wrote this code but it doesn't work! 

var myDoc = app.activeDocument;

var myHyperlinks = myDoc.hyperlinks.everyItem().getElements();

for ( H = 0; H < myHyperlinks.length; H++ )

    {

        var myDestination = myHyperlinks.HyperlinkTextDestination.destinationText;

        if ( !myDestination.isvalid )  myHyperlinks.remove();

    }

Thanks in advance for your help!

(^/)

This topic has been closed for replies.
Correct answer Loic.Aigon

Hi Obi,

If you are looking for improvements, you may want to :

1) Include you code into a function to reduce global scope variables. The more global variables you have the slower your script will go.

2) Check object references validity. your script may throw an error if the script is run without any open document.

3) Use a reference to the array length instead of reading the property every loop

4) Besides you do not need to transform the collection into an array here as you don't take advantage of any specific array only function such as sorting, slicing, concatening…

So you script could have been:

var main = function() {

  var d = app.properties.activeDocument,

  hs, h, ds, n = 0;

  if ( !d) return;

  hs = d.hyperlinks, n = hs.length; 

  while ( n-- )  {

  h = hs;

  ds = h.properties.destination;

  !ds && h.remove();

  }

}

main();

HTH

Loic

http://www.ozalto.com/

1 reply

Obi-wan Kenobi
Legend
December 12, 2016

Fixed! 

var myDoc = app.activeDocument;

var myHyperlinks = myDoc.hyperlinks.everyItem().getElements();

for ( H = 0; H < myHyperlinks.length; H++ )

    {

        var myDestination = myHyperlinks.destination;

        if ( myDestination == null )  myHyperlinks.remove();

    }

(^/)

Loic.Aigon
Loic.AigonCorrect answer
Legend
December 13, 2016

Hi Obi,

If you are looking for improvements, you may want to :

1) Include you code into a function to reduce global scope variables. The more global variables you have the slower your script will go.

2) Check object references validity. your script may throw an error if the script is run without any open document.

3) Use a reference to the array length instead of reading the property every loop

4) Besides you do not need to transform the collection into an array here as you don't take advantage of any specific array only function such as sorting, slicing, concatening…

So you script could have been:

var main = function() {

  var d = app.properties.activeDocument,

  hs, h, ds, n = 0;

  if ( !d) return;

  hs = d.hyperlinks, n = hs.length; 

  while ( n-- )  {

  h = hs;

  ds = h.properties.destination;

  !ds && h.remove();

  }

}

main();

HTH

Loic

http://www.ozalto.com/

Obi-wan Kenobi
Legend
December 13, 2016

Hi Loïc,

Nice code and interesting writing! 

Even if mine works, I indicate it as "correct"! Sample to be followed!

I take it! Thanks!

(^/)