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

UXP | appplication -> afterSave -> eventListener | e.target.name

New Here ,
Sep 01, 2024 Sep 01, 2024

Copy link to clipboard

Copied

Hi, when I try to reference e.target.name (should be the document name) from an afterSave event i get nothing.

 

I'm logging 3 lines but since "e" is messed up when I log just "e" I can't access anything, when I try "e.target" the whole line isn't even logged.

 

video: https://streamable.com/ispiob

 

This is for a UXP plugin.

Here is the relevant code:

import {app} from "indesign";
import {uxp} from "./globals";

async function saveDoc(e) {
    console.log("saveDoc has been triggered.");
    console.log(e);
    console.log(e.target);
}

function addEventListener() {
    app.eventListeners.everyItem().remove();
    app.eventListeners.add('afterSave', saveDoc)
    app.eventListeners.add('afterSaveAs', saveDoc)
}

uxp.entrypoints.setup({
    plugin: {
        create() {
            addEventListener()
        },
    },
});

export const App = () => {
    return (
        <>
       ...
        </>
    );
};

 

 Thanks!

TOPICS
UXP Scripting

Views

199

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
Guide ,
Sep 01, 2024 Sep 01, 2024

Copy link to clipboard

Copied

In short: try without async.

 

If that does not work:

 

Back in 18.5 I tracked down a similar problem from the C++ plug-in side.

The main thread of InDesign creates the event object referring to intermediate storage on the target, passes it on to the subscribed handlers and destroys the intermediate storage on return. It also considers flags set by the handler callback by means of event.preventDefault() and alike. Even if you remember the event object during your handler e.g. by storing it in a global (similar to your use of console.log), the backing data will be gone.

Now add UXP's general async processing in a separate thread (not your use of async). This means the event callback is pushed into some queue across the threads, the main thread continues immediately and again cleans up the intermediate storage before the UXP thread comes along for processing.

Only (much) later on the InDesign main thread becomes ready to handle any other script request sent by UXP, including your getter for the event property. The event object is still there waiting for garbage collection but anything backing is already gone, so it is likely throwing an uncaught exception - wrap that other console.log with a try-catch.

Event handlers in UXP can only work if the native main thread event processing itself (the original caller of the event callback) would be busy-waiting for the async handler in UXP thread to do its thing, on the way process all further script requests incoming from UXP such as your property getter but also anything else (remember, UXP could be running plenty other async activity before its separate queue reaches the handler callback). Such a processing was completely missing back when I checked in 18.5, and I don't know whether it was added since then.

For my UXP plugin I was lucky and could work without event handlers at all, besides my business logic still sits in ExtendScript (where event handlers continue to work) and only communicates with UXP for its UI. I'd suggest the same (use ExtendScript via app.doScript) until the problem is resolved.

Note that it could already be resolved for synchronous handlers, but by declaring your handler function async you actually are asking for async processing with all associated troubles. So first try without explicit async.

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 ,
Sep 02, 2024 Sep 02, 2024

Copy link to clipboard

Copied

I removed all async / await - same issue.

I tried calling extendscript, the first line works, the second gives this error:

Error: while running Plugin entrypoint on Plugin Id: zig.bolt.uxp.test.plugin, Entrypoint: create,  Error: Expected: )
    at o.create (VM2144 index-BP-kTQdn.js:47)
    at o._createPlugin (uxp://uxp-internal/pluginmanager_scripts.js:2)
    at Object.setup (uxp://uxp-internal/pluginmanager_scripts.js:2)
    at VM2144 index-BP-kTQdn.js:47
    at e.exports.execScript (uxp://uxp-internal/domjs_scripts.js:2)
    at l (uxp://uxp-internal/domjs_scripts.js:2)
    at uxp://uxp-internal/domjs_scripts.js:2
    at o._executeCallbacks (uxp://uxp-internal/domjs_scripts.js:2)
    at o._executeIfReady (uxp://uxp-internal/domjs_scripts.js:2)
    at Array.<anonymous> (uxp://uxp-internal/domjs_scripts.js:2)

 

Here is the code:

uxp.entrypoints.setup({
    plugin: {
        create() {
            console.log("Plugin has been loaded, plugin. create has been triggered.");
            app.doScript("alert(app.eventListeners.length)", 1246973031);
            app.doScript("function event() {alert(1)}; app.eventListeners.add('afterSave', event}", 1246973031);
            // addEventListener()
            // connect()
        },
    },
});

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
Guide ,
Sep 02, 2024 Sep 02, 2024

Copy link to clipboard

Copied

Better write the ExtendScript code in a separate file and use a debugger (VS code), at least until you have it going.

That second doScript() is is missing a targetengine directive to produce a persistent targetengine, and the add() call is closed with a curly bracket.

Here one discussion about targetengine, from the related discussions shown when I view this page

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
Guide ,
Sep 02, 2024 Sep 02, 2024

Copy link to clipboard

Copied

LATEST

If you want to have a look at the event object within your event() function, you cann add an argument "e", then alert(e.properties.toSource())

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