• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to render an array of images via scripting !

Participant ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

Hey everone!

 

I need your help about some issues. Now I'm developing some script to ask the user to attach the photo they want in some place. However, I've maneged to do that. 

let me show you my code then get to the question.

 

// I'm asking the user how many image the gonna add to one slot( it's for magazine). anyhow, then save the number then do a for loop and lets say they enterd 2 then the open dailog going to be opend twice to take the data and the push it to the array.

 

 

slot1 = prompt ("", "", "For the first slot how many product you want to insert?");
var a = new Array();
for (var i = 0; i < slot1; i++) {


var pic = File.openDialog("Choose the excel file");
a.push(pic)
}

 

 

The problem is that when I user this code 

 

 

rect = rect.place(a[0]) 

 

it works!
this is the rect code and i'm consider it as a one slot who is holding 2-3 images.

 

var rect =myDoc.rectangles.add({geometricBounds : [12.7 , 12.7,90,90] });

 

 

However, back to the problem when i run this part it onlt takes the pic at index 0 and the other one it carsh and shows that place only accept fails.

 

 

for (var i = 0; i <= a.length;) {

rect = rect.place(a[i])
i++
}

 

 

So, I'm wondering about that it's applicable to do many images in one rectangle and if yes how can I enhace the code. and are their any alternative to the place function so it can render the array of images in one slot.

 

* I'm using JS and i'm new to the indesign enviroment.

TOPICS
Feature request , How to , Scripting , SDK , Type

Views

345

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 22, 2020 Jul 22, 2020

You could use the fit method

undefined fit (given:FitOptions)

Applies the specified fit option to content in a frame.

Parameter

Type

Description

given

FitOptions

FitOptions.APPLY_FRAME_FITTING_OPTIONS

FitOptions.CENTER_CONTENT

FitOptions.CONTENT_AWARE_FIT

FitOptions.CONTENT_TO_FRAME

FitOptions.FILL_PROPORTIONALLY

FitOptions.FRAME_TO_CONTENT

FitOptions.PROPORTIONALLY

The fit option to use.

 

So your code to fit content to frame could be as following

for(var i =0; i<=a.lengt
...

Votes

Translate

Translate
Community Expert ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

The rectangle can hold only one image at a time so if your code works fine then after the code exits you will have the rectangle with only a single image in it(i.e.) the last image you selected. You code crashes because of a couple of issues. First the line

rect = rect.place(a[i])

should be 

rect.place(a[i])

Also the loop should run to length -1 index so the correct loop would be

 

for (var i = 0; i < a.length;)

Now regarding your rrequirement to host multiple images in a single frame, what visual effect are you trying to create. I would suggest to create a new rectangle for each image that you are placing, you already know how to place the rectangle at the postions you want.

 

-Manan 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

Hey Manam,

 

Thanks for responding and telling me about the rectangle. Also, I've replace it with text frame and it fits and takes the images. But I faced a size isuess.

Do you have a clue or know how to scale the images in a way that can proportion with the frame?

 

the size of the frame is going to be fixed but each image i want to make it small or proportion with the frame.

	for(var i =0; i<=a.length-1;i++){
		b = myDoc.textFrames.add({geometricBounds : [12.7 , 12.7,90,90] });
		b.place(a[i])
	}

 

thanks again! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

You could use the fit method

undefined fit (given:FitOptions)

Applies the specified fit option to content in a frame.

Parameter

Type

Description

given

FitOptions

FitOptions.APPLY_FRAME_FITTING_OPTIONS

FitOptions.CENTER_CONTENT

FitOptions.CONTENT_AWARE_FIT

FitOptions.CONTENT_TO_FRAME

FitOptions.FILL_PROPORTIONALLY

FitOptions.FRAME_TO_CONTENT

FitOptions.PROPORTIONALLY

The fit option to use.

 

So your code to fit content to frame could be as following

for(var i =0; i<=a.length-1;i++){
  b = app.documents[0].textFrames.add({geometricBounds : [12.7 , 12.7,90,90] });
  b.place(a[i])
  b.fit(FitOptions.CONTENT_TO_FRAME)
}

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

LATEST

Awww it worked !!!

Thanks Manan for your time and effort! I really appreciate it ❤️

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines