Skip to main content
felix888
Participant
July 11, 2019
Answered

InDesign CC2018 Hyperlink aktualisieren (per script)?

  • July 11, 2019
  • 3 replies
  • 1925 views

Hallo Gemeinde,

ich habe ein Buch mit 17 Dateien. In diesen Dateien wird mit über 100 Hyperlinks auf Seiten innerhalb des Buches verlinkt. Jedes Jahr wird das Buch mit allen Dateien in ein neues Verzeichnis kopiert. Jetzt funktionieren die Hyperlinks nicht mehr, da diese auf das alte Verzeichnis verweisen.

...\2019\T01.indd

...\2020\T01.indd

Kann man per Massenpflege z.B. per Script den Pfad ändern?

Viele Grüße

Felix

This topic has been closed for replies.
Correct answer Kasyan Servetsky

That's a few lines too late.

documentPath is only in hyperlinkExternalPageDestination when it comes to hyperlink's destination property.

Regards,
Uwe


I've fixed the script and posted it here on my site — Restore external page destinations — so I could edit it if necessary.

Felix, does it work for you now?

3 replies

Participant
September 16, 2019

@Kasyan_Servetsky i have an indesign book where i have a bunch of  Hyperlink Text Destinations in one document and links to the destinations in a second document.

 

I regularly copy the book and documents to make a new version and relink the book docs to the updated copy.

This all works fine except the hyperlinks still point to the old copy.

 

Your script looks like it should do what i want, but my links are instance of HyperlinkTextDestination not HyperlinkExternalPageDestination as in your script.

 

I have tried to modify to suit, and code runs, but the hyperlinks are not changed. 

 

I would be very grateful if you could help me with this.

 

Thanks in advance!

 

/* Copyright 2019, Kasyan Servetsky
July 12, 2019
Written by Kasyan Servetsky
<a href="http://www.kasyan.ho.com.ua" target="_blank">http://www.kasyan.ho.com.ua</a>
e-mail: askoldich@yahoo.com */
//======================================================================================
var scriptName = "Restore external page destinations",
doc, set, hyperlinks;

app.doScript(PreCheck, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "\"" + scriptName + "\" Script");

//===================================== FUNCTIONS ======================================
function Main() {
	var hyperlink, destination, destinationFile, oldDestFilePath, newDestFilePath, newDestFile,
	startTime = new Date(),
	count = 0;
	
	for (var i = hyperlinks.length - 1; i >= 0; i--) {
		hyperlink = hyperlinks[i];
		destination = hyperlink.destination;
		
		if (destination instanceof  ) {		
			oldDestFilePath = destination.parent.fullName.absoluteURI;
			destinationFile = destination.parent.fullName;
			
			//if (!destinationFile.exists) {
				newDestFilePath = oldDestFilePath.replace(set.find, set.replace);
				newDestFile = new File(newDestFilePath);
                    newDestDoc = app.open(newDestFile);
				if (newDestFile.exists) {
                       destination = newDestDoc.hyperlinkTextDestinations[destination.index]
					//destination.parent.fullName = newDestFile;
					count++;
				}
			//}
		}
	}

	var endTime = new Date();
	var duration = GetDuration(startTime, endTime);
	var report = "Finished.\n" + count + " hyperlink" + ((count == 1) ? " was" : "s were") + " fixed.\n(time elapsed: " + duration + ")";
	alert(report);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
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());
		Main();
	}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetDialogSettings() {
	set = eval(app.extractLabel("Kas_" + scriptName));
	if (set == undefined) {
		set = { find: "find this", replace: "replace with this" };
	}

	return set;
}	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function PreCheck() {
	if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
	doc = app.activeDocument;
	if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);
	if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
	hyperlinks = doc.hyperlinks;
	if (hyperlinks.length == 0) ErrorExit("No hyperlinks in the document.", true);
	CreateDialog();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetDuration(startTime, endTime) {
	var str;
	var duration = (endTime - startTime)/1000;
	duration = Math.round(duration);
	if (duration >= 60) {
		var minutes = Math.floor(duration/60);
		var seconds = duration - (minutes * 60);
		str = minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");
		if (minutes >= 60) {
			var hours = Math.floor(minutes/60);
			minutes = minutes - (hours * 60);
			str = hours + ((hours != 1) ? " hours, " : " hour, ") + minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");
		}
	}
	else {
		str = duration + ((duration != 1) ? " seconds" : " second");
	}

	return str;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
	alert(error, scriptName, icon);
	exit();
}

 

