//Hello! This script will take any selected text and make it a hyperlink, with the associated Hyperlink character style. It also detects whether the selected text is an email, in which case it makes it a mailto link, and can also add "http://" if the url doesn't already have it. Obviously, this doesn't work if the text you've selected isn't already a url. It's not magic. //Useful for: Probably only ebooks, but maybe PDFs or anything that needs a live link embedded into the document? //Tell indesign what .indexOf() is, because it's old and confused. Array.prototype.indexOf = function ( item ) { var index = 0, length = this.length; for ( ; index < length; index++ ) { if ( this[index] === item ) return index; } return -1; }; //Check if there's a hyperlink swatch already, and if not create one try { var hyperlinkBlue = app.activeDocument.colors.add(); hyperlinkBlue.name = "Hyperlink"; hyperlinkBlue.model = ColorModel.PROCESS; hyperlinkBlue.space = ColorSpace.CMYK; hyperlinkBlue.colorValue = [86, 57, 0, 16]; } catch (e) { } //Check if there's a hyperlink character style already, and if not create one try { newstyle = app.activeDocument.characterStyles.add({ name:"Hyperlink", underline: true, fillColor: app.activeDocument.swatches.itemByName("Hyperlink") }); } catch (e) { } //Create the hyperlink from selection function createHyperLink() { var source = app.activeDocument.hyperlinkTextSources.add(app.activeDocument.selection[0]); if (app.activeDocument.selection[0].contents.indexOf("@") >= 0) { var destination = app.activeDocument.hyperlinkURLDestinations.add(String("mailto:" + app.activeDocument.selection[0].contents)); } else if (app.activeDocument.selection[0].contents.indexOf("http://") < 0 && app.activeDocument.selection[0].contents.indexOf("https://") < 0) { var destination = app.activeDocument.hyperlinkURLDestinations.add(String("http://" + app.activeDocument.selection[0].contents)); } else { var destination = app.activeDocument.hyperlinkURLDestinations.add(app.activeDocument.selection[0].contents); } var hyperlink = app.activeDocument.hyperlinks.add(source, destination); var hyperlinkNumber = 0; var named = false; while (!named){ try { if (hyperlinkNumber == 0){ hyperlink.name = app.activeDocument.selection[0].contents; named = true; } else { hyperlink.name = app.activeDocument.selection[0].contents + " " + hyperlinkNumber; named = true; } } catch (e) { hyperlinkNumber = hyperlinkNumber + 1; named = false; } } var hyperlinkStyle = app.activeDocument.characterStyles.itemByName("Hyperlink"); app.activeDocument.selection[0].applyCharacterStyle(app.activeDocument.characterStyles.itemByName("Hyperlink")); } try { createHyperLink(); } catch (e) { alert("That didn't work. Are you sure you've got some text selected?"); }