Skip to main content
Inspiring
June 23, 2010
Answered

Path change

  • June 23, 2010
  • 4 replies
  • 11898 views

is there a way to make a script to change just a part of the path to all the links.

lets say all the docks links are in //server_1/ and i have //server_2/ with the same folder tree as server 1 so by just change the 1 to 2 the files would be there..can that be done at once with java script?

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

I wrote a few scripts that update, change path, relink links. Here is one of them, which, I think, is suits best to what you ask.

I called it 'Change paths of links'.

BTW, question to native English speakers: which of the following is gramatically correct?

  • Change paths of links
  • Change paths of the links
  • Change the paths of the links
  • Change links' paths
  • Change the links' paths

When you run the script a dialog box appears with two text fields, where you specify a platform-specific path name, or a path in a platform-independent format known as universal resource identifier (URI) notation, or Mac OS 9 path name (for Mac).

For example any of the following notations are valid:

Windows

c:\dir\file(Windows path name)

/c/dir/file(URI path name)

//10.44.54.70/Test/images(uniform naming convention (UNC) path name of the form //servername/sharename)

//Apple/Test/images

\\10.44.54.70\Test\images (Windows path name)

\\Apple\Test\images (Windows path name)

where 10.44.54.70 IP address of the server, Apple -- DNS name of the server, Test -- share name

Mac

Thefollowing examples assume that the startup volume is MacOSX, and that there isa mounted volume Remote.

/dir/file (Mac OS X path name)

/MacOSX/dir/file(URI path name)

MacOSX:dir:file(Mac OS 9 path name)

/Remote/dir/file (URI path name)

Remote:dir:file (Mac OS 9 path name)

Remote/dir/file (Mac OS X path name)

You can just copy a part of the path in Links panel and paste it to the script's dialog. In CS4, make sure to choose "Copy Platform Style Path" in context menu.

The caseof the characters doesn’t matter: you can type both in upper and lowercase inthe script's dialog. For example — Test, test, TEST, TeSt — are all the samefor the script.

// Change paths of links.jsx
// Script for InDesign CS3 and CS4 -- changes paths of links in the active document.
// Version 1.0
// May 13 2010
// Written by Kasyan Servetsky
// http://www.kasyan.ho.com.ua
// e-mail: askoldich@yahoo.com
//--------------------------------------------------------------------------------------------------------------
var gScriptName = "Change paths of links";
var gScriptVer = 1;
var gOsIsMac = (File.fs == "Macintosh") ? true : false;
var gSet = GetSettings();

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

var gDoc = app.activeDocument;
var gLinks = gDoc.links;
var gCounter = 0;

if (gLinks.length == 0) {
     ErrorExit("This document doesn't contain any links.", true);
}

CreateDialog();

