Copy link to clipboard
Copied
hi!
I want to insert an image into a rectangle made of JavaScript fan tools.
var i = 0;
for (var p = 1; p <= myPages.length - 1; p++) {
for (var n = 1; n <= 1; n++) {
if (i <= myOutGroupPics1.length - 1) {
myPages[p].pahts -> What should I change this place to?
.item("myOutGroup" + n)
.place(File(myOutGroupFolder1 + "/" + myOutGroupPics1[i].name));
i++;
}
}
}
1 Correct answer
Hi @cafefatbear
It seems this thread is also related to the one we discussed here, if it is then I suppose you have your answer, and if you say I can merge this thread with the other one.
-Manan
Copy link to clipboard
Copied
I am unsure what you are storing in myOutGroupPics1.
Just to place an Image file into an already made rectangle you can do this like this:
var myImagFile = File("~/Desktop/Untitled.png");
var myRectangle = app.documents[0].pages[0].rectangles[0];
myRectangle.place(myImagFile);
myRectangle.fit(FitOptions.CONTENT_TO_FRAME);
Best
Sunil
Copy link to clipboard
Copied
Hi @cafefatbear
It seems this thread is also related to the one we discussed here, if it is then I suppose you have your answer, and if you say I can merge this thread with the other one.
-Manan
Copy link to clipboard
Copied
Hello i do not know where to put my question but I also got an error 55. In the below script for automating import images from a folder to pages in indesign i have this line "var page = doc.pages[pageNumber - 1]; "
in my code that gives me an error. my .csv has 2 columns. one with titles and one with numbers 1 till 7
Kind regards jeroen
var csvFilePath = "datasheet.csv";
var imageFolderPath = "OUTPUTFOLDER";
var csvFile = File(csvFilePath);
csvFile.open("r");
var csvData = csvFile.read();
csvFile.close();
var csvRows = csvData.split("\n");
for (var i = 0; i < csvRows.length; i++) {
var rowData = csvRows[i].split(",");
var fileName = rowData[0];
var pageNumber = parseInt(rowData[1]);
var doc = app.activeDocument;
var page = doc.pages[pageNumber - 1];
var imageFile = File(imageFolderPath + "/" + fileName);
if (imageFile.exists) {
var imageFrame = page.rectangles.add();
imageFrame.place(imageFile);
imageFrame.fit(FitOptions.PROPORTIONALLY);
var textFrame = page.textFrames.add();
textFrame.contents = fileName;
textFrame.geometricBounds = [imageFrame.geometricBounds[2], imageFrame.geometricBounds[1], imageFrame.geometricBounds[2] + 20, imageFrame.geometricBounds[3]];
} else {
alert("Image not found: " + fileName);
}
}
Copy link to clipboard
Copied
Your code isn't checking if there is enough pages in the document.
You would have to add check if number of pages in the document is larger or equal to pageNumber and add pages if not - or set number of pages in the document's properties.
Copy link to clipboard
Copied
Dear Robert Tkaczyk thank you for your answer. Not sure if i understand it but i tried to add this: " while (doc.pages.length < pageNumber) {
doc.pages.add(); " However that failed so now i try to get a workaround that simply indesign puts from a folder automatically withouth a .csv file.
I made the below, but i get no message whatever from indesign. not a fault code nor action that takes place.
the folder is just on my desktop and made path to the folder with images exact, not relative.
Thanks in advance for your answer, jeroen
var imageFolderPath = new Folder("/C:/Users/xxxx/Documents/Afbeeldingen");
var imageFiles = imageFolderPath.getFiles();
var doc = app.activeDocument;for (var i = 0; i < imageFiles.length; i++) {
var imageFile = imageFiles[i];while (doc.pages.length < i + 1) {
doc.pages.add();
}
var page = doc.pages[i];
if (imageFile.exists) {
var imageFrame = page.rectangles.add();
imageFrame.place(imageFile);
imageFrame.fit(FitOptions.PROPORTIONALLY);
var textFrame = page.textFrames.add();
textFrame.contents = imageFile.name;
textFrame.geometricBounds = [imageFrame.geometricBounds[2], imageFrame.geometricBounds[1], imageFrame.geometricBounds[2] + 20, imageFrame.geometricBounds[3]];
} else {
alert("Afbeelding niet gevonden: " + imageFile.name);
}
}
Copy link to clipboard
Copied
Dear Robert Tkaczyk thank you for your answer. Not sure if i understand it but i tried to add this: " while (doc.pages.length < pageNumber) {
doc.pages.add(); " However that failed[...]
By jeroen358946423v9n
Failed how / where?
Copy link to clipboard
Copied
While adding to my initial script: " while (doc.pages.length < pageNumber) {
doc.pages.add(); " i still got an error message regarding line: " var page = doc.pages[pageNumber - 1]; "
the error message contains: " Object does not support the property or method 'Nan' . That was what i ment about me failing. Kind regards, jeroen
Copy link to clipboard
Copied
While adding to my initial script: " while (doc.pages.length < pageNumber) {
doc.pages.add(); " i still got an error message regarding line: " var page = doc.pages[pageNumber - 1]; "the error message contains: " Object does not support the property or method 'Nan' . That was what i ment about me failing. Kind regards, jeroen
By jeroen358946423v9n
I think, in this case, 'Nan' refers to the value of the pageNumber.
Copy link to clipboard
Copied
While adding to my initial script: " while (doc.pages.length < pageNumber) {
doc.pages.add(); " i still got an error message regarding line: " var page = doc.pages[pageNumber - 1]; "the error message contains: " Object does not support the property or method 'Nan' . That was what i ment about me failing. Kind regards, jeroen
By jeroen358946423v9n
I think, in this case, 'Nan' refers to the value of the pageNumber.
I'm not JS guy and I don't know what is the best tool to step-by-step debug JS code - but you can always add:
alert(some_variable);
or
alert(some_variable.length);
after every
var some_variable = something;
to see what is the assigned value - or number of elements.
Copy link to clipboard
Copied
jeroen358946423v9n wrote:
Dear Robert Tkaczyk thank you for your answer. Not sure if i understand it but i tried to add this: " while (doc.pages.length < pageNumber) {
doc.pages.add(); " However that failed so now i try to get a workaround that simply indesign puts from a folder automatically withouth a .csv file.
I made the below, but i get no message whatever from indesign. not a fault code nor action that takes place.
the folder is just on my desktop and made path to the folder with images exact, not relative.
Thanks in advance for your answer, jeroen
var imageFolderPath = new Folder("/C:/Users/xxxx/Documents/Afbeeldingen");var imageFiles = imageFolderPath.getFiles();
var doc = app.activeDocument;for (var i = 0; i < imageFiles.length; i++) {
var imageFile = imageFiles[i];while (doc.pages.length < i + 1) {
doc.pages.add();
}var page = doc.pages[i];
if (imageFile.exists) {
var imageFrame = page.rectangles.add();
imageFrame.place(imageFile);
imageFrame.fit(FitOptions.PROPORTIONALLY);var textFrame = page.textFrames.add();
textFrame.contents = imageFile.name;
textFrame.geometricBounds = [imageFrame.geometricBounds[2], imageFrame.geometricBounds[1], imageFrame.geometricBounds[2] + 20, imageFrame.geometricBounds[3]];
} else {
alert("Afbeelding niet gevonden: " + imageFile.name);
}
}
Add this "checkpoint":
alert(imageFiles.length);
after
var imageFolderPath = new Folder("/C:/Users/xxxx/Documents/Afbeeldingen");
var imageFiles = imageFolderPath.getFiles();
alert(imageFiles.length);
Copy link to clipboard
Copied
Dear Robert,
It did not work out but thank you anyway for your help. For now i will try to figure it out and learn more about javascript in relation to indesign so my base of understanding will grow. If i fix it later i will post it here for sure. Kind regards, jeroen
Copy link to clipboard
Copied
Hi @cafefatbear , Not sure if this helps, but you don’t need to construct a a frame for the images, if you place them directly on the page their parent would be a container frame with the same bounds.
So you could simplify and add pages as needed (when .isValid returns as false). Something like this:
var f = Folder.selectDialog("Select the folder containing the images", "");
if(f != null){
var fl = f.getFiles(/\.(jpg|jpeg|eps|pdf|png|indd|tif|gif|psd|ai)$/i);
}
//the script’s measurement unit
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var doc = app.activeDocument;
var p, img, tf;
for (var i = 0; i < fl.length; i++){
//add a page if needed
if (doc.pages[i].isValid) {
p = doc.pages[i]
} else {
p = doc.pages.add()
}
//place the image on the page—the image’s container frame would be img[0].parent
img = p.place(fl[i]);
//make a text frame below the image with the file name as contents
tf = p.textFrames.add({geometricBounds:[img[0].geometricBounds[2],
img[0].geometricBounds[1],
img[0].geometricBounds[2] + 20,
img[0].geometricBounds[3]],
contents:fl[i].name})
};
//reset
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
Copy link to clipboard
Copied

