• 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! [011] Hyperlinks to be removed! …

LEGEND ,
Dec 12, 2016 Dec 12, 2016

Copy link to clipboard

Copied

Hi Scripters,

Another issue about hyperlinks!

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

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

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!

(^/)

TOPICS
Scripting

Views

258

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

correct answers 1 Correct answer

People's Champ , Dec 13, 2016 Dec 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 an

...

Votes

Translate

Translate
LEGEND ,
Dec 12, 2016 Dec 12, 2016

Copy link to clipboard

Copied

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();

    }

(^/)

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
People's Champ ,
Dec 13, 2016 Dec 13, 2016

Copy link to clipboard

Copied

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/

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 13, 2016 Dec 13, 2016

Copy link to clipboard

Copied

LATEST

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!

(^/)

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