0

i´m desperately finding a solution for my issue. I need to place a huge amount of inline graphics in InDesign with this script, but for some reason it doesn't work. I have a very poor knowledge of Javascript and my time is running out so i cannot spend much time studying JS. I'm working in InDesign CC2014 on an iMac with Yosemite.

The following error message pops-up:

error snap:

error snap

I'll be so glad if someone give me a light on this.

main();

function main() {
var name, f, file, text,
arr = [];

if(app.documents.length != 0) {
    var doc = app.activeDocument;   
    var folder = Folder.selectDialog("Choose a folder with images");

    if (folder != null) {
        app.findObjectPreferences = app.changeGrepPreferences  = NothingEnum.NOTHING;
        app.findGrepPreferences.findWhat = "@.+?@";
        f = doc.findGrep(true);

        for (i = 0; i < f.length; i++) {
            name = f[i].contents.replace(/@/g, "");
            file = new File(folder.fsName + "/" + name);

            if (file.exists) {
                f[i].contents = "";
                var rect = f[i].insertionPoints[0].rectangles.add({geometricBounds:[0,0, 60, 40.667 ]} );
    rect.place ( file );
    rect.fit ( FitOptions.FRAME_TO_CONTENT);
                
            }
            else {
                arr.push("File doesn't exist '" + name + "'");
            }
        }

        app.findObjectPreferences = app.changeGrepPreferences  = NothingEnum.NOTHING;

        arr.push("------------------------------------------");
        text = arr.join("\r");
        writeToFile(text);
    }
}
else{
    alert("Please open a document and try again.");
}   
}

function writeToFile(text) {
var file = new File("~/Desktop/Place inline images.txt");
if (file.exists) {
    file.open("e");
    file.seek(0, 2);
}
else {
    file.open("w");
}
file.write(text + "\r"); 
file.close();
}
Mohammad Sadiqur Rahman
  • 5,379
  • 7
  • 31
  • 45

1 Answers1

1

Problem is - probably - cause script is editing found contents and refering to it in the next lines of code. I suggest to use backward looping and move f[i].contents = "" to the line after.

Something like:

main();

function main() {
var name, f, cF, file, text,
arr = [];

if(app.documents.length != 0) {
    var doc = app.activeDocument;   
    var folder = Folder.selectDialog("Choose a folder with images");

    if (folder != null) {
        app.findObjectPreferences = app.changeGrepPreferences  = NothingEnum.NOTHING;
        app.findGrepPreferences.findWhat = "@.+?@";
        f = doc.findGrep(true);

       while(cF = f.pop()) {
            name = cF.contents.replace(/@/g, "");
            file = new File(folder.fsName + "/" + name);

            if (file.exists) {
                var rect = cF.insertionPoints[0].rectangles.add({geometricBounds:[0,0, 60, 40.667 ]} );
                rect.place ( file );
                rect.fit ( FitOptions.FRAME_TO_CONTENT);
                cF.contents = "";
            }
            else {
                arr.push("File doesn't exist '" + name + "'");
            }
        }

        app.findObjectPreferences = app.changeGrepPreferences  = NothingEnum.NOTHING;

        arr.push("------------------------------------------");
        text = arr.join("\r");
        writeToFile(text);
    }
}
else{
    alert("Please open a document and try again.");
}   
}

function writeToFile(text) {
var file = new File("~/Desktop/Place inline images.txt");
if (file.exists) {
    file.open("e");
    file.seek(0, 2);
}
else {
    file.open("w");
}
file.write(text + "\r"); 
file.close();
}
Cashmirek
  • 269
  • 1
  • 9