Showcase (maybe a thread for more showcases?): First attempts with UXP plugins in InDesign

Hey, everyone!

While I wouldn’t exactly say that I’m happy about the reason for it (no longer working for Adobe after my contract expired a month ago), this means that I now have more time to develop exciting side projects again. And, of course, I had to use this opportunity to play around with UXP for InDesign (while I’m much more familiar with XD and the DVA space, I’ve also used InDesign for projects in the past, so ) :grin:

Showcase Video

It isn’t much, but I’ve already learned a few quirks about using UXP in InDesign, so I thought it might be valuable to some of you who come from UXP in a different host app to put these things into a short video:

Quick overview of the lessons I learned developing this first mini plugin:

  • InDesign’s DOM works somewhat differently compared to other UXP host apps. From what I’ve seen, I think this can add some convenience but takes some getting used to (e.g., bounds being [y1, x1, y2, x2] instead of [x1, y1, x2, y2] or [x, y, width, height])
  • from what I’ve seen, the InDesign equivalent of XD’s require('application').editDocument() and Photoshop’s require('photoshop').core.executeAsModal() is:
    app.doScript(() => {
        // ...
    }, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, historyName);
    
  • app.activeDocument.colors.itemByName(name) returns a Color regardless of if it exists or not (the same applies for other functions in this style). To check if it actually exists (and doesn’t have to get added to the set of colors first), you can’t check if it’s undefined, but have to check its isValid property:
    let color = app.activeDocument.colors.itemByName(name);
    if (!color.isValid) {
        console.log(`Creating color ${name} because it doesn't exist`);
        color = app.activeDocument.colors.add({
            name: name,
            colorValue: colorValue,
            parentColorGroup: getOrCreateColorGroup()
        });
    }
    return color;
    
  • You can already get good TypeScript support by adding a types.d.ts that contains the UXP types (e.g., from Add type declarations for UXP 7.1 by Apoorva2405 · Pull Request #66017 · DefinitelyTyped/DefinitelyTyped · GitHub) and combining them with the InDesign types from Types-for-Adobe/InDesign/2021/index.d.ts at e5b482f30ef55a6800e038c6d1467c111a0a68f9 · docsforadobe/Types-for-Adobe · GitHub, wrapped in a declare module 'indesign' { ... } with the original declare keywords removed.

Now’s your turn!

I’m sure I’m not the only one trying their first steps of getting into the UXP in InDesign-world these days, so I’m curious what y’all have been up to (and lessons learned from that). So I’m hoping that maybe we can make this a showcase/lessons learned thread around first steps in using UXP in this “new” host app.

Happy coding,
Zuri

6 Likes

Thanks for the typescript info in that! And glad to see you still around here, Zuri.

1 Like

I’m glad you’re glad since the fact that I no longer have access to (and thus, no longer have to monitor) way too many internal Slack channels probably means you’ll see me around here more frequently, again :stuck_out_tongue_winking_eye:.

Jokes aside, I’ll probably share a ready-to-use type declaration file (that’s ready to be pasted into any project) as soon as I have played around with InDesign-based UXP a bit more (probably sometime in the next 4 days) :blush: .

1 Like

Another thing I’ve learned: This code can be used to listen to selection changes in plugins:

const app = require('indesign');

app.eventListeners.add('afterSelectionChanged', () => {
	console.log(`Selection changed: ${app.selection.length} items selected`);
});

And … I’m happy to say that the test plugin just became a real plugin (and potentially the first ever published open-source UXP-based InDesign plugin? :sweat_smile: ) :tada:!

The code (while still a bit messy as I wanted to get the job done today, although I guess it’s already the new day given that it’s 3:30 AM on my end right now) is available at GitHub - pklaschka/indesign-pride-flagger: A UXP-based plugin for Adobe InDesign that allows you to add pride flags to your documents. if you want to take a look.

I’ve also submitted the first version to the marketplace, so I’m now waiting patiently for the review team :blush:.

And with that, I’m off to bed :grin: .

3 Likes

Hope you slept well, Zuri! This indeed is great and the first ever UXP InDesign Plugin that I am aware of. Can’t wait to see this live in the Marketplace

2 Likes

Hot damn, this is awesome, Zuri!

2 Likes

Nice work Zuri! Great to see you spreading wisdom around UXP and this time for Indesign.

2 Likes

Thanks, everyone! :blush:. Now all I need to do to get this into the marketplace is to buy a Mac Mini to debug weird issues that only happen on macOS :stuck_out_tongue_winking_eye: . Jokes aside, this is exactly what I meant above with

It’s still early days for UXP in InDesign (which, in more than one place, is also quite noticeable), which means it’s totally understandable that some weird issues occur and we as the developer community still have to figure out best practices around it. So I would (and I’ll do the same) ask/encourage that we make a habit of documenting things like those in forum threads so that we can all benefit from each other and build the best plugins we can :blush: !

And … pride flagger just got approved on the marketplace :tada:

https://exchange.adobe.com/apps/cc/ae7b0450/pride-flagger

3 Likes

Curious what the reason for the crashes were?

1 Like

That makes two of us :sweat_smile: .

Here’s what I know:

  1. I was never able to reproduce the exact crashing / freezing reported by the engineering team
  2. A few days after the rejection, my Mac Mini testing machine (that I had ordered, also for other projects) arrived and I was able to do some deeper testing
  3. I found a bunch of problems on macOS with things like segmentation faults happening sporadically (that “sporadically” is the key part since it meant there’s no reliable way of testing if it’s fixed or not, only proabilities)
  4. Crucially, I was able to replicate these problems with the starter plugin, as well. So it didn’t seem to be one particular thing my plugin did :thinking:
  5. Adobe confirmed that this wasn’t a problem with my plugin, but with how InDesign handles the plugin, and a fix for this will likely arrive in v19.0.
  6. I tinkered around for many hours on macOS, eventually leading me to several changes, including (most prominently) fix(main): Check selection on an interval instead of on selection change · pklaschka/indesign-pride-flagger@b0da463 · GitHub.
  7. With that change, an intensive 8-hour testing session (because we’re dealing with probabilities and not a clear way to replicate, that was the only way to get some confidence that it might actually be fixed) went smoothly. So the change, at the very least, severely decreases the chance of problems occurring, and I can only hope that this also “covers” the ones encountered by the review team.
  8. I (because of the testing session, I currently believe that the plugin is mostly stable :crossed_fingers: ) resubmitted the new version for review and it got approved.

So hopefully, it won’t be a concern as soon as v19.0 comes out. But after embarking on this journey, I wanted to go through with having the first ever published UXP based plugin for InDesign, so I was keen on releasing for v18.5, which is why I played around with it for many more hours. And while I can’t say for sure that it completely fixes it (in fact, based on me having these issues with the starter plugin, as well, I doubt that), the changes I was able to make seem to at least have made it a lot less likely to happen (and hopefully, cover the very bad complete freeze/crash that I was never able to replicate in the first place).

3 Likes