UXP Plugin roadblock with reading layer properties

I’m trying to create my first UXP-based InDesign plugin and I have run into a snag trying to retrieve layer properties. I’ve reduced my code to be very basic to show the issue that I’m encountering.

const app = require("indesign");

function get_name_button () {
    try {
        alert(app.activeDocument.layers[0].name);
    } catch (error) {
        alert(error);
    }
}

document.getElementById("get_name_button").addEventListener("click", get_name_button);

When I click on the button it returns this error:

TypeError: Cannot read properties of undefined (reading name)

If I change the code to

alert(app.activeDocument.layers.length);

It alerts a value of 7 as there are 7 layers in the test document. It sees the layers but I’m not able to retrieve the layer properties for any of the layers.

Does anyone have any idea what the problem could be?

Hi @MikeBaugh I’m a raw beginner at UXP, but what if you try:

app.activeDocument.layers.item(0).name

I seem to recall UXP preferred this approach, but I’m very likely wrong.

  • Mark
1 Like

Hi m1b,

Thank you for the reply. When I change to ( ) I get this error:

TypeError: app.activeDocument.layers is not a function

My idea was to try the item method of Layers. I think you left out .item

1 Like

Ah, you’re correct. that worked. Thank you! I really appreciate it.

1 Like

There are some other gotchas if you are coming from an ExtendScript environment. This link has the .item as its first one:

2 Likes

Also, UXP doesn’t have the alert() function.

1 Like

I mean … it does have it. It’s just not very stable in InDesign at the moment :wink: .

I seem to recall UXP preferred this approach, but I’m very likely wrong.

It’s in fact the only way to address items in collections.

2 Likes

I mean … it does have it. It’s just not very stable in InDesign at the moment

Aha – do there’s hope!

2 Likes

medium_jon, I really appreciate the resource link. Thank you!

2 Likes

I don’t know if I want to get my hopes up, yet. But I recently tried it and it worked ~ 80 % of the time. So it’s not production-ready, yet (especially since I couldn’t figure out a pattern on when it works, so it seems to also fall into this "weird side-effects of UXP interacting with ExtendScript stuff in asynchronous contexts). But there’s definitely some implementation.

Hey Zuri,

I watched your YouTube video First Attempts with UXP Plugins in InDesign: Demo and Lessons Learned and found it really helpful. Thank you for posting it!

2 Likes