Skip to main content
Participant
June 17, 2009
Question

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

  • June 17, 2009
  • 2 replies
  • 1652 views

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,

This topic has been closed for replies.

2 replies

Inspiring
June 18, 2009

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++
        }
    }
}

//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

Participant
June 19, 2009

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++

}

}

}

>

Inspiring
June 19, 2009

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

Robert at ID-Tasker
Legend
June 18, 2009

hi

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

robin

www.adobescripts.co.uk

Participant
June 19, 2009

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

>