Highlighted

Help Writing Script for Batch Find and Replace Hyperlink External Page Destinations

New Here ,
Sep 16, 2020

Copy link to clipboard

Copied

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/1056....

 

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

Topics

How to, Scripting

Views

79

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Help Writing Script for Batch Find and Replace Hyperlink External Page Destinations

New Here ,
Sep 16, 2020

Copy link to clipboard

Copied

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/1056....

 

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

Topics

How to, Scripting

Views

80

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Sep 17, 2020

Copy link to clipboard

Copied

(I am running CC2020)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
New Here ,
Sep 17, 2020

Copy link to clipboard

Copied

Update:

I included these two alerts to diagnose if the hyperlink destination file paths were being modified, and how they were being modified:

alert(String(destination.documentPath.absoluteURI));
alert(String(newDestFilePath));

 

I discovered four important results:

 

1. Nothing is saving. Even with the  'new' operator and 'File' constructor. Not sure how to resolve this yet. Could it be an unmodifiable read-only file? This is my biggest problem to overcome.

2. The function 'Main' will only find and replace one instance of the set.find value, even if more than one value exists to replace.

3.  The alerts show the file path as a URL (obvious to me now). Thus, it's obviously not pulling the file path from the 'designmap.xml' file, so I don't know where the file path that I need to modify is being stored, or if that matters. Also apparent now: trying to do a find/replace using spaces (and not '%20' characters) will not work (in fact, any spaces in the set.find value causes a no-change result.

 

Problems #1 and #2 above are the most necessary to fix.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
MatsonWP LATEST
New Here ,
Sep 18, 2020

Copy link to clipboard

Copied

Otherwise, would using either GREP or Rorohiko's Gryperlink.jsx work? However, I cannot determine if there is a way to use GREP for hyperlinked objects (rectangles used as chapter shortcuts, for example), since these hyperlinked objects do not carry character/font/paragraph styles or other obvious patterns with which to search.

 

Any thoughts? Thanks.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...