Skip to main content
Participating Frequently
July 10, 2008
Question

Indd - JS: Searching an image in subfolders and relinking

  • July 10, 2008
  • 13 replies
  • 2364 views
Hi,

Can any one help me....
In an Indesign Document I want to relink all the images to a new path.
Please see the below script, it search for the images in a specified path only. But i want to search the images in a particular directory along with its sub-folder too.

var myFolder = Folder.selectDialog("Select Folder for Images");
var new_path = myFolder+ '/'
imgs = app.activeDocument.allGraphics
for (i = 0; i < imgs.length; i++)
{

img = imgs.itemLink

myFile = File (new_path + img.name);

if (myFile.exists) {
img.relink (File (new_path + img.name))
img.update()
}
}

Thanks in advance,
Bharathi Raja G.
This topic has been closed for replies.

13 replies

Kasyan Servetsky
Legend
July 10, 2008
Hi Bharathi,

I recently wrote a similar script, you can download it from here:
http://www.businessweekly.h.com.ua/relink.html

See discussion here:
http://www.adobeforums.com/webx?128@@.59b50466

Kasyan
Inspiring
July 10, 2008
Hello Bharathi,

you will have to take care of one thing:
> img = imgs.itemLink

There may be images without a link (e.g. embedded images).
With them you would run into an error adressing their itemLink.

Martin
Inspiring
July 10, 2008
Hi,

I am just working on a script like yours.
To catch files in subfolders I use the following function:


var topFolder = Folder.selectDialog();
var myResult = scanSubFolders( topFolder );

function scanSubFolders( tFolder )
{
var sFolders = new Array();
var allFiles = new Array();
sFolders[0] = tFolder;
for ( var j = 0; j < sFolders.length; j++ ) // loop through folders
{
var procFiles = sFolders.getFiles();
for ( var k = 0; k < procFiles.length; k++ ) // loop through this folder contents
{
if ( procFiles instanceof File )
allFiles.push( procFiles );
else if ( procFiles instanceof Folder )
sFolders.push( procFiles );
}
}
return allFiles;
}


If you wish to you can return the subfolders too:

return {files:allFiles, folders:sFolders};


Martin
Known Participant
July 10, 2008
Here's what I use to locate files in a tree:

function findFiles(folder, mask) {
var files = folder.getFiles(mask);
var folders = folder.getFiles(function(f) { return f instanceof Folder; });

for (var i = 0; i < folders.length; i++) {
var f = folders;
var ffs = findFiles(f, mask);

// Array.concat occasionally fails in CS/CS2. Do it manually
while (ffs.length > 0) {
files.push(ffs.shift());
}
}
return files;
};

This has been sliced out of some other code

The mask parameter can be a string, RegExp, or a function. In your case, the
call would probably look something like this:

myFile = findFile(img.name);

-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com