Convert to Button UXP script?
Been trying (coming on 20 hours now) to code a convertToButton function in UXP.
I thought I was don,e but then realized if the object I want to convert is double nested inside a group... and and/or inline in a text frame... it gets real messy real fast.
Hoping someone has already solved this in UXP. If not hopefully what i have will help someone else... i wouldn't have gotten as far as i did without the below post.
i've referred to this page:
https://community.adobe.com/t5/indesign-discussions/how-to-convert-textframe-to-button/td-p/2668958
@sreekarthikeyank and @Marc Autret both have replies that were helpful but I'm still struggling with this function. The functionality of convertToButton is exactly what i need as it doesn't pull the button out of the group its in. (which i tried to get clever and put it back but when its nested a bunch of different ways I get hosed.

Here is my UXP script so far. You'll find a few workarounds in there... some likely not needed due to ignorance on my part.
function GC_ConvertItemtoButton(itemYouLikeToConvert, buttonName) {
const originalParent = itemYouLikeToConvert.parent; // Capture the original parent group
let invisibleTextFrame;
// Check if selected item is a Rectangle
if (itemYouLikeToConvert.constructor.name === "Rectangle") {
invisibleTextFrame = doc.textFrames.add({geometricBounds: itemYouLikeToConvert.geometricBounds, contents: ""});
itemYouLikeToConvert = doc.groups.add([itemYouLikeToConvert, invisibleTextFrame]);
}
const myButton = doc.buttons.add();
myButton.name = buttonName;
const firstState = myButton.states.firstItem();
firstState.addItemsToState(itemYouLikeToConvert);
// Retrieve and remove the original (now duplicated) item from the button's state
const myoldItem = firstState.pageItems.firstItem();
const newitem = myoldItem.pageItems.firstItem();
newitem.remove();
// Remove invisible text frame if it was created
if (invisibleTextFrame) {
itemYouLikeToConvert.ungroup();
invisibleTextFrame.remove();
}
// Add the button back into the original parent group
// WARNING currently this doesn't work if the itemYouLIkeToConvert is nested more than once.
// I worked forever to get it working at one level but i need multiple levels
// AND ability for the object to be inline in a text field
if (originalParent.constructor.name === "Group") {
console.log('was enclosed in a group', originalParent.name);
GC_AddToGroup([myButton], originalParent);
// originalParent.groups.add(myButton);
} else {
console.log('not enclosed in a group', originalParent.name);
// Handle cases where the original parent is not a group
// For instance, add to the same page/spread
}
};
