Highlighted

Script to find a specific word/string and create a hyperlink?

Community Beginner ,
Oct 21, 2020

Copy link to clipboard

Copied

Hi all,

For the scripting pros out there, this should be a straightforward request. I'm surprised I didn't find a script somewhere that could do what I need it to do after searching over the last hour!

 

The problem:

I have a set of documents that all have the same paragraph in it. This paragraph has two different links. For simplicity, let's use filler Latin:

"Lorem ipsum dolor sit amet."

Both ipsum and dolor need be hyperlinks, the first going to www.ipsum.org and the second going to www.randomsite.com

I'd like a script that searches for instances of "ipsum", links it to www.ipsum.org and then searches for "dolor" and links it to www.randomsite.com. Bonus: if it can ignore text styles and simply put an underline on it (without changing anything else), that'd be even better.

 

Because copy/paste doesn't work with hyperlinks between InDesign documents,* I typically need to manually create the link each time. Rather, I'd like to have something akin to a Find/Replace function.

I saw a script out there on the web that will create URLs based on the matching text (mytext becomes www.mytext.com for instance) which happens to solve for "ipsum," but the hyperlink attached to "dolor" doesn't include "dolor" in the URL.

I'm not necessarily looking for a script that creates an interface like Find/Replace - all of the documents I work on have this paragraph with "ipsum" and "dolor" in it, and they will always go to the websites provided above.

 

Thank you!

 

*Typically, I have to create links manually in each document, even if it's the same exact thing as what's in another document, because copying and pasting creates links to the source document, not to the actual website. Completely unhelpful, in my opinion.

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

Here ya go. 

 

 

var searches = [
    ["ipsum", "www.ipsum.org"],
    ["dolor", "www.randomsite.org"],
];
var docs = app.documents.everyItem().getElements();
var doc, finds, hl, hlts, hld;
for (var d = 0; d < docs.length; d++) {
    doc = docs[d];
    for (var i = 0; i < searches.length; i++) {
        app.findTextPreferences.findWhat = searches[i][0];
        finds = doc.findText();
        try {
            hld = doc.hyperlinkURLDestinations.add(searches[i][1]);
            hld.name = searches[i][1];
        } catch(e) {
            hld = doc.hyperlinkURLDestinations.itemByName(searches[i][1]);
        }
        for (var j = 0; j < finds.length; j++) {
            try {
                hlts = doc.hyperlinkTextSources.add(finds[j]);
            } catch(e) {
                finds[j].findHyperlinks()[0].remove();
                hlts = doc.hyperlinkTextSources.add(finds[j]);
            }
            hl = doc.hyperlinks.add(hlts, hld);
            finds[j].underline = true;
        }
        app.findTextPreferences = NothingEnum.NOTHING;
    }
    
}
app.findTextPreferences = NothingEnum.NOTHING;

 

 

 

 

TOPICS
Scripting, Type

Views

80

Likes

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

Script to find a specific word/string and create a hyperlink?

Community Beginner ,
Oct 21, 2020

Copy link to clipboard

Copied

Hi all,

For the scripting pros out there, this should be a straightforward request. I'm surprised I didn't find a script somewhere that could do what I need it to do after searching over the last hour!

 

The problem:

I have a set of documents that all have the same paragraph in it. This paragraph has two different links. For simplicity, let's use filler Latin:

"Lorem ipsum dolor sit amet."

Both ipsum and dolor need be hyperlinks, the first going to www.ipsum.org and the second going to www.randomsite.com

I'd like a script that searches for instances of "ipsum", links it to www.ipsum.org and then searches for "dolor" and links it to www.randomsite.com. Bonus: if it can ignore text styles and simply put an underline on it (without changing anything else), that'd be even better.

 

Because copy/paste doesn't work with hyperlinks between InDesign documents,* I typically need to manually create the link each time. Rather, I'd like to have something akin to a Find/Replace function.

