Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Replacing occurrences of a word by links

New Here ,
Jul 16, 2010 Jul 16, 2010

Hello,

I'm trying to write a Javascript script that would replace every occurrences of a given word in an Indesign CS3 document by a hyperlink pointing to a given URL.

I'm very new to Indesign scripting, and I don't know where to start. I've tried searching this forum, but couldn't find the answer to my problem.

If anyone can help...

Thanks,

Bertrand

TOPICS
Scripting
2.0K
Translate
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

Community Expert , Jul 16, 2010 Jul 16, 2010

You could use my findAndDo Script (blog is in german): http://www.indesignblog.com/?p=7

for a hyperlink you need to change it:

app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "SEARCH";
var erg = app.activeDocument.findGrep ();     

for (var i = erg.length - 1; i >= 0; i--) {
    var _hlinkSource = app.activeDocument.hyperlinkTextSources.add(erg);
    var _destination = app.activeDocument.hyperlinkURLDestinations.add("http

...
Translate
Community Expert ,
Jul 16, 2010 Jul 16, 2010

You could use my findAndDo Script (blog is in german): http://www.indesignblog.com/?p=7

for a hyperlink you need to change it:

app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "SEARCH";
var erg = app.activeDocument.findGrep ();     

for (var i = erg.length - 1; i >= 0; i--) {
    var _hlinkSource = app.activeDocument.hyperlinkTextSources.add(erg);
    var _destination = app.activeDocument.hyperlinkURLDestinations.add("http://www.google.de");
    app.activeDocument.hyperlinks.add(_hlinkSource, _destination);
}

Translate
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
New Here ,
Jul 16, 2010 Jul 16, 2010

Thank you very much Grefel! Your script works perfectly and creates hyperlinks for all the occurrences of my word.

However there are two more things I need : I'd like the hyperlink object to be invisible, and the text occurrence to appear in blue.

I figured I could make the links invisible by using the line

app.activeDocument.hyperlinks.add(_hlinkSource, _destination).visible=false;

But I'm not sure how I can make the text turn blue. I suppose I have to use apply some function to erg within the loop, is that right?

Translate
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
Community Expert ,
Jul 16, 2010 Jul 16, 2010

Best is to assign a character style on the links. Make one named "link", and specify your color in there. Then in the script, add this:

erg.appliedCharacterStyle = app.activeDocuments.characterStyles.item("link");

Translate
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
New Here ,
Jul 16, 2010 Jul 16, 2010

Excellent idea, thank you very much for all your help.

Translate
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
New Here ,
Aug 16, 2010 Aug 16, 2010

One problem with that script is that you can't run it a second time to update the URLs of the hyperlinks. If I do I get a message:

The object you have chosen is already in use by another hyperlink

It seems like the error is caused by the following line:

var _hlinkSource = app.activeDocument.hyperlinkTextSources.add(erg);

Is there a way to prevent that? For example, can the existing HyperlinkTextSource be removed? I couldn't find the existing  HyperlinkTextSource using app.activeDocument.hyperlinkTextSources.findByID( ), because I don't know its ID.

Is there a way to remove or update the hyperlink knowing just erg?

Translate
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
Community Expert ,
Aug 16, 2010 Aug 16, 2010
LATEST

hey,

mmh, this looks more complex then expected. anyone out there who knows how to reference from a text object to a hyperlinkTextSource ???

you can solve it dirty this way:

app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "SEARCH";
var erg = app.activeDocument.findGrep ();   

for (var i = erg.length - 1; i >= 0; i--) {
   
    // check for existing textSources:
    var _hlinkSource = null;
    var _allHlts = app.activeDocument.hyperlinkTextSources.everyItem().getElements();
    for (m =0; m < _allHlts.length; m++) {
        var _hlts = _allHlts;
        if (_hlts.sourceText === erg) {
            _hlinkSource = _hlts;
         }       
    }   
    // Only add if there is no textSource
    if (_hlinkSource == null ) _hlinkSource = app.activeDocument.hyperlinkTextSources.add(erg);


    var _url =  "http://de.wikipedia.org/wiki/" + erg.contents
    var _destination = app.activeDocument.hyperlinkURLDestinations.itemByName(_url);
    // check if the destination already exists
    if (_destination == null) app.activeDocument.hyperlinkURLDestinations.add(_url);
       
    var _hlink = app.activeDocument.hyperlinks.add(_hlinkSource);
    try {
        _hlink.name = _url;
    } catch (e) {}
    _hlink.destination = _destination;
}

this code looks pretty ugly, because it checks every hyperlinkTextSource against the found text object.

another feasible way could be to remove all

hyperlinkTextSources, hyperlinkURLDestinations, hyperlinks

and run the script again.

anyway: i hope someone else has a more elegant solution.

Translate
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