Skip to main content
Liphou
Inspiring
February 4, 2021
Answered

[JS] test selection if hyperlink?

  • February 4, 2021
  • 2 replies
  • 538 views

Hello,

I try to test if a selection or a result of a search is a hyperlink?

Thank you

Philou

/------------------------------

Bonjour,

je cherche à test si une selection ou un resultat d'une recheche est un hyperlien ?

Merci  à vous

Philou

This topic has been closed for replies.
Correct answer Manan Joshi

Try the following

if(app.selection[0].findHyperlinks () != "")
 alert("Is a hyperlink")
else
 alert("Not a hyperlink")

-Manan 

2 replies

Community Expert
February 4, 2021

Hi Philou,

do you mean selected text? Or found text? Then work on with Manan's suggestion.

The result of findHyperlinks() is an array of found hyperlinks.

 

However, you have to be careful what exactly is selected or found.

I did not test that, but I could imagine that the same hyperlink can be found more than one time.

Why? Because findHyperlinks() is not a method of superclass Texts, but comes with:

Character | Word | Line | TextColumn | Paragraph | TextStyleRange | InsertionPoint

 

Certainly this point is worth to explore…

 

Regards,
Uwe Laubender

( ACP )

Liphou
LiphouAuthor
Inspiring
February 4, 2021

Thank you to all of you,


my problem was the following in a document with underlined text (with 2690 hyperlink) so sertaint was just signed.


I assume that the hyperLink information has been lost.

I had selected the underlined text and checked if the text was indeed a link. I actually came across some "findHyperlinks" command, after some research, Manan confirms the command. Thank you.

here is the script. simple and with probably a lot of things changed ... (a little in a hurry)

---------

Merci à vous tous,

mon probmème était le suivant dans un document avec du texte souligné (avec 2690 hyperlink) donc sertaint etait juste soiligné. je supose que les information d'hyperLink ont été perdu.

Je davais selectioné le textes souligné et verifié si se texte était bien un lien.

Je suis effectivement tombé sur certte commende "findHyperlinks", après quelque recherche, Manan confirme la commade Merci.

voici le script. simple et avec probalement beaucoup de chose à changé (un peu dans l'urgence)

var leDoc = app.activeDocument;

leMain();


function leMain() {

app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; // mise à zero du Find
with (app.findGrepPreferences){

underline = true;
} // fin

var lesRecherche = leDoc.findGrep(true); // True??
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

if (lesRecherche.length == 0) ErrorExit("Pas de resulta de la recherche.", true);
for (var f = 0; f < lesRecherche.length; f++) {
var laRecherche = lesRecherche[f];
try {
var lelien = laRecherche.findHyperlinks()[0]; // possition ou insertion
}

catch (erreur)

{

if(laRecherche.underline == true){
//$.writeln('yes');
laRecherche.underline = true;
laRecherche.underlineColor = app.activeDocument.colors.item("erreur_Lien"); //corr
laRecherche.underlineGapColor = app.activeDocument.swatches.item("None");
laRecherche.underlineGapOverprint = false;
laRecherche.underlineGapTint = 100;
laRecherche.underlineOffset = -3;
laRecherche.underlineOverprint = false;
laRecherche.underlineTint = 100;
laRecherche.underlineWeight = 10;
}
}
}
}

Merci

Liphou
LiphouAuthor
Inspiring
February 4, 2021
 try {

    app.panels.item('$ID/Hyperlinks').visible = false;

  } catch (_) {

  }
//-----------------------------------
// variabnle general

  var leDoc = app.activeDocument;
  
  
  // ICI pour creation de varaible decouleur et de style de caractaires
//----------------------------------

//surFichier ();
//surLivre ();

leMain();


function leMain() { 
      
        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;  // mise à zero du Find
        
        
        with (app.findGrepPreferences){
                        
                        underline = true;
                } // fin 
        
        var lesRecherche = leDoc.findGrep(true);  // True??
        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;  
      
        if (lesRecherche.length == 0) ErrorExit("Pas de resulta de la recherche.", true);  
        

        
        for (var f = 0; f < lesRecherche.length; f++) {  
           var laRecherche = lesRecherche[f];
           try {

            var lelien = laRecherche.findHyperlinks()[0]; // possition ou insertion
           
            } 
        
        catch (erreur) 
        
        {
        
            //$.writeln (erreur);
        
           // $.writeln ('n\'est pas un lien');
           if(laRecherche.underline == true){
              //$.writeln('yes');
              laRecherche.underline = true;
              laRecherche.underlineColor = app.activeDocument.colors.item("erreur_Lien"); //corr
              laRecherche.underlineGapColor = app.activeDocument.swatches.item("None");
              laRecherche.underlineGapOverprint = false;
              laRecherche.underlineGapTint = 100;
              laRecherche.underlineOffset = -3;
              laRecherche.underlineOverprint = false;
              laRecherche.underlineTint = 100;
              laRecherche.underlineWeight = 10;
              }
              
            }
        }  

       
    }  //
Manan JoshiCommunity ExpertCorrect answer
Community Expert
February 4, 2021

Try the following

if(app.selection[0].findHyperlinks () != "")
 alert("Is a hyperlink")
else
 alert("Not a hyperlink")

-Manan 

-Manan
FRIdNGE
February 4, 2021

Hi Manan,

 

That only means there's an (or more) hyperlink(s) in the selection!  😉

 

(^/)  The Jedi

Community Expert
February 4, 2021

Isn't that what the OP wanted to know? This is what I understood and tried to answer 😛

-Manan

-Manan