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

Setting geometricBounds via script

New Here ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Hi,
Very new to scripting in InDesign, so there may be a simple explanation here. Any help appreciated.
The following is an extract from a script that adds a text frame for each pageItem and then positions it over the top of each pageItem (I then go on to feed some text into it). It works perfectly well for image frames and shapes, but for some reason for grouped items and text frames it places the new frames in the top left of the page. Can anyone explain why geometricBounds behaves differently for these items and/or how to get this working correctly? Changing to visibleBounds doesn't seem to make a difference.
(i is looping all pageItems that were on the page at the start of the process)

 

    tagframe = myPage.textFrames.add();
    tagframe.geometricBounds = myPage.pageItems.item(i).geometricBounds;

 

I don't actually need the new text frames to match up exactly on top of the reference object, but it does need to be relatively clear from their position which object they were 'based on'.

Thanks,

Josh

TOPICS
Scripting

Views

1.1K

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 , Oct 26, 2020 Oct 26, 2020

.pageItems wouldn’t get items inside of groups so you might try using .allPageitems and skip the group objects in the loop. Something like this:

 

var doc = app.activeDocument
var p= doc.allPageItems;

for (var i = 0; i < p.length; i++){
    if (p[i].constructor.name != "Group") {
        var gb = p[i].geometricBounds
        doc.textFrames.add({geometricBounds:gb, contents:"YO", fillColor:"None"});
    } 
};   

 

Votes

Translate

Translate
People's Champ ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

geometricBounds works the same for Groups as it does for TextFrames, so the problem is elsewhere.

What type of PageItem is your pageItems.item(i) exactly?

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
New Here ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

So pageItems.item(i) is just running through all pageItems on a loop. This is the issue: it runs through all the image frames, polygons, lines etc on the page OK, but for some reason doesn't apply the geometricBounds correctly for text frames or groups.

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 ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

.pageItems wouldn’t get items inside of groups so you might try using .allPageitems and skip the group objects in the loop. Something like this:

 

var doc = app.activeDocument
var p= doc.allPageItems;

for (var i = 0; i < p.length; i++){
    if (p[i].constructor.name != "Group") {
        var gb = p[i].geometricBounds
        doc.textFrames.add({geometricBounds:gb, contents:"YO", fillColor:"None"});
    } 
};   

 

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
New Here ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

That's all useful to know, thank you – but should something running through pageItems still pick up any groups (and their geometricBounds), even if not the individual grouped items?

I guess my question is essentially: why is "myPage.pageItems.item(i).geometricBounds" behaving differently when the item in question happens to be a text frame or a group? The script is behaving as if in those cases, it returns 0 (rather than actually failing).

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 ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

LATEST

.pagetems returns a pageItem class, so you should be able to get the bounds even when the page item is a group. So here there are only 2 page items—the group is selected so I can see the bounds:

 

Screen Shot 13.png

 

allPageItems returns 5 items including the group

 

Screen Shot 14.png

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