Help Writing Script for Batch Find and Replace Hyperlink External Page Destinations
Hello ID Community,
What I'd like to accomplish in this post: help in creating a batch find-and-replace script to modify each hyperlink's *external page* destination from a former edition's folder path to a new edition's folder path. Again, please note that these hyperlinks in question reference pages in other ID documents, not urls.
I have a set of INDD files with hyperlinks that reference each other (essentially chapter buttons to hyperlink between specific documents) (Hyperlinks Panel > Link To: Page).
This set of documents ('chapters') are copied from a previous edition's folder into a new edition. The problem is: the new edition's documents still reference the former edition's documents.
Kasyan provided a great response and (untested?) script for updating hyperlink external page destinations. This is the post: https://community.adobe.com/t5/indesign/indesign-cc2018-hyperlink-aktualisieren-per-script/td-p/10569729.
I modified Kasyan's script to include a batch function for all open documents, shown here:
/* Copyright 2019, Kasyan Servetsky
July 12, 2019
Written by Kasyan Servetsky
http://www.kasyan.ho.com.ua
e-mail: askoldich@yahoo.com */
//======================================================================================
var scriptName = "Batch Find And Replace All Hyperlink Page Destinations",
set;
CreateDialog();
function CreateDialog() {
GetDialogSettings();
var dialog = new Window("dialog", scriptName);
dialog.orientation = "column";
dialog.alignChildren = "fill";
var panel = dialog.add("panel", undefined, "Type here the part of the path that differs:");
panel.orientation = "column";
panel.alignChildren = "right";
var group1 = panel.add("group");
group1.orientation = "row";
var findStTxt = group1.add("statictext", undefined, "Find what:");
var findEdTxt = group1.add("edittext", undefined, set.find);
findEdTxt.minimumSize.width = 300;
var group2 = panel.add("group");
group2.orientation = "row";
var replaceStTxt = group2.add("statictext", undefined, "Change to:");
var replaceEdTxt = group2.add("edittext", undefined, set.replace);
replaceEdTxt.minimumSize.width = 300;
var btnGroup = dialog.add("group");
btnGroup.orientation = "row";
btnGroup.alignment = "center";
var okBtn = btnGroup.add("button", undefined, "Ok");
var cancelBtn = btnGroup.add("button", undefined, "Cancel");
var showDialog = dialog.show();
if (showDialog== 1) {
set.find = findEdTxt.text;
set.replace = replaceEdTxt.text;
app.insertLabel("Kas_" + scriptName, set.toSource());
AllActiveDocs();
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function AllActiveDocs() {
for (var d=0; d<app.documents.length; d++)
{
doc = app.documents[d];
Main();
}
var report = "Finished."
alert(report);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function Main() {
var hyperlink, destination, destinationFile, oldDestFilePath, newDestFilePath, newDestFile,
doc = app.activeDocument,
hyperlinks = doc.hyperlinks;
for (var i = hyperlinks.length - 1; i >= 0; i--) {
hyperlink = hyperlinks[i];
destination = hyperlink.destination;
if (destination instanceof HyperlinkExternalPageDestination) {
newDestFilePath = destination.documentPath.absoluteURI.replace(set.find, set.replace);
new File(newDestFilePath);
}
}
}
I suspect the problem is how to correctly point to the hyperlink destination to execute the replace() method and modify the path. Is there an error in syntax? Is 'absoluteURI' not the correct property, perhaps? Here is a sample snip from the IDML 'designmap.xml' that shows the HyperlinkExternalPageDestination document path before and ideally after...
...pointing to the FORMER edition's folder:
<HyperlinkExternalPageDestination Self="u1c58" DestinationUniqueKey="207" Name="TABLE OF CONTENTS - DIGITAL.indd - Page 1 [Fit in Window]" DocumentPath="\\storage\Product Manuals\Owners Manuals\2019\OWNERS MANUAL - CONTENT DRAFT\TABLE OF CONTENTS - DIGITAL.indd" DestinationPageIndex="1" ViewSetting="FitWindow" ViewPercentage="131" Hidden="true">
<Properties>
<ViewBounds Top="-410.68702290076334" Left="-258.77862595419845" Bottom="410.6870229007633" Right="1233.5877862595419" />
</Properties>
</HyperlinkExternalPageDestination>
...and modified here to point to the NEW edition's folder (desired):
<HyperlinkExternalPageDestination Self="u1c58" DestinationUniqueKey="207" Name="TABLE OF CONTENTS - DIGITAL.indd - Page 1 [Fit in Window]" DocumentPath="\\storage\Product Manuals\Owners Manuals\2020\OWNERS MANUAL - CONTENT DRAFT\TABLE OF CONTENTS - DIGITAL.indd" DestinationPageIndex="1" ViewSetting="FitWindow" ViewPercentage="131" Hidden="true">
<Properties>
<ViewBounds Top="-410.68702290076334" Left="-258.77862595419845" Bottom="410.6870229007633" Right="1233.5877862595419" />
</Properties>
</HyperlinkExternalPageDestination>
If I'm missing anything, please let me know. Happy to provide more info as needed. And thanks to all who take a peek at this.
Regards,
Matson
