[GastForen Programmierung/Entwicklung JavaScript myUpdateLinksAfterMoving.jsx an InDesign document to another file location with javascript

  • Suche
  • Hilfe
  • Lesezeichen
  • Benutzerliste
Themen
Beiträge
Moderatoren
Letzter Beitrag

myUpdateLinksAfterMoving.jsx an InDesign document to another file location with javascript

Gerald Hemetsberger
Beiträge gesamt: 5

18. Jan 2022, 16:49
Beitrag # 1 von 3
Bewertung:
(2146 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
myUpdateLinksAfterMoving.jsx an InDesign document
© 2022 Gerald Hemetsberger

Here's another script on on how to get around the fact
that InDesign only works with absolute path links and not with relative references.
However, it is limited to relative links that are only below but not above the active directory.
(Continued from https://www.hilfdirselbst.ch/...rum.cgi?post=574960)

This script is intended to update the links in an active InDesign file
when you have moved with the document to a new file location.
To do this, you only need to call the script without making any further entries in an initial dialog.
Instead, the directory name of the active InDesign document is used for some logic
to make changes to the links as "root" which is more convenient and less prone to errors than manual inputs.

Of course, the script offers the possibility to be aborted again before any actions are triggered.

We assume that all necessary linked files (assets) are part of an existing subdirectory structure
that is direct part of the folder of the active InDesign document (no referencing above).
We also assume that together with this document, the whole asset structure was copied to the new location.

On the other hand, links remain unaffected by this script
that do not point into this subdirectory structure (otherwise referencing above would be needed) or
the file is missing at the new location.

The paths of linked files are updated if they contain the name of the active folder.
The absolute path part is exchanged for the current one while the relative path parts
will be preserved as subject of the new subdirectory structure.

If the name of the active directory exists multiple times in a link, the instance lowest in the directory structure
is used while the other instances are exchanged as part of the absolute path.

Caution: The procedure will not work correctly if linked files have names that include the active folder name.

If necessary, the script will show two dialogs at the end:
1.) The list of files that are missing in the new location.
Two paths are given for a file if the file was nevertheless found at an old location
as these files are hard to uncover in InDesign's link panel.
2.) List of all files that are outside the relative path description.
(as relative linking does only refer here to folders that are inside the active directory)

Examples
------------
Assume the active document to be in a new location on drive B: which is folder "Production":
B:\Editorial\Handouts\Production\InDesign.indd

Link 1 before:
C:\Users\Default\Documents\Production-Structure\Production\picture1.psd
Link 1 after:
B:\Editorial\Handouts\Production\picture1.psd

Link 2 before:
F:\Production\Photoshop\picture2.psd
Link 2 after:
B:\Editorial\Handouts\Production\Photoshop\picture2.psd

Link 3 before:
G:\myDocuments\picture3.psd
Link 3 after:
G:\myDocuments\picture3.psd (unchanged as there is no "Production" folder)

Now start with initial checks:
Make sure that the active document is really stored under a name in a folder.
Put the name of the file, its active folder, and its absolute path in variables.

Code
/* myUpdateLinksAfterMoving.jsx an InDesign document 
© 2022 Gerald Hemetsberger
*/

var doc = null; // this will be the active document
var folder // folder object
var ActiveFilename, ActiveFolder, ActivePath; // active InDesign document name, its folder, and its ablsolute path

// alert("Stop 0");

try {
doc = app.activeDocument; // get the acitve document
}
catch(err){
// oh oh no active doc
alert ("You have no document in InDesign open!\n" +err);
exit();
}

// alert("Stop 1");

try{
folder = Folder(doc.filePath) // get the folder
// folder.execute() //open it
}
catch(err){
alert ("You have forgotten to save the active document!"); // never been saved
exit();
}

ActiveFilename=doc.name;
ActiveFolder=folder.name;
ActivePath=(doc.fullName.parent.fsName).toString() //.replace(/\\/g, '/');

// alert("File name:\t" + ActiveFilename +"\nFolder:\t"+ ActiveFolder +"\nPath:\t"+ ActivePath);


/*
Re-Linking
*/

explanation = "This script is intended to update the links in an active InDesign file when you have moved with it. ";
explanation = explanation + "We assume that all necessary linked files are also in the copied subdirectory structure. ";
explanation = explanation + "The paths of linked files are updated if they contain the name of the active folder:\n";
explanation = explanation + "The absolute path part is exchanged for the current one while the relative path parts are preserved.\n\n";
explanation = explanation + "Caution: The procedure will not work correctly if linked files have names that include the current folder name.\n\n\n";
explanation = explanation + "Do you want to continue?"

if (confirm(explanation) != 1) {
exit();
}


// To work with links
var i, k;
var allLinks = doc.links; // set array allLinks to collection of all links
var currentLink, newPath, IndexFound, newFile, oldFile;

// To display missing files at new location:
var NumMissingFiles = 0;
var NumExternalFiles = 0;
var showList = new Array();
var showListExternals = new Array();
var DisplayList = new Array();
var DisplayListExternals = new Array();

for (i = allLinks.length-1; i >= 0; i--) { // loop through all links backwards
currentLink = allLinks[i]; // set the variable currentLink to the current link
// alert("Existing link: " + currentLink.filePath.toString()); // show current path link
oldFile = new File(currentLink.filePath);

if(currentLink.filePath.lastIndexOf(ActiveFolder) >= 0) {
IndexFound = currentLink.filePath.lastIndexOf(ActiveFolder) + ActiveFolder.length; // find index of active folder name farthest back in the link path
// alert(IndexFound.toString()); // show index

newPath = ActivePath + currentLink.filePath.toString().substring(IndexFound);
// alert("newPath:\t" + newPath); // show new path

newFile = new File(newPath);
if(newPath != currentLink.filePath.toString()) {
// alert("Path needs update!");
if (newFile.exists){
currentLink.relink(newFile); // relink only if the file exists
// I use try-catch block to make the script compatible with CS3 and above
// In other words, if an error occurs here, skip it over and go on
try {
currentLink.update();
}
catch(err) {}
}
else {
// alert("No path change and action required as file is missing in the new location: " + newPath);
showList[NumMissingFiles] = currentLink.filePath.toString() + '\n' + newPath;
NumMissingFiles = NumMissingFiles + 1;
}
}
else {
// alert("I do nothing as path already points to new locationt!");
if (!newFile.exists){
// alert("Action required as file is missing in the new location: " + newPath);
showList[NumMissingFiles] = currentLink.filePath.toString();
NumMissingFiles = NumMissingFiles + 1;
}
}
}
else {
// alert("I do nothing as path is outside active folder!");
if (!oldFile.exists){
// alert("Action required as file is missing in an external location: " + currentLink.filePath.toString());
showListExternals[NumExternalFiles] = 'Missing: ' + currentLink.filePath.toString();
NumExternalFiles = NumExternalFiles + 1;
}
else {
showListExternals[NumExternalFiles] = 'Existing: ' + currentLink.filePath.toString();
NumExternalFiles = NumExternalFiles + 1;
}
}
}

/*
Reporting
*/

// Reporting on files that are missing in the NEW location:
if (showList.length > 0 ) { // report only if needed
showList.sort(); // sorting reporting content
for (k=0;k<=showList.length/50;k++){

for (i=0;i<50;i++){

var count = k*50+i+1;

if (count <= showList.length) {

DisplayList [i]= '# ' + leftPad(count,3) + '\t'+ showList [count-1] + '\n';

}
else {
DisplayList [i] = "";
}

};

alert (DisplayList.join('\n'),'Action required as ' + showList.length +' files are missing in the NEW location:');
}
}

// Reporting on EXTERNAL files:
if (showListExternals.length > 0 ) { // report only if needed
showListExternals.sort(); // sorting reporting content
for (k=0;k<=showListExternals.length/50;k++){

for (i=0;i<50;i++){

var count = k*50+i+1;

if (count <= showListExternals.length) {

DisplayListExternals [i]= '# ' + leftPad(count,3) + '\t'+ showListExternals [count-1] + '\n';

}
else {
DisplayListExternals [i] = "";
}

};

alert (DisplayListExternals.join('\n'), showListExternals.length +' links point to EXTERNAL locations:');
}
}


alert("I am done!");

function leftPad(number, targetLength) {
var output = number + '';
while (output.length < targetLength) {
output = '0' + output;
}
return output;
}


(Dieser Beitrag wurde von Thomas Richard am 18. Jan 2022, 19:15 geändert)
X

myUpdateLinksAfterMoving.jsx an InDesign document to another file location with javascript

Gerald Hemetsberger
Beiträge gesamt: 5

18. Jan 2022, 18:57
Beitrag # 2 von 3
Beitrag ID: #582235
Bewertung:
(2095 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Sorry, I found a wrong decision that affects the reporting on files but not the function of the script. Please, substitute the quoted code by the version below.

Code
 
if (newFile.exists){
currentLink.relink(newFile); // relink only if the file exists

// I use try-catch block to make the script compatible with CS3 and above
// In other words, if an error occurs here, skip it over and go on
try {
currentLink.update();
}
catch(err) {}
}
// alert("No path change and action required as file is missing in the new location: " + newPath);
showList[NumMissingFiles] = currentLink.filePath.toString() + '\n' + newPath;
NumMissingFiles = NumMissingFiles + 1;
}
else {
// alert("I do nothing as path already points to new locationt!");



To be substituted with
Code
if (newFile.exists){ 
currentLink.relink(newFile); // relink only if the file exists

// I use try-catch block to make the script compatible with CS3 and above
// In other words, if an error occurs here, skip it over and go on
try {
currentLink.update();
}
catch(err) {}
}
else {
// alert("No path change and action required as file is missing in the new location: " + newPath);
showList[NumMissingFiles] = currentLink.filePath.toString() + '\n' + newPath;
NumMissingFiles = NumMissingFiles + 1;
}
}
else {
// alert("I do nothing as path already points to new locationt!");



als Antwort auf: [#582234]

myUpdateLinksAfterMoving.jsx an InDesign document to another file location with javascript

Thomas Richard
  
Beiträge gesamt: 19014

18. Jan 2022, 19:17
Beitrag # 3 von 3
Beitrag ID: #582236
Bewertung:
(2065 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Ich habe das Listing in Beitrag 1 korrigiert, falls jemand gleich mit dem dortigen Code anfängt, ohne den Thread weiter zu lesen.


MfG

Thomas


Und wenn dir geholfen wurde, hilf uns, dies auch weiterhin zu können.
http://www.hilfdirselbst.ch/info/


als Antwort auf: [#582235]

Aktuell

Veranstaltungskalender

Hier können Sie Ihre Anlässe eintragen, welche einen Zusammenhang mit den Angeboten von HilfDirSelbst.ch wie z.B. Adobe InDesign, Photoshop, Illustrator, PDF, Pitstop, Affinity, Marketing, SEO, Büro- und Rechtsthemen etc. haben. Die Einträge werden moderiert freigeschaltet. Dies wird werktags üblicherweise innert 24 Stunden erfolgen.

pdf-icon Hier eine kleine Anleitung hinsichtlich Bedeutung der auszufüllenden Formularfelder.

Veranstaltungen
21.06.2022

Online
Dienstag, 21. Juni 2022, 10.00 - 10.30 Uhr

Webinar

Viele Druckereien – kleine und große – suchen heute nach Wegen, ihre Effizienz und Produktivität zu steigern. Dabei erkennen sie oft, dass sie sich dringend um die Optimierung, Standardisierung und Automatisierung ihrer Workflows und Prozesse kümmern sollten. In unserer kostenlosen, jeweils ca. 30 minütigen Webinar-Reihe erfahren Sie, wie Sie die Effizienz- und Produktivitätspotenziale Ihrer Druckerei erschließen können – und wie einfach und kostengünstig das sein kann.

kostenlose 3-teilige Webinar-Reihe, 21., 23. und 26. Juni 2022, jeweils 10:00 - 10:30

Ja

Organisator: Impressed GmbH

Kontaktinformation: Silvia Noack, E-Mailsnoack AT impressed DOT de

https://impressed-workflow-server.de/impressed-webinar-reihe-the-big-picture/

Impressed Webinar-Reihe THE BIG PICTURE
Veranstaltungen
02.02.2023

Prozesse optimieren und effizient gestalten

Zürich
Donnerstag, 02. Feb. 2023, 08.00 - 10.00 Uhr

Digitalisierung, Webauftritt

Digitalisierung mitgestalten - Worauf kommt es an? Wie wichtig ist die Webseite? Webseite mit Word Press? Interne Prozesse optimieren

Ja

Organisator: B. Isik - SNF Academy

Kontaktinformation: Birol Isik, E-Mailinfo AT bkcc DOT ch

https://digitalisierung-heute.ch/digitalisierung-informationstag-schweiz/