Copy link to clipboard
Copied
Hello,
I have a catlog in which I have been tasked with writing a script to turn all ISBNs into hyperlinks to products on our website. I've searched through a number of similar posts, but none of the solutions suggested have worked for me. I'm a web developer, so am familiar with writing JavaScript for the web, but since the InDesign API is different enough from a web browser, I'm struggling with writing my own script for this as well.
Things to note:
1. Most of the ISBNs I care about are found in tables.
2. The ISBNs do not currently have character styles and I don't have time to apply them at this point (unless I can write a script for that as well).
3. The ISBN pattern is 123-1-1234-1234-1 (example: 978-1-4966-8092-1).
Thank you!
Hi lensera,
indeed, a first step could be to apply a character style to all ISBN coded texts. You could use InDesign's GREP Find/Change for this task very easily. Translating your pattern to a GREP for the find part could be:
\d{3}-\d-\d{4}-\d{4}-\d
If you are sure that the ISBN is always at the end of a paragraph you could add a $ as well:
\d{3}-\d-\d{4}-\d{4}-\d$
But a character style is not essential to your task if the GREP pattern is able to find every instance of an ISBN text.
Wh
...Copy link to clipboard
Copied
Hi lensera,
indeed, a first step could be to apply a character style to all ISBN coded texts. You could use InDesign's GREP Find/Change for this task very easily. Translating your pattern to a GREP for the find part could be:
\d{3}-\d-\d{4}-\d{4}-\d
If you are sure that the ISBN is always at the end of a paragraph you could add a $ as well:
\d{3}-\d-\d{4}-\d{4}-\d$
But a character style is not essential to your task if the GREP pattern is able to find every instance of an ISBN text.
What can be important: Is there always a white space character in front of the code? Are there other circumstances ( patterns ) we can use to identify surrounding text?
If a pattern is found the next step would be to add a hyperlink text source, a hyperlink URL destination and finally a hyperlink to the document for every found text. I have no idea regarding the URLs you have to use. Is there a list, a text file perhaps, where the URLs are stored for a corresponding ISBN code? Or how do you "calculate" a URL from a given ISBN code?
Basic code using the find query from above and adding a hyperlink is this:
// An array for found texts:
var resultTextArray = [];
// The target, the scope of this all, is the active document:
var doc = app.documents[0];
// Clear some preferences first:
app.findGrepPreferences = null ;
app.changeGrepPreferences = null ;
// Define the GREP pattern
// NOTE: Escape the backslashes!
app.findGrepPreferences.findWhat = "\\d{3}-\\d-\\d{4}-\\d{4}-\\d" ;
// All the results, texts, will be stored in this array:
resultTextArray = doc.findGrep();
// Loop the result array and add hyperlinks:
for( var n=0; n<resultTextArray.length; n++ )
{
// Add function to add a hyperlink here.
// [1] We need a text source:
var textSource = doc.hyperlinkTextSources.add
(
{
sourceText : resultTextArray[n]
}
);
// [2] We need a destination:
// IMPORTANT: If you do not want to add a shared destination use value true for property hidden!
var urlDestination = doc.hyperlinkURLDestinations.add
(
{
destinationURL : "https://www.adobe.com" ,
hidden : true
}
);
// ONLY THEN WE FINALLY CAN ADD A HYPERLINK:
// Add a hyperlink using source and destination and other properties:
var hyperlink = doc.hyperlinks.add
(
{
source : textSource ,
destination : urlDestination ,
borderStyle : HyperlinkAppearanceStyle.SOLID ,
highlight : HyperlinkAppearanceHighlight.OUTLINE ,
hidden : false ,
visible : true
}
);
};
As I already said: I cannot tell where you get your URL strings from.
And you could define other properties as well. Like an applied character style to the source texts or other visual formatting of the hyperlinks.
Consult InDesign DOM documentation for more details:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Hyperlink.html#d1e112792
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#HyperlinkTextSource.html
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#HyperlinkURLDestination.html
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Thanks, Uwe! I must not have been very clear about the contents of the actual hyperlinks I'll be using. Essentially, I want to find the ISBN, store it as a variable, append that variable to the end of a URL (in my case, it's https://www.capstonepub.com/library/search/?Keyword=) and then replace the text of the ISBN with a hyperlink to the newly created URL. Does that make more sense?
I'll try out your script suggestion and let you know what I find. Thanks again!
Copy link to clipboard
Copied
If you would like to use the ISBN text at the end of a basie URL you could do a string like that:
var baseURLstring = "https://adobe.com/";
var isbnTextContents = resultTextArray[n].contents;
var destinationURLstring = baseURLstring + isbnTextContents;
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Uwe!
Thanks again for your help with my question. I think one of the hardest parts about scripting for something like InDesign is I don't know how to debug it (there's no Developer Tools built in that I know of, and the ExtendScript program isn't configured for use on my OS). So, after running your script, I recieved this error:
Error Number: 79111
Error String: The object you have chosen is already in use by another hyperlink.
Source: var textSource = doc.hyperlinkTextSources.add
I'll keep trying my best to work with this, but like I said, it's hard to debug, so any mor help you can offer would be appreciated!
Regards,
Rachel Lense
EDIT:
Nevernmind! It looks like the script worked, but there wasn't any visual indicator that the text turned into a URL, so I didn't know it worked. Thank you, Uwe!
Copy link to clipboard
Copied
Hi Rachel,
thank you for your feedback. Hm. If you tried my script code there is indeed a visual representation of the hyperlink text source in your document.Through properties borderStyle and highlight of the defined source. The reason why you cannot see anything of it could be that you are viewing the InDesign pages not in Normal mode, but perhaps with Overprint Preview turned on. Below a couple of screenshots from my test document before I executed the script, after I executed the script and after I switched from Normal view to e.g. Preview:
Before running my script:
Watch the Hyperlinks panel, after running my script:
The hyperlink text sources in the text are surrounded by a black stroke that does not print.
The strokes are not visible anymore after I changed the view on the page from "Normal" to "Preview":
Could also be that the view of Hyperlinks in the View > Extras menu is disabled.
Debugging of ExtendScript code can be done with the Adobe ExtendScript Toolkit app (ESTK). That is working very well on Windows. On Mac OS X it is difficult, because the ESTK is 32-bit software that cannot run on the latest OS X and the last versions of the ESTK have a serious bug that makes it hard to run it also on Mac OS X version that allow 32-bit software. The alternative to the ESTK is a special plug-in to Visual Studio Code available for Windows and Mac OS X:
The bug I mentioned with the ESTK on Mac OS X can be worked around or "fixed":
https://medium.com/adobetech/workaround-for-extendscript-toolkit-debugger-error-1116-f067f81f96c6
FWIW: The reason why you see an error when trying to run the script:
You may have done one or more hyperlinks with the exact same properties before.
And you cannot do the same hyperlink two times.
Regards,
Uwe Laubender
( ACP )