0
votes

I have a script that finds image names and replaces it with it's image.

This is what the text in my InDesign file could look like.

@blue_dress_xl.JPG@

Blue Dress XL

Lorem ipsum...

The text is in 3 columns, the width in each column is 40,667 mm. When i use the script to replace @blue_dress_xl.JPG@ with the image, the images comes in 100%.

I'm not that strong in JS, and i tried some different things, but it's not really working.

Is there a way to set the image width to "40,667 mm" when it get's imported?

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].remove();
                f[i].insertionPoints[0].place(file);
            }
            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();
}
1

1 Answers

0
votes

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].remove();
                var rect = f[i].insertionPoints[0].rectangles.add( {geometricBounds:[0,0, 60, 40.667 ]} );
				rect.place ( file );
				rect.fit ( FitOptions.CONTENT_TO_FRAME );
            }
            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();
}