Copy link to clipboard
Copied
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
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
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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");
Copy link to clipboard
Copied
Excellent idea, thank you very much for all your help.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now