Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Change link path

New Here ,
May 22, 2015 May 22, 2015

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!

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 25, 2015 May 25, 2015

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");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 25, 2015 May 25, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 25, 2015 May 25, 2015

Hi Brett

Thats good news. I just don't have windows so I can't test it.

Thanks for your input.

Daniel (from Switzerland)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
May 26, 2015 May 26, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 26, 2015 May 26, 2015

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

Screen Shot 2015-05-26 at 10.44.10.png

and here what I get from Windows

pfade_windows.PNG

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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 25, 2015 May 25, 2015

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 messageerror copy.jpg

I will continue on trying to figure what I have wrong, but please let me know if you have a suggestion.

Again Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 25, 2015 May 25, 2015

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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 26, 2015 May 26, 2015

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");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2015 May 26, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 27, 2015 May 27, 2015
LATEST

Hi sketcg


Did you get it to work? Found the problem?


kind regards


Daniel (from Switzerland)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines