Change link path
Copy link to clipboard
Copied
I am looking for a way to change the file path of all my links in a document. I just need to remove part of the file path...
If the original file path is
/Volumes/My Passport/HiRes/990/9906.psd
and I need to make it
HiRes/990/990.psd
Is there an action script or some hidden trick to rename all the links?
I'm working on a mac in OS 10.9.5
Thanks for any help!
Copy link to clipboard
Copied
Hi sketcg
I have put together a few lines. Try them if the meet your needs.
Would like to hear what you think.
kind regards
Daniel (from Switzerland)
/*
* written by Daniel Sterchi moonsoft.ch GmbH
*/
main();
exit();
function main() {
if (app.documents.length > 0) {
const delimiter = "/"; // "/" for mac "\\" for windows at lieast I think so (have a mac)
// the active document
var myDoc = app.activeDocument;
// get all the links of this document
var oldLinks = myDoc.links.everyItem().getElements();
// if there are any links
if (oldLinks.length > 0) {
// open a dialog
var newPath = new Folder(oldLinks[0].filePath).selectDlg("Choose a directory").fsName;
for (var i = 0; i < oldLinks.length; i++) {
// to get the filename of the Link.
// for windows you can use the folowing code
// var myFileName = oldLinks.filePath;
// myFileName = myFileName.substr(myFileName.lastIndexOf(delimiter) + 1, myFileName.length);
// var newFile = File(newPath + delimiter + myFileName);
// This way you dont have to bother with HFS+ or UFS directory names with macs
var oldFile = File(oldLinks.filePath);
var newFile = File(newPath + delimiter + oldFile.name);
oldLinks.relink(newFile);
}
}
else alert("Information\nThis document has no links to pictures");
}
else alert("Warning\nNo document open");
alert("Information\nI have done my job, what about yours");
}
Copy link to clipboard
Copied
Hi Daniel,
On line 12 in your comments you mention Windows possibly being back-slashes, that is true for Windows explorer but for InDesign JavaScript it is forward-slashes.
For example:
"/v/Folder Name/File Name.indd"
or
"//server.address.com/Folder Name/File Name.jsx"
Regards,
Brett
Copy link to clipboard
Copied
Hi Brett
Thats good news. I just don't have windows so I can't test it.
Thanks for your input.
Daniel (from Switzerland)
Copy link to clipboard
Copied
Regarding the Windows vs OSX delimiter:
Since the script uses ".fsName" you will have to worry about them because fsName will return the path in the standard platform format (eg. "c:\test\folder" or "/Users/Test/Folder").
My approach for these kinds of situations is to always use the Posix type of path for everything ("/c/test/folder" works just fine on windows) and use fsName only for display purposes.
Copy link to clipboard
Copied
Hi Vamitul
The problem with the mac is that have HFS+ and UFS. I have to make sure that the old path and the new path are in the same format. Otherwise you have something like
newpath UFS: "/Users/…"
oldPath HFS+ "Macintosh HD:Users:USERNAME:directory
I just could do some tests on windows.
Here is what I get for Mac
and here what I get from Windows
Based on what I just saw i would change the script from:
var newPath = new Folder(oldLinks[0].filePath).selectDlg("Choose a directory").fsName;
to:
var newPath = new Folder(oldLinks[0].filePath).selectDlg("Choose a directory").fullName;
kind regards
Daniel (from Switzerland)
Copy link to clipboard
Copied
Daniel! Thank you so much for taking the time to look at this. I am a novice with the actionscript.
I have taken your code and tried to use it but I get an error message
I will continue on trying to figure what I have wrong, but please let me know if you have a suggestion.
Again Thanks!
Copy link to clipboard
Copied
Hi sketcg
What the error message is telling you is, that there is a quotation mark missing at line 24 or above. And actually it is a JavaScript script 😉
Can you provide us the changed script, then I can tell you what goes wrong.
kind regards
Daniel (from Switzerland)
Copy link to clipboard
Copied
Hi sketcg
This is a much smaller variation of the code which also works. It is also faster as the first one.
kind regards
Daniel (from Switzerland)
/*
* written by Daniel Sterchi moonsoft.ch GmbH
*/
main();
exit();
function main() {
if (app.documents.length > 0) {
const delimiter = "/";
// the active document
var myDoc = app.activeDocument;
// get all the links of this document
var oldLinks = myDoc.links.everyItem().getElements();
// if there are any links
if (oldLinks.length > 0) {
// open a dialog
var newPath = new Folder(oldLinks[0].filePath).selectDlg("Choose a directory").fullName;
// traverse the array and relink
for (var i = 0; i < oldLinks.length; i++) {
var newFile = File(newPath + delimiter + oldLinks.name);
oldLinks.relink(newFile);
}
}
else alert("Information\nThis document has no links");
}
else alert("Warning\nNo document open");
alert("Information\nI have done my job, what about yours");
}
Copy link to clipboard
Copied
It looks like the end double quote got converted to a curly one (hence "unterminated string constant" because the starting quote is still okay). If that's the case, it is kind of weird because it is correct in Daniel's code.
Copy link to clipboard
Copied
Hi sketcg
Did you get it to work? Found the problem?
kind regards
Daniel (from Switzerland)

