Skip to main content
Participant
February 14, 2023
Answered

Can't fit image to the frame!! please help me with the code

  • February 14, 2023
  • 2 replies
  • 1723 views

I am trying to write the script using chatgpt, and so far, although with difficulty, I have managed to insert as I need , the images in the specific pages. Now the problem is to scale the image in the frame of which I have specified size and position.
What function to use?
Thanks

 

 

// Create an instance of the text file with the settings
var settingsFile = new File("I:/settings.txt");

// Check if the text file exists
if (settingsFile.exists) {
// Open the file in read mode
settingsFile.open("r");

// Read the folder path from the text file
var folderPath = settingsFile.readln();

// Read the pages from the text file
var pages = settingsFile.readln();

// Close the file
settingsFile.close();

// Split the pages string into an array of page numbers
var myPages = pages.split(",");

// Get all the files in the selected folder
var myImages = new Folder(folderPath).getFiles();

// Get the current document
var myDocument = app.activeDocument;

// Iterate through each image in the selected folder
for (var i = 0; i < myImages.length; i++) {

// Get the file name
var fileName = myImages[i].name;

// Check if the file extension is .jpg, .jpeg or .png (you can add more extensions if necessary)
if (fileName.match(/\.(jpg|jpeg|png)$/i)) {

// Get the correct page
var pageNum = parseInt(myPages[i]) - 1;
if (pageNum >= 0 && pageNum < myDocument.pages.length) {
var myPage = myDocument.pages.item(pageNum);
} else {
continue;
}

// Calculate the coordinates of the four corners of the rectangle based on the specified dimensions and the position of the reference point
var rectangleWidth = 187.1;
var rectangleHeight = 196.9;
var rectangleX = 331.812 - (rectangleWidth / 2);
var rectangleY = 111.125 - (rectangleHeight / 2);

// Create a new image frame with the specified dimensions and position
var myRectangle = myPage.rectangles.add({
geometricBounds: [rectangleY, rectangleX, rectangleY + rectangleHeight, rectangleX + rectangleWidth]
});

// Assign the image to the frame
var myImage = myRectangle.place(myImages[i])[0];

// Scale the image to fit the rectangle
myImage.fit(FitOptions.centerContent);
}
}
}

This topic has been closed for replies.
Correct answer Loic.Aigon

Nice that GPT could help but what is even better is actually looking at the doc:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Image.html

You will find appropriate constants for fitting.

2 replies

James Gifford—NitroPress
Legend
February 14, 2023

I am trying to write the script using chatgpt...

 

<fx Kosh> And so it begins. </fx>

 

 

Loic.Aigon
Legend
February 14, 2023

Let's get used to it @James Gifford—NitroPress 😉

James Gifford—NitroPress
Legend
February 14, 2023

I reserve the right to lose my temper, once, the first time a newcomer insists that we are wrong because Chatifer must be right. 😄

 

Loic.Aigon
Loic.AigonCorrect answer
Legend
February 14, 2023

Nice that GPT could help but what is even better is actually looking at the doc:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Image.html

You will find appropriate constants for fitting.

Participant
February 14, 2023

Thank you! Solved...thanks for the link, I have been looking for it for a very long time, in fact I suggested him to use one of the functions and now it is ok! CGPT apologized for not using it before, and thanked me! ajhaha! mythical