Copy link to clipboard
Copied
Hello,
I have an Indesign file with the name of the images I want to place:
Each images has it's folder and they're in a top folder named "fotos".
The structure:
fotos / 10 - description / image1
fotos / 20 - description / image2
fotos / 30 - description / image2
fotos / etc………
Here is the code I'm using:
It finds the subfolders and the images files, but don't place them into Indesign.
if(app.documents.length != 0)
{
var subf = Folder ("/Users/iMac/Desktop/fotos/");
var sf = subf.getFiles(onlyFolders);
var s = "";
for (var n = 0; n < sf.length; n++) {
s += sf
.name + "\r"; }
alert(s);
function onlyFolders(subf) {
if (subf.name == "File") {
return false;
} else {
return true;
}
}
if(subf != null){
// reset the Find/Change dialog
app.findGrepPreferences = app.changeGrepPreferences = null;
// formulate a grep search string
app.findGrepPreferences.findWhat = '@.+?@';
// find all occurrence, last one first
f = app.activeDocument.findGrep (true);
for (i = 0; i < f.length; i++){
// construct file name
name = f.contents.replace (/@/g, '');
// place the image
var placedObjects = f.insertionPoints[0].place (File (sf + '/'+ name+'/'));
}
// delete all @??@ codes
app.activeDocument.changeGrep();
}
}
else{
alert('Please open a document and try again.');
}
Here is the error from ExtendScript Toolkit:
Can I get help from you guys?!
Thank you.
The problem is that getFiles won't look at subfolders (unless I am wrong). You need a recursive function.
A possible approach:
...var lib = {
getFiles : function ( fo, aExtensions, bRecursive, aFiles, includeFolder )
{
var exts = aExtensions? aExtensions.join("|") : ".+" ;
var pattern = new RegExp ( "\\."+exts+"$", "g" );
var files = aFiles? aFiles : [];
var filterFunction = function(file)
{
return pattern.test ( file.name );
}
if ( bRecursive )
{
var foFiles = fo.getFiles();
while ( f = foFiles.shift() )
{
if ( f
Copy link to clipboard
Copied
Your biggest mistake is that you are using your reference to the array of files your retrieve from the folder user selection instead of the folder itself.
var sf = subf.getFiles(onlyFolders);
//This is an array of files objects
var placedObjects = f.insertionPoints[0].place (File (sf + '/'+ name+'/'));
//sf isn't the folder object !
//subf is
Copy link to clipboard
Copied
Hello,
Thank you for your reply.
I changed the code to:
if(app.documents.length != 0)
{
var subf = Folder ("/Users/iMac/Desktop/fotos/");
var sf = subf.getFiles(onlyFolders);
var s = "";
for (var n = 0; n < sf.length; n++) {
s += sf
.name + "\r"; }
alert(s);
function onlyFolders(subf) {
if (subf.name == "File") {
return false;
} else {
return true;
}
}
if(subf != null){
// reset the Find/Change dialog
app.findGrepPreferences = app.changeGrepPreferences = null;
// formulate a grep search string
app.findGrepPreferences.findWhat = '@.+?@';
// find all occurrence, last one first
f = app.activeDocument.findGrep (true);
for (i = 0; i < f.length; i++){
// construct file name
name = f.contents.replace (/@/g, '');
// place the image
var placedObjects = f.insertionPoints[0].place (File (subf + '/'+ name+'/'));
}
// delete all @??@ codes
app.activeDocument.changeGrep();
}
}
else{
alert('Please open a document and try again.');
}
But it returns to the top folder and don't search in the subfolders.
Do you know how can fix the code?
Thank you.
Copy link to clipboard
Copied
The problem is that getFiles won't look at subfolders (unless I am wrong). You need a recursive function.
A possible approach:
var lib = {
getFiles : function ( fo, aExtensions, bRecursive, aFiles, includeFolder )
{
var exts = aExtensions? aExtensions.join("|") : ".+" ;
var pattern = new RegExp ( "\\."+exts+"$", "g" );
var files = aFiles? aFiles : [];
var filterFunction = function(file)
{
return pattern.test ( file.name );
}
if ( bRecursive )
{
var foFiles = fo.getFiles();
while ( f = foFiles.shift() )
{
if ( f instanceof Folder )
{
if (includeFolder===true) files[ files.length ] = f;
this.getFiles ( f, aExtensions, true, files );
}
if ( f instanceof File && pattern.test ( f.name ) && f.name!=".DS_Store" )
files[ files.length ] = f;
}
return files;
}
else
{
return fo.getFiles ( filterFunction );
}
},
getFilesDB:function(arrFiles){
var db = {}, n = arrFiles.length, aFile;
while ( n-- ) {
aFile = arrFiles
; db[decodeURI(aFile.name)] = aFile;
}
return db;
},
replaceFiles:function ( doc, db ){
var fgp = app.findGrepPreferences.properties,
found = [],
text,
n = 0,
fileName = "",
file;
app.findGrepPreferences.properties = {
findWhat:"@.+@",
};
found = doc.findGrep();
n = found.length;
while ( n-- ) {
text = found
; tf = text.parentTextFrames.length ? text.parentTextFrames[0] : text.parentStory.textContainers[0];
fileName = text.contents.replace(/@/g, "" );
file = db[fileName];
if ( file ) {
tf .contents = "";
tf.contentType = ContentType.GRAPHIC_TYPE;
tf.place ( file );
tf.fit ( FitOptions.FILL_PROPORTIONALLY );
tf.fit ( FitOptions.CENTER_CONTENT );
}
}
}
};
var main = function(paragraph) {
var doc = app.properties.activeDocument,
fo, files, db;
if ( !doc ) return;
fo = Folder.selectDialog("Please choose the parent folder" );
if ( !fo ) return;
files = lib.getFiles ( fo, ["jpe?g"], true);
if (!files.length) {
alert("no files returned!");
return;
}
db = lib.getFilesDB ( files );
lib.replaceFiles ( doc, db );
}
var u;
app.doScript ( "main(app.activeDocument.stories[0].paragraphs[-1])",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Copy link to clipboard
Copied
Man,
you made my day!!!!
Thank you so much!!
Cheers!
Copy link to clipboard
Copied
Be aware it might not be bulletproof. What if several images are equally named ? Actually script "overrides" file references so the latest found file will prior when placed (I only one "1.jpg" file will be placed even if several found).
Copy link to clipboard
Copied
I will keep that in mind.
It works perfectly for what I need.
Thank you again!