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.
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
...
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;
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?
Copy link to clipboard
Copied
There's probably a more efficient way, but I updated the original post to handle.
Copy link to clipboard
Copied
Many thanks!
Copy link to clipboard
Copied
So how do you apply this??? this will make my life so much easier!
Copy link to clipboard
Copied
It's definitely made my life much easier over the last couple months since I came here and got the answer!
I'll explain how to get it working. Not knowing your level of understanding, I'll make this beginner-friendly:
1. First, find the InDesign script folder on your computer. The easiest way to do this is to open InDesign, and in the Scripts panel (if not visible, go to Window > Utilities > Scripts), click the menu icon and then "Open in Finder" (Mac) or "Open in Explorer/folder" (or whatever Windows says)
2. Copy the script above and paste into a blank TextEdit/Notepad doc. At the top, you'll have to replace the filler with whatever you need. Where you see ["ipsum", "www.ipsum.org"], replace ipsum with the word that needs to have a hyperlink applied, and replace www.ipsum.org with the site that the link will to point to. It's important that you keep the structure the same: quotes, commas, brackets, etc. Add as many of these lines as you need within var searches = [] as you need, and don't forget a comma at the end of each line (except for the very last one).
3. Save the file into the scripts folder as something you'll remember, like autohyperlinker.jsx (If it tries to save as .txt, don't let it, it needs to be .jsx)
4. In the Scripts panel in InDesign, you should see your new jsx file. Whenever you need to use it, just double click it and it will apply the links in all the documents you have open. Verify that it worked by looking in the Hyperlinks panel (if not visible, go to Window > Interactive > Hyperlinks).
Hope that helps!