Skip to main content
Inspiring
December 20, 2018
Question

Place img inside rectangle identified by scriptLabel

  • December 20, 2018
  • 5 replies
  • 1477 views

I'm only going to include a short piece of the overall script, since the question is fairly simple (and basic).

I'm hoping to identify a rectangle by scriptLabel on each page of a document and place an image inside it. "Target" is the scriptLabel applied to the rectangle in which the imgFile will be contained.

Thank you for any advice you might be able to provide.

var imgFile = "~/Documents/images/img-1.jpg";
var thisRectangle = myPage.rectangles[0];

var targetRectangle = myPage.rectangles[i].label == "target";


// works (targeted rectangle is first item on page)
try{

thisRectangle.place(File(imgFile));
}


// doesn't work (targetRectangle is not a function)
try{

     targetRectangle.place(File(imgFile));
}

This topic has been closed for replies.

5 replies

matthewjmAuthor
Inspiring
December 22, 2018

Tantalizingly close! Now I have images placed on pages one and two but not three.

Clearly, I need to practice more with iterating through loops.

Community Expert
December 23, 2018

The code seems to be working fine, you have written the code to place image in rectangle which has the label set to string target. Page  3 does not have any such rectangle, add the label to any rectangle on that page and it should work. Look at the gif of your document to see what i am saying, look at the Script Label palette in the gif as i select rectangles on different pages.

-Manan

-Manan
matthewjmAuthor
Inspiring
December 22, 2018

I created three different img-1.jpg files. The alerts gave me time to go back and replace the img file in-between pages.

It appears the issue is that I'm continually replacing the image on the first page, even as I loop over pages two and three.

Community Expert
December 22, 2018

You need to use j in the following statements instead of i

if (thisPage.rectangles[i].label == "target")

thisPage.rectangles[i].place(File(thisState));

-Manan

-Manan
matthewjmAuthor
Inspiring
December 22, 2018

Like so?

var doc = app.activeDocument,

   pagesLength = doc.pages.length;


for (var i = 0; i < pagesLength; i++) {

   alert(i);

   var myPage = doc.pages[i];

   placeImg(myPage);

}


function placeImg(myPage) {

   // alert("inside placeImg");


   var thisPage = myPage,

   thisState = "~/Documents/code/targeted-rectangle/images/img-1.jpg";

   // alert (thisPage);


   for(var j = 0; j < thisPage.rectangles.length; j++){

   alert("looping through rectangles")

   if (thisPage.rectangles[i].label == "target") {

   alert("found target")

   try {

   thisPage.rectangles[i].place(File(thisState));

  } catch (e) {

   alert(e);

  };

  }

  }

}

matthewjmAuthor
Inspiring
December 21, 2018

Manan,
Your addition helped to an extent. I'm posting the link to the files I'm testing, in case that would be helpful. http://arthousemedia.com/files/targeted-rectangle.zip

I can see that I'm looping through each page, but the "target" rectangle is only found on the first of the three pages I'm testing.

The first alert tells me which page I'm on. The second tells me when I'm in the placeImg function. These fire three times, but the "found target" alert fires only once.

Here's the entire script with your addition included.

var doc = app.activeDocument,

   pagesLength = doc.pages.length;

 

for (var i = 0; i < pagesLength; i++) 

  { 

   alert(i);

   var myPage = doc.pages[i]; 

   placeImg(myPage); 

  } 

  

function placeImg(myPage){

   alert("inside placeImg");

  

   var thisState = "~/Documents/code/targeted-rectangle/images/img-1.jpg",

   thisPage = app.properties.activeWindow && app.activeWindow.activePage;

   if(thisPage.rectangles[i].label == "target"){

   alert("found target")

   try {

   thisPage.rectangles[i].place(File(thisState)); 

  }

   catch(e){ 

   alert(e); 

  };

  } 

}

Community Expert
December 22, 2018

Hi Mathew,

There a couple of issue with your code, i will list them below

1. You loop over all the pages in the document and call the method placeImg with each page, but inside the method you don't use the page object sent as argument but use the following statement

thisPage = app.properties.activeWindow && app.activeWindow.activePage;

what the above statement does is it assigns the active page to thisPage which will always remain the same during script execution. Hence the method placeImg always works on a single page i.e the active page.

2. In the method placeImg you use the statement

if(thisPage.rectangles.label == "target")

This has a couple of problems, what is this i that you are using, you never defined its value inside this function. Also this statement will be run just once while the method code is executing so we won't be able to process all the rectangles of the page. To process all the rectangles we would need a loop inside each method call. So this statement should be inside a loop.

To put it simply we should have one loop to iterate over all the pages and another loop to iterate over rectangle of each page

Now i would l like you to try and make the changes as per my suggestion and things should work as you need them. To summarise

1. Use myPage inside placeImg method and not thisPage, as thisPage will always remain the same object between method calls

thisPage = myPage

2. Place the if statement inside a loop of all the rectangles within myPage.

for(var j = 0; j < thisPage.rectangles.length; j++)

3. Then use the variable of loop you created above i.e. j to process the rectangle not i that you have been using

Let me know how this goes

-Manan

-Manan
Community Expert
December 21, 2018

The code does not work because targetRectangle is a boolean value,  it is assigned by the result of

myPage.rectangles.label == "target"

So targetRectangle is either true or false and a boolean value does not have the function place, so the error is correct.

What you need to do is use the object whose label is true, so something like below will work

if(myPage.rectangles.label == "target")

     myPage.rectangles.place(File(imgFile))

In the above code we checked the label value of the rectangle and if its target we place the file into that rectangle.

-Manan

-Manan