This sure needed a round trip through InDesign’s Document Object Model — hyperlinks, anchored objects, text frames .. the lot! But I think the following will get you started. The following script scans your document for hyperlinks, and creates a new anchored frame to its left on a fixed position. It fills the frame with a placeholder text — how and where would your abbreviations come from? That part wasn’t entirely clear, but perhaps you can fiddle with my script and add it where you wish.
The script assumes the following:
1. An Object Style called “RedBorder”. Please note: make sure this object style does *NOT* define any Anchored Object settings! These will override the ones that I put in my script (as I only found out after some serious head scratching).
2. A Paragraph Style called “RedCaps”.
3. Some texts marked with a hyperlink to an external URL.
4. The baseline of the new box will be aligned to the baseline of your text. I moved it down 1mm, but how much it needs to have the *text* inside it aligned depends (I think) on the font and size of the paragraph style RedCaps. I suppose it’s easiest to run the script once, move one of the anchored objects up or down until it aligns correctly, note down the vertical offset, and then discard your new document and run the script again, with the correct value inserted. I don’t think this step can be automated.
First off, let me show a *short* sample script to illustrate how Hyperlinks work in InDesign. A Hyperlink is an abstract item, only visible in the Hyperlinks palette, which has two important properties: where it *points to* (which can be anything: to another page, to a text location, or — the one you want — to an external URL), and what the *link* item is in your document (this can be a Cross Reference, any clickable Page Item such as a button, or some plain text). The following script changes the font of all of your *text* links that point to *URLs* to something else:
links = app.activeDocument.hyperlinks;
for (i=0; i<links.length; i++)
{
if (links[i].source instanceof HyperlinkTextSource &&
links[i].destination instanceof HyperlinkURLDestination)
links[i].source.sourceText.appliedFont = “Comic Sans”;
}
You see it needs to check the *type* of both source and destination, because not every ‘clickable’ item may have a font, and not every link may point to an URL.
The *sourceText* is where I am going to insert a new anchored object; then I set some properties, so your text can be inserted and the anchor is moved into position, and finally, the same *destination* is applied to this new object — not to its text as you requested, but — hey! — to the entire rectangle. Instant Hyperlink Buttons ahoy!
Here is the full script; after copying, take care to check it for unexpected modifications, such as curly quotes (they should all be straight) and en-dashes (these probably should be two hyphens). Also, make sure the object and paragraph styles exist before running (it won’t work otherwise). Hope it’s enough to get you started!
// Get list of links — the following forces a COPY of ID’s internal list
// That is important, because we’re going to ADD a lot of them :-/
links = app.activeDocument.hyperlinks.everyItem().getElements();
for (i=0; i<links.length; i++)
{
if (links[i].source instanceof HyperlinkTextSource &&
links[i].destination instanceof HyperlinkURLDestination)
{
r = links[i].source.sourceText.insertionPoints[0].textFrames.add();
// initially inline; convert to Anchored Object
r.anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED;
// set some Anchored Object properties
r.anchoredObjectSettings.horizontalReferencePoint = AnchoredRelativeTo.TEXT_FRAME;
r.anchoredObjectSettings.verticalReferencePoint = VerticallyRelativeTo.LINE_BASELINE;
r.anchoredObjectSettings.pinPosition = false;
r.anchoredObjectSettings.anchorXoffset = “5mm”;
r.anchoredObjectSettings.anchorYoffset = “1mm”; // <- Find proper value!
// apply object style. Make sure it *sets* no Anchored Style!
r.appliedObjectStyle = app.activeDocument.objectStyles.item(“RedBorder”);
// write some text into it
r.texts[0].contents = “AAA”;
r.texts[0].appliedParagraphStyle = “RedCaps”;
// make sure it has some space left and right
r.textFramePreferences.firstBaselineOffset = FirstBaseline.CAP_HEIGHT;
r.textFramePreferences.insetSpacing = [“1mm”, “3mm”,”1mm”,”3mm”];
// ..first make it *way* too large (hopefully)
r.geometricBounds = [0,0, “10mm”,”100mm”];
// .. then resize to fit
r.fit (FitOptions.FRAME_TO_CONTENT);
// make the entire rectangle a hyperlink, pointing to the same
// URL as the original one
newlink = app.activeDocument.hyperlinks.add(app.activeDocument.hyperlinkPageItemSources.add(r), links[i].destination);
newlink.visible = false;
}
}