//======================= FUNCTIONS =============================
function CreateDialog() {
     var dialog = new Window("dialog", gScriptName);
     dialog.orientation = "column";
     dialog.alignChildren = "fill";
     
     var panel = dialog.add("panel", undefined, "Settings");
     panel.orientation = "column";
     panel.alignChildren = "right";
     
     var group1 = panel.add("group");
     group1.orientation = "row";
     var findWhatStTxt = group1.add("statictext", undefined, "Find what:");
     var findWhatEdTxt = group1.add("edittext", undefined, gSet.findWhatEdTxt);
     findWhatEdTxt.minimumSize.width = 300;
     
     var group2 = panel.add("group");
     group2.orientation = "row";
     var changeToStTxt = group2.add("statictext", undefined, "Change to:");
     var changeToEdTxt = group2.add("edittext", undefined, gSet.changeToEdTxt);
     changeToEdTxt.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) {
          gSet.findWhatEdTxt = findWhatEdTxt.text;
          gSet.changeToEdTxt = changeToEdTxt.text;
          app.insertLabel("Kas_" + gScriptName + "_ver_" + gScriptVer, gSet.toSource());
          Main();
     }
}
//--------------------------------------------------------------------------------------------------------------
function Main() {
     WriteToFile("\r--------------------- Script started -- " + GetDate() + " ---------------------\n");
     
     for (var i = gLinks.length-1; i >= 0 ; i--) {
          var currentLink = gLinks;
          var oldPath = currentLink.filePath;
          oldPath = oldPath.replace(/:|\\/g, "\/");
          oldPath = oldPath.toLowerCase();
          
          gSet.findWhatEdTxt = gSet.findWhatEdTxt.replace(/:|\\/g, "\/");
          gSet.changeToEdTxt = gSet.changeToEdTxt.replace(/:|\\/g, "\/");
                    
          gSet.findWhatEdTxt = gSet.findWhatEdTxt.replace(/([A-Z])(\/\/)/i, "/$1/");
          gSet.changeToEdTxt = gSet.changeToEdTxt.replace(/([A-Z])(\/\/)/i, "/$1/");
          
          gSet.findWhatEdTxt = gSet.findWhatEdTxt.toLowerCase();
          gSet.changeToEdTxt = gSet.changeToEdTxt.toLowerCase();
          
          if (File.fs == "Windows") oldPath = oldPath.replace(/([A-Z])(\/\/)/i, "/$1/");
          
          var newPath = oldPath.replace(gSet.findWhatEdTxt, gSet.changeToEdTxt);
          
          if (File.fs == "Windows") {
               newPath = newPath.replace(/([A-Z])(\/\/)/, "/$1/");
          }
          else if (File.fs == "Macintosh") {
               newPath = "/Volumes/" + newPath;
          }
     
          var newFile = new File(newPath);
          
          if (newFile.exists) {
               currentLink.relink(newFile);
               gCounter++;
               WriteToFile("Relinked \"" + newPath + "\"\n");
          }
          else {
               WriteToFile("Can't relink \"" + newPath + "\" because the file doesn't exist\n");
          }
     }     
     
     WriteToFile("\r--------------------- Script finished -- " + GetDate() + " ---------------------\r\r");

     if (gCounter == 1) {
          alert("One file has been relinked.", "Finished");
     }
     else if  (gCounter > 1) {
          alert(gCounter + " files have been relinked.", "Finished");
     }
     else {
          alert("Nothing has been relinked.", "Finished");
     }     
}
//--------------------------------------------------------------------------------------------------------------
function GetSettings() {
     var settings = eval(app.extractLabel("Kas_" + gScriptName + "_ver_" + gScriptVer));
     if (settings == undefined) {
          if (gOsIsMac) {
               settings = { findWhatEdTxt:"//ServerName/ShareName/FolderName", changeToEdTxt:"ShareName:FolderName" };
          }
          else {
               settings = { findWhatEdTxt:"ShareName:FolderName", changeToEdTxt:"//ServerName/ShareName/FolderName" };
          }
     }
     return settings;
}
//--------------------------------------------------------------------------------------------------------------
function ErrorExit(myMessage, myIcon) {
     alert(myMessage, gScriptName, myIcon);
     exit();
}
//--------------------------------------------------------------------------------------------------------------
function WriteToFile(myText) {
     var myFile = new File("~/Desktop/" + gScriptName + ".txt");
     if ( myFile.exists ) {
          myFile.open("e");
          myFile.seek(0, 2);
     }
     else {
          myFile.open("w");
     }
     myFile.write(myText);
     myFile.close();
}
//--------------------------------------------------------------------------------------------------------------
function GetDate() {
     var myDate = new Date();
     if ((myDate.getYear() - 100) < 10) {
          var myYear = "0" + new String((myDate.getYear() - 100));
     } else {
          var myYear = new String ((myDate.getYear() - 100));
     }
     var myDateString = (myDate.getMonth() + 1) + "/" + myDate.getDate() + "/" + myYear + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
     return myDateString;
}
//--------------------------------------------------------------------------------------------------------------

