Skip to main content
Community Expert
February 13, 2023
Answered

Event listener to check slug area

  • February 13, 2023
  • 2 replies
  • 2432 views

I'm trying to write an event listener for opening documents to check slug area on the left and I can't get around a piece

 

But I keep getting an error that the object does not exist. 

 

I am flummoxed and it might be a bit beyond me at this point.

 

 

Even this is not working for me

var doc = app.activeDocument;
var slugBounds = doc.documentPreferences.slugBounds;

if (slugBounds[1] < 130) {
alert("The left slug area is less than 130mm.");
}

 

 

This topic has been closed for replies.
Correct answer rob day

Ok it's working - I get 2 prompts now.

 

Any way to reduce to just 1 prompt?


Hi Eugene, Not sure if this helps, but this simple after open script sends alerts that tell you what object is triggering the event:

 

 

#targetengine "session"

if(app.eventListeners.length > 0){
    for(var i = 0; i < app.eventListeners.length; i++){
        app.eventListeners.item(i).remove();
    }
}

app.addEventListener("afterOpen", docOpen);

function docOpen(e) {
    var d = e.parent.constructor.name
    alert("Event Object is: " + d)
    if (d == "LayoutWindow") {
        alert("The Document is Open and Ready");
    } 
};

 

2 replies

rob day
Community Expert
Community Expert
February 13, 2023

But I keep getting an error that the object does not exist.

 

Hi @Eugene Tyson , In case it isn’t completely clear, with scripts that include event listeners you usually have to start with a targetengine directive otherwise the listener won’t persist—the object doesn’t exist error is referring to the event listener object. Uncomment the line in Mark’s code:

 

@targetengine 'session'

 

 

 

 

Community Expert
February 13, 2023

I've restarted InDesign and now it doesn't trigger at all

 

Here's what I have so far

@targetengine 'session'

app.addEventListener("afterOpen", checkSlugBounds, false);

function checkSlugBounds(myEvent) {
var doc = app.documents[0];
var slugBounds = doc.documentPreferences.slugInsideOrLeftOffset;
if (slugBounds < 130) {
alert("The left slug area is less than 130mm.");
}
};

 

Thanks for all the help

rob day
Community Expert
Community Expert
February 13, 2023

Sorry I copied Mark’s line without looking it should start with # not @

 

#targetengine "session"

 

saschak38136531
Known Participant
February 13, 2023

try this:

var doc = app.activeDocument;

var slugBounds = doc.documentPreferences.slugInsideOrLeftOffset;

if (slugBounds < 130) {
alert("The left slug area is less than 130mm.");
}

Community Expert
February 13, 2023

That works! - anyway to run this script when a document is opened?

I've much to learn.

saschak38136531
Known Participant
February 13, 2023

In this case, you'll need a startup script. Here you'll find some answers:

https://community.adobe.com/t5/indesign-discussions/how-to-run-a-script-on-indesign-startup/td-p/2508947