8 Replies Latest reply on Jun 23, 2009 11:31 AM by L. Guy O'Rojo

    Script to search and relink linked image files that were moved?

    shadoutmapes

      Good afternoon

       

       

      My indesign product catalog has links with a lot of different folders around in my hard disk, not a single folder. And to keep my catalog up to date I must package it.


      When I move my linked files in my computer because I change the folder structure or do some cleaning, my indd loses track of its linked files. Currently I need to relink one by one.

       

      Therefore I would like to find a script which would scan the hard disk with the all broken links in mind to detect their new location and relink. Yes I am a lazy person.


      Thanks for any clue,

        • 1. Re: Script to search and relink linked image files that were moved?
          AdobeScripts Level 3

          hi

           

          I can help you - but only if you work on PC - contact me on priv

           

          robin

           

          www.adobescripts.co.uk

          • 2. Re: Script to search and relink linked image files that were moved?
            Randomsen

            At least your honest about your laziness.... Here's your reward:

            Just a hint, don't select root of your volume, it will loop through every folder in existence, but key to this script is, if you know the general location, select into that folder hierarchy and the script will test for a relative path, otherwise, come back in a few days, and it will be finished.

             

            ~mike

             

             


            var processed = 0
            var skipped = 0
            var updated = 0
            if (app.documents.length > 0){
                if(app.activeDocument.links.length > 0){
                    var mydoc = app.activeDocument;
                    var mylinks = mydoc.links;
                    var myRoot = Folder.selectDialog("Choose the volume or server where assets are located", undefined, false);
                    main();
            }
            else
            { alert("No Links present")
                }
            }
            else{alert("No Documents Open")
                }

             

            function main(){
                if(myRoot != null){
                    for(var i = 0; i < mylinks.length ; i++){
                        if(mylinks.item(i).status == LinkStatus.linkMissing){
                                var linkdata= mylinks.item(i).filePath;
                                var my_result = linkRepair(linkdata);
                                    if (my_result == false){
                                        var filetype = "." + mylinks.item(i).linkType;
                                        var mysearch =   search(linkdata, filetype);
                                            if(mysearch != undefined){
            var myswitch = confirm(mylinks.item(i).name + " has been found in a different location, Relink and Update?", "Relinker")
                                                    if(myswitch){
                                                        mylinks.item(i).relink(File(mysearch));
                                                        mylinks.item(i).update();
                                                    }
                                                }
                                        else{
                                            alert("" + mylinks.item(i).name +" was not found\nFolders processed: "+processed + "\nFiles skipped: "+ skipped)
                                        }
                               
                                    }
                                    else{ alert( my_result + " has been found");
                                        mylinks.item(i).relink(File(my_result));
                            }
                        }
                    }
                }
            }

             

            function linkRepair(linkdata){
            var mypath = linkdata.split(":")
                    my_status = analyzePath(mypath)
                    if(my_status == true){alert("UPDATED");
                            updated++       
                        }
                return my_status
                }
            function analyzePath(mypath){
            var num = mypath.length ;
            for(var i = 0; i < num-1; i++){
                mypath.shift()
                var newpath = pathRebuild(mypath)
                newpath = (myRoot + newpath)
                    if(File(newpath).exists){
                        return newpath
                        }
                    }
                return false
            }

             

            function pathRebuild(pathArray){
            var solidPath = ""
                for(var i = 0; i < pathArray.length ; i++){
                    solidPath +=  "/" + pathArray[i] ;
                }
                return solidPath;
            }
            function search(linkdata, filetype){
                var mypath = linkdata.split(":")
                var mylink = mypath.pop();
                var OK = confirm("Relative Path does not exist, search folder hiearchy for file?", true, "File Scanner")
                if(OK){
            var myscanresult = getfiles(mylink, myRoot)
            return myscanresult
            }
                }
            function getfiles(mylink, myBase){
            myBase = Folder(myBase);
            var files = myBase.getFiles("*")
            for(var i = 0; i < files.length; i++){
            try{
            var foldertest = files[i].getFiles();

             

            processed++
            var myfile =     File(files[i] + "/" + mylink)
                if(myfile.exists == true){
                    return myfile
                    break;
                    }
                else{
                var myscan = getfiles(mylink, Folder(files[i]))    
                    if(myscan != undefined){
                    return myscan
                    break;
                    }
                }
            }
            catch(myerror){
                skipped++
                    }
                }
            }

             

            //PS. THIS IS SET UP FOR A MAC

            // IF YOUR ON A PC    change this: var mypath = linkdata.split(":") to this     var mypath = linkdata.split("/")

             

            Message was edited by: Randomsen

            • 3. Re: Script to search and relink linked image files that were moved?
              shadoutmapes Level 1

              Yes I am on Vista 64 and cs4

              Thanks in advance

               

              Date: Thu, 18 Jun 2009 12:25:46 -0600

              From: forums@adobe.com

              To: guiliguili1@hotmail.fr

              Subject: Script to search and relink linked image files that were moved?

               

              hi

               

              I can help you - but only if you work on PC - contact me on priv

               

              robin

               

              www.adobescripts.co.uk

              >

              • 4. Re: Script to search and relink linked image files that were moved?
                shadoutmapes Level 1

                Thanks so much! I will try soonest.

                How does your script react in the unlikely case of duplicates?

                 

                 

                Date: Thu, 18 Jun 2009 12:43:47 -0600

                From: forums@adobe.com

                To: guiliguili1@hotmail.fr

                Subject: Script to search and relink linked image files that were moved?

                 

                At least your honest about your laziness.... Here's your reward:

                Just a hint, don't select root of your volume, it will loop through every folder in existence, but key to this script is, if you know the general location, select into that folder hierarchy and the script will test for a relative path, otherwise, come back in a few days, and it will be finished.

                 

                ~mike

                 

                 

                 

                var processed = 0

                var skipped = 0

                var updated = 0

                if (app.documents.length > 0){

                    if(app.activeDocument.links.length > 0){

                        var mydoc = app.activeDocument;

                        var mylinks = mydoc.links;

                        var myRoot = Folder.selectDialog("Choose the volume or server where assets are located", undefined, false);

                        main();

                }

                else

                { alert("No Links present")

                    }

                }

                else{alert("No Documents Open")

                    }

                 

                function main(){

                    if(myRoot != null){

                        for(var i = 0; i < mylinks.length ; i++){

                            if(mylinks.item(i).status == LinkStatus.linkMissing){

                                    var linkdata= mylinks.item(i).filePath;

                                    var my_result = linkRepair(linkdata);

                                        if (my_result == false){

                                            var filetype = "." + mylinks.item(i).linkType;

                                            var mysearch =   search(linkdata, filetype);

                                                if(mysearch != undefined){

                var myswitch = confirm(mylinks.item(i).name + " has been found in a different location, Relink and Update?", "Relinker")

                                                        if(myswitch){

                                                            mylinks.item(i).relink(File(mysearch));

                                                            mylinks.item(i).update();

                                                        }

                                                    }

                                            else{

                                                alert("" + mylinks.item(i).name " was not found\nFolders processed: "processed + "\nFiles skipped: "+ skipped)

                                            }

                                     

                                        }

                                        else{ alert( my_result + " has been found");

                                            mylinks.item(i).relink(File(my_result));

                                }

                            }

                        }

                    }

                }

                 

                function linkRepair(linkdata){

                var mypath = linkdata.split(":")

                        my_status = analyzePath(mypath)

                        if(my_status == true){alert("UPDATED");

                                updated++       

                            }

                    return my_status

                    }

                function analyzePath(mypath){

                var num = mypath.length ;

                for(var i = 0; i < num-1; i++){

                    mypath.shift()

                    var newpath = pathRebuild(mypath)

                    newpath = (myRoot + newpath)

                        if(File(newpath).exists){

                            return newpath

                            }

                        }

                    return false

                }

                 

                function pathRebuild(pathArray){

                var solidPath = ""

                    for(var i = 0; i < pathArray.length ; i++){

                        solidPath +=  "/" + pathArray+ ;

                    }

                    return solidPath;

                }

                function search(linkdata, filetype){

                    var mypath = linkdata.split(":")

                    var mylink = mypath.pop();

                    var OK = confirm("Relative Path does not exist, search folder hiearchy for file?", true, "File Scanner")

                    if(OK){

                var myscanresult = getfiles(mylink, myRoot)

                return myscanresult

                }

                    }

                function getfiles(mylink, myBase){

                myBase = Folder(myBase);

                var files = myBase.getFiles("*")

                for(var i = 0; i < files.length; i++){

                try{

                var foldertest = files+.getFiles();

                 

                processed++

                var myfile =     File(files+ + "/" + mylink)

                    if(myfile.exists == true){

                        return myfile

                        break;

                        }

                    else{

                    var myscan = getfiles(mylink, Folder(files+))    

                        if(myscan != undefined){

                        return myscan

                        break;

                        }

                    }

                }

                catch(myerror){

                    skipped++

                        }

                    }

                }

                >

                • 5. Re: Script to search and relink linked image files that were moved?
                  Randomsen Level 1

                  As long as the assets are in the same location, the duplicates will be updated as well.  It will prompt you for each file that isn't found automatically (So if you want to cancel you can. )

                   

                  It gets the old path and tests each folder that it could be

                  If this was the original path,

                   

                  /c/folder1/folder2/folder3/folder4/myfile.eps

                   

                  and lets say you choose a folder like /c/foldera/

                   

                  its going to loop through and test every possibilty:

                   

                  /c/foldera/folder1/folder2/folder3/folder4/myfile.eps

                  /c/foldera/folder2/folder3/folder4/myfile.eps

                  /c/foldera/folder3/folder4/myfile.eps

                  /c/foldera/folder4/myfile.eps

                  /c/foldera/myfile.eps

                   

                  and will keep doing this through every folder within the folder you originally selected... the idea being there may be a possible relative link at least partially from the original path, and if there is, it will have saved a ton of time.

                   

                  I've seen this relinking type of script alot in these forums... which would lead me to ask this question to everyone? Can this be improved on??

                  Is it any good? Dave Saunders! (whose blog got me started a year ago) how'd be this script?

                   

                  ~Mike

                  • 6. Re: Script to search and relink linked image files that were moved?
                    L. Guy O'Rojo Level 2

                    > I've seen this relinking type of script alot in these forums... which would lead me to ask

                    > this question to everyone? Can this be improved on??

                     

                    In my opinion, one good way would be to do what QX did (or does): Allow the user to specify the linking behavior. For example: "Use Relative Path - Do Not Ask". This would allow the user to swap an entire folder of graphics with another one when all the new folder and filenames matched the previous paths. So if a frame is expecting "/ui_elements/button3.eps" in a sub-folder, if it finds one at that path location then that's what it uses, and without throwing up a workflow-killing dialog box. There would be no need for PITA scripting workarounds.

                     

                    But I have asked for this feature for years without success, so I doubt Adobe will allow mere users that much "power".

                     

                    egr

                    • 7. Re: Script to search and relink linked image files that were moved?
                      AdobeScripts Level 3
                      In my opinion, one good way would be to do what QX did (or does): Allow the user to specify the linking behavior. For example: "Use Relative Path - Do Not Ask". This would allow the user to swap an entire folder of graphics with another one when all the new folder and filenames matched the previous paths. So if a frame is expecting "/ui_elements/button3.eps" in a sub-folder, if it finds one at that path location then that's what it uses, and without throwing up a workflow-killing dialog box. There would be no need for PITA scripting workarounds.

                       

                      But I have asked for this feature for years without success, so I doubt Adobe will allow mere users that much "power".

                       

                      egr

                       

                      but you can achieve this all the time in InDesign - you only need to copy your INDD file to new directory - where you have new links - and rename original directory - when InDesign can't find links in original source dir - it will look for links in current directory and subdirectories

                       

                      robin

                       

                      www.adobescripts.co.uk

                      • 8. Re: Script to search and relink linked image files that were moved?
                        L. Guy O'Rojo Level 2

                        > copy your INDD file to new directory - where you have new links - and rename original directory -

                        > when InDesign can't find links in original source dir - it will look for links in current directory and subdirectories

                         

                        Thanks robin, I will try this extremely intuitive technique!

                         

                        egr