4 replies

Participating Frequently
February 11, 2019

Hi, Sorry to drag up old ground, we have recently swapped to Macs in the department but a lot of the documents we open are from PC's and our content management system outputs files to InDesign and all the links are PC, so for instance

On a PC the a file would be located:

Z/:Campaigns:552:Added Value Solutions:Ecospill:Ecosoak:Links:Ecospill 021.psd

But on the Mac the path to that same image will be:

Marketing:Campaigns:552:Added Value Solutions:Ecospill:Ecosoak:Links:Ecospill 021.psd

will the script provided in this thread help me change this? and if so could you point me at the bits I need to change, coding is not my strong point. I need it to run in indesign when I document is opened so that it automatically changes the path rather than anyone having to run the script.

I just need it to change the Z/: or which ever server it pulls the image from to Marketing: etc

I have asked out IT department but they want me to provide them with a Mac on permanent loan and InDesign, that's not going to happen.

Any help you could give would be fantastic.

Kasyan Servetsky
Legend
February 12, 2019

Hi there,

Here's the latest version on my site. I don't remember: maybe I updated it since posting in the thread.

I can't tell whether it will work for you or not: you have to test it yourself. The Windows path in your example is wrong: no colons are allowed.

Use either URI path name

/Z/Campaigns/552/Added Value Solutions/Ecospill/Ecosoak/Links/

or Windows path name

Z:\Campaigns\552\Added Value Solutions\Ecospill\Ecosoak\Links\

You should enter only the part of the file path which is different, like so:

Regards,
Kas

Participating Frequently
February 12, 2019

Massive thanks for that, a bit of playing around and I seem to have it working see above, my next issue is we have 3 servers, G, S and Z now obviously I can run this script each time so that G\ changes to graphics: S\ changes to DTP: and Z\ changes to Marketing: but what I really need is a bit of script that will do all the changes but auto runs when you open a document, I've had a look at modifying the script but frankly I feel like I'm in the Matrix looking at it, is this something that can be done? If you would like me to email you separately then that's not a problem.

Participant
November 28, 2018

Have successfully used it to change an individual link, but was thinking that it would be able to find and replace a portion of the link across multiple missing links. For example:

These are two links that I was able to reconcile using this script.

Find what: \Volumes\STT Marketing\02_SPECIFIC RESPONSES\project\SOQ\Consultants\arch\sample work\Images\sample03.jpg

Change to: M:\02_SPECIFIC RESPONSES\project\SOQ\Consultants\arch\sample work\Images\sample03.jpg

Instead, if I try the following, it says that all the links in the document have been updated, but they’re still shown as missing in the links window:

Find what: \Volumes\STT Marketing\02_specific responses\

Change to: M:\02_specific responses\

Any thoughts or am I not using the right notation?

Kasyan Servetsky
Legend
November 30, 2018

Some people tell me that the script works well for them; some say it ain't working.

Sometimes -- though quite rarely -- I use it at my work and it works for me without glitches.

To solve the problem I have to open a doc in Indy and run the script in debugger (ESTK) to see what and where goes wrong.