I saw a script out there on the web that will create URLs based on the matching text (mytext becomes www.mytext.com for instance) which happens to solve for "ipsum," but the hyperlink attached to "dolor" doesn't include "dolor" in the URL.

I'm not necessarily looking for a script that creates an interface like Find/Replace - all of the documents I work on have this paragraph with "ipsum" and "dolor" in it, and they will always go to the websites provided above.

 

Thank you!

 

*Typically, I have to create links manually in each document, even if it's the same exact thing as what's in another document, because copying and pasting creates links to the source document, not to the actual website. Completely unhelpful, in my opinion.

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

Here ya go. 

 

 

var searches = [
    ["ipsum", "www.ipsum.org"],
    ["dolor", "www.randomsite.org"],
];
var docs = app.documents.everyItem().getElements();
var doc, finds, hl, hlts, hld;
for (var d = 0; d < docs.length; d++) {
    doc = docs[d];
    for (var i = 0; i < searches.length; i++) {
        app.findTextPreferences.findWhat = searches[i][0];
        finds = doc.findText();
        try {
            hld = doc.hyperlinkURLDestinations.add(searches[i][1]);
            hld.name = searches[i][1];
        } catch(e) {
            hld = doc.hyperlinkURLDestinations.itemByName(searches[i][1]);
        }
        for (var j = 0; j < finds.length; j++) {
            try {
                hlts = doc.hyperlinkTextSources.add(finds[j]);
            } catch(e) {
                finds[j].findHyperlinks()[0].remove();
                hlts = doc.hyperlinkTextSources.add(finds[j]);
            }
            hl = doc.hyperlinks.add(hlts, hld);
            finds[j].underline = true;
        }
        app.findTextPreferences = NothingEnum.NOTHING;
    }
    
}
app.findTextPreferences = NothingEnum.NOTHING;

 

 

 

 

TOPICS
Scripting, Type

Views

81

Likes

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
Oct 21, 2020 0
Adobe Community Professional ,
Oct 22, 2020

Copy link to clipboard

Copied

Here ya go. 

 

 

var searches = [
    ["ipsum", "www.ipsum.org"],
    ["dolor", "www.randomsite.org"],
];
var docs = app.documents.everyItem().getElements();
var doc, finds, hl, hlts, hld;
for (var d = 0; d < docs.length; d++) {
    doc = docs[d];
    for (var i = 0; i < searches.length; i++) {
        app.findTextPreferences.findWhat = searches[i][0];
        finds = doc.findText();
        try {
            hld = doc.hyperlinkURLDestinations.add(searches[i][1]);
            hld.name = searches[i][1];
        } catch(e) {
            hld = doc.hyperlinkURLDestinations.itemByName(searches[i][1]);
        }
        for (var j = 0; j < finds.length; j++) {
            try {
                hlts = doc.hyperlinkTextSources.add(finds[j]);
            } catch(e) {
                finds[j].findHyperlinks()[0].remove();
                hlts = doc.hyperlinkTextSources.add(finds[j]);
            }
            hl = doc.hyperlinks.add(hlts, hld);
            finds[j].underline = true;
        }
        app.findTextPreferences = NothingEnum.NOTHING;
    }
    
}
app.findTextPreferences = NothingEnum.NOTHING;

 

 

 

 

Likes

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
Reply
Loading...
Oct 22, 2020 3
Community Beginner ,
Oct 23, 2020

Copy link to clipboard

Copied

Thank you so much! Super straightforward to work with, too – the searches variable at top makes it easy.

I wonder if this is too much to ask being this goes beyond what my original ask contained, but is there any way to have this work for all open documents? What would need to be tweaked?

Likes

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
Reply
Loading...
Oct 23, 2020 0
Adobe Community Professional ,
Oct 23, 2020

Copy link to clipboard

Copied

There's probably a more efficient way, but I updated the original post to handle. 

Likes

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
Reply
Loading...
Oct 23, 2020 1
Community Beginner ,
Oct 23, 2020

Copy link to clipboard

Copied

Many thanks!

Likes

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
Reply
Loading...
Oct 23, 2020 0