Copy link to clipboard
Copied
I'm creating a button with:
var myRevealButton = myPage.buttons.add();
This creates an "empty" button consisting of a small rectangle.
Then I add some artwork (a group of InDesign objects) to the first state:
myRevealButton.states[0].addItemsToState(myRevealButtonArt);
But when I try to remove the small rectangle automatically created by myPage.buttons.add(), I'm unable to do so (Object is invalid).
myRevealButton.states[0].rectangles[0].remove();
Am I doing something wrong, or is this a bug? The rectangle shows up clearly in the Layers panel, and is visible on-screen, and I can delete it normally, but not via scripting.
On closer inspection,
myRevealButton.states[0].rectangles.length = 0?
Any ideas?
All items in a button state are grouped. When you add an object to a state of a button, it's added to the group inside the button.
So myRevealButton.states.item(0).addItemsToState(myRevealButtonArt);
give you an object: myRevealButton.states.item(0).groups.item(0).pageItems.item(1); (or myRevealButton.states.item(0).groups.item(0).groups.item(0) if you know it was a group)
To remove the default rectangle do the following:
myRevealButton.states.item(0).groups.item(0).rectangles.item(0).remove();
Copy link to clipboard
Copied
@Keith – strange, indeed.
And it's not limited to InDesign CS6.
Same thing in InDesign CS5. Just tested…
The length on rectangles in the first state after adding the button seems to be 0, but the length of the pageItems is: 1.
So I grabbed the ID of the pageItem, stored it in a variable and removed the pageItem after adding the group.
That worked.
The pageItem was removed.
But with it the whole button as well…
Very strange…
Uwe
Copy link to clipboard
Copied
All items in a button state are grouped. When you add an object to a state of a button, it's added to the group inside the button.
So myRevealButton.states.item(0).addItemsToState(myRevealButtonArt);
give you an object: myRevealButton.states.item(0).groups.item(0).pageItems.item(1); (or myRevealButton.states.item(0).groups.item(0).groups.item(0) if you know it was a group)
To remove the default rectangle do the following:
myRevealButton.states.item(0).groups.item(0).rectangles.item(0).remove();
Copy link to clipboard
Copied
@Harbs – that is working. Thank you…
I was mislead by the layers panel in the UI that is showing one object, the [Normal] object, that obviously represents a state, where in the DOM we have two objects, that resemble that object:
the "State" Object plus its inherit "Group" object.
In removing pageItems[0] of states[0] I removed one component that composes a state representation in the UI. In fact the whole state was removed. And a button without a state cannot exist.
So in effect the whole button was removed.
Does that make sense?
Uwe
Copy link to clipboard
Copied
Does that make sense?
I think so.
Copy link to clipboard
Copied
Thanks to both of you for looking into this! Indeed, the rectangle is part of the group, as Harbs solution works. I was mislead by the lack of a visible group in the Layers panel.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more