Your network set up can be different than mine. For example, once I was writing a script processing links located on client's network. It worked properly on my network but failed on the client's side. Finally, I went to his office and found out that the link paths there had a strange prefix (I couldn't guess it). After I added the prefix, it worked perfectly.

So, I can suggest the following options:

  • you send me a test doc with some links so I could to attempt recreate your problem (you know my e-mail)
  • try to debug it on your side via teamViewer

I don't promise I will solve it: I will try to fix it if it won't take much time for me.

— Kas

Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
June 24, 2010

I wrote a few scripts that update, change path, relink links. Here is one of them, which, I think, is suits best to what you ask.

I called it 'Change paths of links'.

BTW, question to native English speakers: which of the following is gramatically correct?

  • Change paths of links
  • Change paths of the links
  • Change the paths of the links
  • Change links' paths
  • Change the links' paths

When you run the script a dialog box appears with two text fields, where you specify a platform-specific path name, or a path in a platform-independent format known as universal resource identifier (URI) notation, or Mac OS 9 path name (for Mac).

For example any of the following notations are valid:

Windows

c:\dir\file(Windows path name)

/c/dir/file(URI path name)

//10.44.54.70/Test/images(uniform naming convention (UNC) path name of the form //servername/sharename)

//Apple/Test/images

\\10.44.54.70\Test\images (Windows path name)

\\Apple\Test\images (Windows path name)

where 10.44.54.70 IP address of the server, Apple -- DNS name of the server, Test -- share name

Mac

Thefollowing examples assume that the startup volume is MacOSX, and that there isa mounted volume Remote.

/dir/file (Mac OS X path name)

/MacOSX/dir/file(URI path name)

MacOSX:dir:file(Mac OS 9 path name)

/Remote/dir/file (URI path name)

Remote:dir:file (Mac OS 9 path name)

Remote/dir/file (Mac OS X path name)

You can just copy a part of the path in Links panel and paste it to the script's dialog. In CS4, make sure to choose "Copy Platform Style Path" in context menu.

The caseof the characters doesn’t matter: you can type both in upper and lowercase inthe script's dialog. For example — Test, test, TEST, TeSt — are all the samefor the script.

// Change paths of links.jsx
// Script for InDesign CS3 and CS4 -- changes paths of links in the active document.
// Version 1.0
// May 13 2010
// Written by Kasyan Servetsky
// http://www.kasyan.ho.com.ua
// e-mail: askoldich@yahoo.com
//--------------------------------------------------------------------------------------------------------------
var gScriptName = "Change paths of links";
var gScriptVer = 1;
var gOsIsMac = (File.fs == "Macintosh") ? true : false;
var gSet = GetSettings();

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

var gDoc = app.activeDocument;
var gLinks = gDoc.links;
var gCounter = 0;

if (gLinks.length == 0) {
     ErrorExit("This document doesn't contain any links.", true);
}

CreateDialog();

//======================= FUNCTIONS =============================
function CreateDialog() {
     var dialog = new Window("dialog", gScriptName);
     dialog.orientation = "column";
     dialog.alignChildren = "fill";
     
     var panel = dialog.add("panel", undefined, "Settings");
     panel.orientation = "column";
     panel.alignChildren = "right";
     
     var group1 = panel.add("group");
     group1.orientation = "row";
     var findWhatStTxt = group1.add("statictext", undefined, "Find what:");
     var findWhatEdTxt = group1.add("edittext", undefined, gSet.findWhatEdTxt);
     findWhatEdTxt.minimumSize.width = 300;
     
     var group2 = panel.add("group");
     group2.orientation = "row";
     var changeToStTxt = group2.add("statictext", undefined, "Change to:");
     var changeToEdTxt = group2.add("edittext", undefined, gSet.changeToEdTxt);
     changeToEdTxt.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) {
          gSet.findWhatEdTxt = findWhatEdTxt.text;
          gSet.changeToEdTxt = changeToEdTxt.text;
          app.insertLabel("Kas_" + gScriptName + "_ver_" + gScriptVer, gSet.toSource());
          Main();
     }
}
//--------------------------------------------------------------------------------------------------------------
function Main() {
     WriteToFile("\r--------------------- Script started -- " + GetDate() + " ---------------------\n");
     
     for (var i = gLinks.length-1; i >= 0 ; i--) {
          var currentLink = gLinks;
          var oldPath = currentLink.filePath;
          oldPath = oldPath.replace(/:|\\/g, "\/");
          oldPath = oldPath.toLowerCase();
          
          gSet.findWhatEdTxt = gSet.findWhatEdTxt.replace(/:|\\/g, "\/");
          gSet.changeToEdTxt = gSet.changeToEdTxt.replace(/:|\\/g, "\/");
                    
          gSet.findWhatEdTxt = gSet.findWhatEdTxt.replace(/([A-Z])(\/\/)/i, "/$1/");
          gSet.changeToEdTxt = gSet.changeToEdTxt.replace(/([A-Z])(\/\/)/i, "/$1/");
          
          gSet.findWhatEdTxt = gSet.findWhatEdTxt.toLowerCase();
          gSet.changeToEdTxt = gSet.changeToEdTxt.toLowerCase();
          
          if (File.fs == "Windows") oldPath = oldPath.replace(/([A-Z])(\/\/)/i, "/$1/");
          
          var newPath = oldPath.replace(gSet.findWhatEdTxt, gSet.changeToEdTxt);
          
          if (File.fs == "Windows") {
               newPath = newPath.replace(/([A-Z])(\/\/)/, "/$1/");
          }
          else if (File.fs == "Macintosh") {
               newPath = "/Volumes/" + newPath;
          }
     
          var newFile = new File(newPath);
          
          if (newFile.exists) {
               currentLink.relink(newFile);
               gCounter++;
               WriteToFile("Relinked \"" + newPath + "\"\n");
          }
          else {
               WriteToFile("Can't relink \"" + newPath + "\" because the file doesn't exist\n");
          }
     }     
     
     WriteToFile("\r--------------------- Script finished -- " + GetDate() + " ---------------------\r\r");

     if (gCounter == 1) {
          alert("One file has been relinked.", "Finished");
     }
     else if  (gCounter > 1) {
          alert(gCounter + " files have been relinked.", "Finished");
     }
     else {
          alert("Nothing has been relinked.", "Finished");
     }     
}
//--------------------------------------------------------------------------------------------------------------
function GetSettings() {
     var settings = eval(app.extractLabel("Kas_" + gScriptName + "_ver_" + gScriptVer));
     if (settings == undefined) {
          if (gOsIsMac) {
               settings = { findWhatEdTxt:"//ServerName/ShareName/FolderName", changeToEdTxt:"ShareName:FolderName" };
          }
          else {
               settings = { findWhatEdTxt:"ShareName:FolderName", changeToEdTxt:"//ServerName/ShareName/FolderName" };
          }
     }
     return settings;
}
//--------------------------------------------------------------------------------------------------------------
function ErrorExit(myMessage, myIcon) {
     alert(myMessage, gScriptName, myIcon);
     exit();
}
//--------------------------------------------------------------------------------------------------------------
function WriteToFile(myText) {
     var myFile = new File("~/Desktop/" + gScriptName + ".txt");
     if ( myFile.exists ) {
          myFile.open("e");
          myFile.seek(0, 2);
     }
     else {
          myFile.open("w");
     }
     myFile.write(myText);
     myFile.close();
}
//--------------------------------------------------------------------------------------------------------------
function GetDate() {
     var myDate = new Date();
     if ((myDate.getYear() - 100) < 10) {
          var myYear = "0" + new String((myDate.getYear() - 100));
     } else {
          var myYear = new String ((myDate.getYear() - 100));
     }
     var myDateString = (myDate.getMonth() + 1) + "/" + myDate.getDate() + "/" + myYear + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
     return myDateString;
}
//--------------------------------------------------------------------------------------------------------------

Jongware
Community Expert
Community Expert
June 24, 2010

Not a native speaker, but I'd suggest "Change the path of each link" 

Kasyan Servetsky
Legend
June 24, 2010

Not a native speaker, but I'd suggest "Change the path of each link"

Thanks, I'll change it.

Kasyan

Jongware
Community Expert
Community Expert
June 23, 2010

I'm guessing you can.

The filePath property of a link is just a Javascript string, and you can manipulate it at will. You can't immediately change the file name of the link, as it is a read-only property, but you can copy the string, change it, then feed it back into the link using the relink method.