0

In the Adobe InDesign CS6 Scripting Guide: JavaScript (link to pdf), in Chapter 8: Events on page 136 it says,

eventListeners that use handler functions defined inside the script (rather than in an external file) must use #targetengine "session". If the script is run using #targetengine "main" (the default), the function is not available when the event occurs, and the script generates an error.

The error looks like this:

The requested action could not be completed because the object no longer exists.

Using a #targetengine is not an option for my script, so I'm stuck with using an external file reference for the event handler.

My problem is this: how do I access the event object in the external file event handler? I've tried app.scriptArgs.get("event") and checking typeof event to no avail. My event handler is useless without knowing the target of the event.

dln385
  • 11,630
  • 12
  • 48
  • 58

1 Answers1

1

The event is available in the local scope through the evt variable.

As an example, place these two files in the same folder:

activateEventHandler.jsx

var parentFolder = (new File($.fileName)).parent;
var eventHandlerFile = new File(parentFolder.fsName + "/onEvent.jsx");
var eventListener = app.addEventListener("afterNew", eventHandlerFile);
app.documents.add();
eventListener.remove();

onEvent.jsx

alert("Event '" + evt.eventType + "' with target " + evt.target.constructor.name);

When you run activateEventHandler.jsx, you should see an alert with the text

Event 'afterNew' with target Document

dln385
  • 11,630
  • 12
  • 48
  • 58