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>

 

 

Participant
February 15, 2023

Perhaps I have somehow offended someone?
All I said was to try one of the many tools on the web, perhaps the most interesting one right now, to help me solve a problem...

Participant
February 16, 2023

To clarify and sum up: a regular search would take the user to a place to find the answer, either with plenty of information (such as Adobe Help) or interactive interpretation and guidance, as here.

 

ChatGPT says, "Here's the answer." Emphasis on that period. And with something as complex as ID task scripting, it may not be a complete or even accurate answer... but there's no path to follow up and correct it. The answer may be dazzlingly "correct"... but completely wrong.

 

Not a good thing. Not for this, and not for many things for which I have seen ChatGPT pop up in Messiah robes. (For one thing, go count the complete-trash insta-books on Amazon!)

 


I understand the classic scaremongering and and detractors in the face of technologies that are absolutely disruptive and that are ready quickly to challenge the paradigms of work and modus operandi of almost all of us, but history shows how inherent this is now in the very history and evolution of man. That is not to say that the process of "integrating" such algorithms into economic and cultural society, will not go through criticism, adjustments, hardliners, detractors, limitations and regulations, this too, rest assured is normal and will happen.
The first thing I did is to look on the adobe site for the page related to help on scripting, but I couldn't find it, maybe I looked wrong, but it was the first thing I did.
By reading the chatgpt script, learning how to ask, understanding how it goes wrong, I also learned in this little experience. Even I had to learn some code and point out some mistakes to him. From a "learning" perspective, it doesn't seem bad for someone like me who would have maybe taken days to write the first couple of lines of code.

 

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