Srishti_Bali
Legend
July 11, 2019

Moved the discussion to InDesign Scripting community.

Kasyan Servetsky
Legend
July 11, 2019

Here I wrote a quick and dirty script for you. Warning: not totally tested.

You have to type in the part of the file path which is different.

I tried to recreate your situation: the file T02.indd was moved from the 2019 to 2020 folder

Before — the destination is broken

After — the hyperlinks were fixed

var scriptName = "Restore external page destinations",

doc, set, hyperlinks;

PreCheck();

//===================================== FUNCTIONS ======================================

function Main() {

    var hyperlink, destination, destinationFile, oldDestFilePath, newDestFilePath, newDestFile,

    startTime = new Date(),

    count = 0;

   

    for (var i = hyperlinks.length - 1; i >= 0; i--) {

        hyperlink = hyperlinks;

        destination = hyperlink.destination;

        oldDestFilePath = destination.documentPath.absoluteURI;

        destinationFile = destination.documentPath;

        if (destination instanceof HyperlinkExternalPageDestination) {

            if (!destinationFile.exists) {

                newDestFilePath = oldDestFilePath.replace(set.find, set.replace);

                newDestFile = new File(newDestFilePath);

       

                if (newDestFile.exists) {

                    destination.documentPath = newDestFile;

                    count++;

                }

            }

        }

    }

    var endTime = new Date();

    var duration = GetDuration(startTime, endTime);

    var report = "Finished.\n" + count + " hyperlink" + ((count == 1) ? " was" : "s were") + " fixed.\n(time elapsed: " + duration + ")";

    alert(report);

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

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());

        Main();

    }

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

function GetDialogSettings() {

    set = eval(app.extractLabel("Kas_" + scriptName));

    if (set == undefined) {

        set = { find: "find this", replace: "replace with this" };

    }

    return set;

}   

//--------------------------------------------------------------------------------------------------------------------------------------------------------

function PreCheck() {

    if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);

    doc = app.activeDocument;

    if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);

    if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);

    hyperlinks = doc.hyperlinks;

    if (hyperlinks.length == 0) ErrorExit("No hyperlinks in the document.", true);

    CreateDialog();

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

function GetDuration(startTime, endTime) {

    var str;

    var duration = (endTime - startTime)/1000;

    duration = Math.round(duration);

    if (duration >= 60) {

        var minutes = Math.floor(duration/60);

        var seconds = duration - (minutes * 60);

        str = minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");

        if (minutes >= 60) {

            var hours = Math.floor(minutes/60);

            minutes = minutes - (hours * 60);

            str = hours + ((hours != 1) ? " hours, " : " hour, ") + minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");

        }

    }

    else {

        str = duration + ((duration != 1) ? " seconds" : " second");

    }

    return str;

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

function ErrorExit(error, icon) {

    alert(error, scriptName, icon);

    exit();

}

By the way, it also works with sub-folders. They should be separated by forward slash/ — like so:

Hope it helps!

— Kas

felix888
felix888Author
Participant
July 12, 2019

sorry - it doesn't work!

Kasyan Servetsky
Legend
July 12, 2019

I tired to recreate your situation and created a couple of simple files I used for testing.

I realize that real files are much more complex: most probably you have other types of hyperlinks which the script currently can't handle.

I suggest you to give me some test files — where the error occurs — so I could look into what goes wrong.