[GastForen Programmierung/Entwicklung JavaScript Search and Replace of Path Links in InDesign as a Batch Job

  • Suche
  • Hilfe
  • Lesezeichen
  • Benutzerliste
Letzter Beitrag

Search and Replace of Path Links in InDesign as a Batch Job

Gerald Hemetsberger
Beiträge gesamt: 5

18. Apr 2020, 00:49
Beitrag # 1 von 4
(32012 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen

With the help of this time saving script, you can replace the paths of all linked objects with new ones.
This function is especially helpful if you use an existing InDesign project as a template for a new project, copy it to a new directory,
but the paths for InDesign will not change, relatively.

Tip: In this case, simply rename the old path to the linked objects after copying
so that InDesign does not mistakenly continue to use the old objects. You will then get the warnings in the link panels, instead.

The linked objects can even be located in any nested subdirectories.

The script UI: A dialogue box shows up in the sense of "Search and Replace". You search for and enter the text expression that is to change in all paths, equally.
Note: The paths will only be replaced if the files do actually exist in the new directories.
Paths that do not match the search-replace condition remain unaffected.

Example 1:
Original Path, search for:
New Path, replace with:

Example 2:
Original Path:
New Path:
Search for: "7.1"
Replace by: "7.2"

And: I am not a programmer.
The only thing that annoys me is the poor comfort with the cumbersome handling of file paths in InDesign's Link Panel, as 1000 others on the Internet have been complaining for years.

Feedback and improvements are very welcome!

// the following 4 lines declare variables
// usually I declare all vars at the beginning of the function
// when you declare a variable with var, you limit its scope to the function
// (making them local),
// otherwise the scope is global which may cause you problems
// a rule-of-thumb is to use global vars only you really need them

var i, link, newFile, AbsolutePath;
var NumMatches = 0; // counter of how many file paths actually were found that match the replacement condition
var NumUpdates = 0; // counter of how many files paths were actually updated
var NumOpen = 0; // number of files that still do not exist in the new directories

doc = app.activeDocument; // set variable doc to the front most document
links = doc.links; // set variable links to collection of all links

// call the dialoge window
var shownWindow = setConfigWindow();

// Clicked on Ok = true
if(shownWindow.result == true){
// alert ('Ok!');
alert ('Searching for:\t'+shownWindow.StrSearch+'\n'+
'To be replaced by:\t'+shownWindow.StrReplace);

for (i = links.length-1; i >= 0; i--) { // loop through all links backwards
link = links[i]; // set the variable link to the current link

AbsolutePath = link.filePath;

if (AbsolutePath != AbsolutePath.replace(shownWindow.StrSearch,shownWindow.StrReplace)) {

NumMatches = NumMatches+1;
// alert (AbsolutePath+'\n\n'+AbsolutePath.replace(shownWindow.StrSearch,shownWindow.StrReplace));
// create file object:
newFile = new File(AbsolutePath.replace(shownWindow.StrSearch,shownWindow.StrReplace));

if (newFile.exists){
NumUpdates = NumUpdates+1;
link.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 {
catch(err) {}

NumOpen = NumMatches - NumUpdates;
alert ('Number of cases that matched the replacement condition:\t'+ NumMatches +
'\nNumber of relinked objects:\t\t\t\t' + NumUpdates +
'\nNumber of files missing in new folders:\t\t\t' + NumOpen);

// Clicked on Cancel = if false
else {
alert ('The action was canceled');

// provides dialog box as a function
function setConfigWindow() {

// Create dialog box
var configWindow = new Window('dialog','Updating Paths for Linked Objects');

// Adding a display column (siehe auch Adobe JavaScript Tools Guide)
configWindow.column01 = configWindow.add ('group {orientation: "column"}');

// Add framing with "Title"
configWindow.column01.gridPanel = configWindow.column01.add ('panel', undefined,
'Differentiating strings as part of the absolute paths');

// Do the following with the "inside" of the frame...

// Set "orientation" of the "frame" to the left
aligment = 'left';

// Add display group for 1st input
var SearchGroup = add('group');

// Add "input label" (statictext) & input field (edittext)
SearchGroup.add('statictext', [0,0,80,15], 'Search for:');
var SearchInputField = SearchGroup.add('edittext', [0,0,150,24], 'A:\\');

// Add display group for 2nd input
var ReplaceGroup = add('group');

// Add "input label" (statictext) & input field (edittext)
ReplaceGroup.add('statictext', [0,0,80,15], 'Replace with:');
var ReplaceInputField = ReplaceGroup.add('edittext', [0,0,150,24], 'B:\\');


// Add buttons in the "Display column" but outside the "Frame"
configWindow.column01.btnGrp = configWindow.add('group');
configWindow.column01.btnGrp.add('button', undefined, 'Ok');
configWindow.column01.btnGrp.add('button', undefined, 'Cancel');

// View windows in Adobe InDesign
var usrAction = configWindow.show();

// Return three objects
return {
result: usrAction,
StrSearch: SearchInputField.text,
StrReplace: ReplaceInputField.text



Search and Replace of Path Links in InDesign as a Batch Job

Thomas Richard
Beiträge gesamt: 19032

18. Apr 2020, 08:37
Beitrag # 2 von 4
Beitrag ID: #574961
(31990 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Erinnert mich doch stark an das Script von Kaysan Servetsky: https://indesignsecrets.com/...ipt-help/#post-60792



Und wenn dir geholfen wurde, hilf uns, dies auch weiterhin zu können.

als Antwort auf: [#574960]

Search and Replace of Path Links in InDesign as a Batch Job

Gerald Hemetsberger
Beiträge gesamt: 5

19. Apr 2020, 18:46
Beitrag # 3 von 4
Beitrag ID: #574967
(31895 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
I am glad that I found the script from Kaysan Servetsky and could use it as a template. I am not a programmer but a merger. The problem I want to solve is different. I will explain this here briefly.

The strength of Kaysan Servetsky's template is that it can distinguish and select between different file types when relinking graphics. This is not really possible with my script.

The problem that I want to solve more easily is much better described in the description of:

What's this about? I am faced with a finished InDesign document that has its linked assets in a highly branched directory structure. The focus of this directory structure is not on the possible file types. Rather, it's about thematic structure, such as corporate or project-related, different drives or interfaces to other teams. I have a brochure with 12 pages and about 130 linked assets. And now there is an update. So copy and paste the whole structure. On a node further up in the directory structure there will be a small update. And this task can be solved better if you can search and replace certain expressions in link paths.

Here's another script that allows me to quickly display all current link paths alphabetically, 50 per page. I find it quite useful, but please don't expect any praiseworthy programming skills from me.

als Antwort auf: [#574961]

Search and Replace of Path Links in InDesign as a Batch Job

Gerald Hemetsberger
Beiträge gesamt: 5

19. Apr 2020, 18:49
Beitrag # 4 von 4
Beitrag ID: #574968
(31887 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
/* myShowAllLinkPaths.jsx

Lists alphabetically all link paths of the active document regardless of whether the files exist or not

var i, k

var doc = app.activeDocument; // set variable doc to the front most document
var links = doc.links; // set variable links to collection of all links

var showList = new Array();
var DisplayList = new Array();

for (i = links.length-1; i >= 0; i--) {
showList[i] =links[i].filePath;


for (k=0;k<=links.length/50;k++){

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

var count = k*50+i+1;

if (count <= links.length) {

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

else {
DisplayList [i] = "";


alert (DisplayList.join('\n'),'Link Paths List');
// alert(print_r(DisplayList));

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

// alternative to display:

function print_r(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];

if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += print_r(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
} else { //Stings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
return dumped_text;

als Antwort auf: [#574967]


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.


Prozesse optimieren und effizient gestalten

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


Organisator: B. Isik - SNF Academy

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


01.03.2023 - 09.03.2023

Mittwoch, 01. März 2023, 00.00 Uhr - Donnerstag, 09. März 2023, 00.00 Uhr

Online Webinar

Wie gehen wir mit diesen Veränderungen um? Was ist notwendig, damit wir die Digitalisierung im Unternehmen klappt? Veränderungsprozesse verstehen und entsprechend handeln Mitarbeiter als Botschafter Webseite mit WordPress erstellen SEA /SEO (Ads aufschalten)


Organisator: B. Isik - SNF Academy

Kontaktinformation: B. Isik, E-Mailinfo AT snfa DOT ch