InDesign CC2018 Hyperlink aktualisieren (per script)?

New Here ,
Jul 11, 2019

Copy link to clipboard

Copied

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

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?

TOPICS
Scripting

Views

756

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

InDesign CC2018 Hyperlink aktualisieren (per script)?

New Here ,
Jul 11, 2019

Copy link to clipboard

Copied

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

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?

TOPICS
Scripting

Views

757

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
Jul 11, 2019 0
Guru ,
Jul 11, 2019

Copy link to clipboard

Copied

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.

2019-07-11_13-30-26.png

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

2019-07-11_13-53-52.png

Before — the destination is broken

2019-07-11_13-29-28.png

After — the hyperlinks were fixed

2019-07-11_13-45-30.png

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:

2019-07-11_14-39-30.png

Hope it helps!

— Kas

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...
Jul 11, 2019 1
New Here ,
Jul 12, 2019

Copy link to clipboard

Copied

sorry - it doesn't work!

fail.PNG

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...
Jul 12, 2019 0
Guru ,
Jul 12, 2019

Copy link to clipboard

Copied

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.

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...
Jul 12, 2019 0
Adobe Community Professional ,
Jul 12, 2019

Copy link to clipboard

Copied

Hi Kasyan,

just check if the hyperlink's destination is a hyperlinkExternalPageDestination.

If not continue the loop.

Regards,
Uwe

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...
Jul 12, 2019 0
Guru ,
Jul 12, 2019

Copy link to clipboard

Copied

Hi Uwe,
I do check it in the line #18.

— Kas

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...
Jul 12, 2019 0
Adobe Community Professional ,
Jul 12, 2019

Copy link to clipboard

Copied

That's a few lines too late.

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

Regards,
Uwe

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...
Jul 12, 2019 0
Guru ,
Jul 12, 2019

Copy link to clipboard

Copied

Ouch!

Sure, I will fix it in a moment.

Thanks for pointing this out, Uwe.

— Kas

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...
Jul 12, 2019 0
Guru ,
Jul 12, 2019

Copy link to clipboard

Copied

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?

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...
Jul 12, 2019 1
New Here ,
Jul 15, 2019

Copy link to clipboard

Copied

Hi Kas,

it works well - fantastic

thx Felix

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...
Jul 15, 2019 0
Adobe Employee ,
Jul 11, 2019

Copy link to clipboard

Copied

Moved the discussion to InDesign Scripting community.

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...
Jul 11, 2019 0
Ot_Gu LATEST
New Here ,
Sep 16, 2019

Copy link to clipboard

Copied

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

 

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...
Sep 16, 2019 0