Anybody have any “Event Listener” script code that I can paste in to an existing script?

1,406 views
Skip to first unread message

Stephen Marsh

unread,
Jan 7, 2015, 9:16:13 PM1/7/15
to indesi...@googlegroups.com
I have a script, it works well when manually run. Thing is, it would be good not to have to remember to run it all the time!

It would be good to automate this script so that it runs on any open document. If the conditions in the script are not met nothing happens, so it would not matter if this script was run against every opened file (although there may be a better way, such as only running the file on documents that have a certain filename, or perhaps metadata etc, I am open to suggestions).

Apparently an InDesign “startup script” can use an “even handler”. One such event handler being when a document is open, the script is triggered to run.

I don’t know how to extendscript/javascript - so I am hoping for a simple “scriptlet” that I can plug into the existing script code.

Can anybody help? Thank you in advance.


Stephen Marsh

Olav Martin Kvern

unread,
Jan 8, 2015, 5:09:51 PM1/8/15
to indesi...@googlegroups.com
Hi Stephen,

The scripting documentation has a few examples of how to do this. You can get all of the scripts from:

http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/indesign/sdk/cs6/scripting/scriptingguide.zip

...these are for CS6 (Adobe seems to be trying to hide the CC versions, for reasons I don't understand), but nothing in this area has changed. Download and unzip the archive, then look at the examples inside the scriptingguide/scripts/javascript/events folder. (They're in a somewhat odd structure, which was intended for an automated tutorial/publishing system that was never implemented, but I think you'll get the idea.)

In particular, take a look at the AfterNew.jsx script. There are three things to look at:

* First, the line:

#targetengine "session"

...directs the script to an ExtendScript engine that persists after the script is run. If you don't do this, there's a good chance that your event handler function will not exist when the event occurs.

* Next, the line:

var myEventListener = app.eventListeners.add("afterNew", myAfterNewHandler);

...creates an event listener for the "afterNew" event (also known as Event.AFTER_NEW, Document.AFTER_NEW, or DocumentEvent.AFTER_NEW). This event listener specifies that, when the event fires, the myAfterNewHandler function should execute.

* Finally, the function "myAfterNewHandler" performs tasks when the event fires. In this example, the function adds metadata and creates some objects in the slug area of the document.

Thanks,

Ole

Stephen Marsh

unread,
Jan 8, 2015, 7:09:38 PM1/8/15
to indesi...@googlegroups.com
Ole, thank you for the reply!

You may be overestimating my ability to script, which is next to non-existent, I can sometimes hack/change existing code or perhaps combine elements from two different scripts if I am lucky!

I’ll give the info you provided a look.

I was hoping for simple “premade” code that I could plug in, however after spending a lot of time trawling various sites I am out of luck.

This of course brings me back to my long standing rant regarding InDesign programming and product management. Nobody can deny the success and usefulness of Actions and the resulting “ecosystem” built around them in Photoshop. Same for the Photoshop “Script Events Manager”, which brings a GUI interface to event driven scripts/actions. But when it comes to InDesign, what do we have? Only scripts! No Actions. No Script Events Manager. All we have is scripting. Scripting is powerful, better than actions - however if the user does not know scripting and finds it hard to learn, they are out of luck.

Thanks again for the help, I’ll let the list know how I go!


Stephen Marsh

Stephen Marsh

unread,
Jan 9, 2015, 8:22:30 AM1/9/15
to indesi...@googlegroups.com
Olav,

I have downloaded and looked through the documentation and examples at the link kindly posted. Thank you. As expected, it did not help me, which is no fault of yours!

I have managed to get this far, by hacking a script that I finally found at the Adobe forums that works with “afterOpen”.

The problems I have so far and minor, but annoying (Mac OS and CS6):

1) When InDesign is run each time, I receive a warning dialog box stating that the startup script generated the following error “no document is open”. I understand why this is happening, however it is less than ideal. I am offered a choice of disabling the event handler, yes or no. No is the obvious choice. I can of course tick the “don’t show again” option, however I am hoping that there may be another event that I could use or a work around to remove this error without having to use the “don’t show again” to hide the error.

2) There is a 6 second or so delay while ExtendScript Toolkit is launched, before the script runs. A dialog appears “You are about to run a script in Adobe InDesign CS6. You should only run scripts from a trusted source. Do you want to run the script? Yes or No?”

I don’t have to answer this ExtendScript Toolkit message, the script runs on the open document fine in InDesign – the delay is not good though. I don’t know why this has to happen in the first place. This is painful and may cause impatient users to think that the program has locked up. When the script is run from the Scripts Panel it runs instantly. So the 6 second delay is either due to the startup script/event implementation or ExtendScript Toolkit kicking in. Why is and is there a way to stop ExtendScript Toolkit from being launched? I have looked in Preferences, however nothing leaps out at me. A google search did not help either…

Script snippet below:

#target indesign // It appears that this line is optional? I have tried removing it and there is no difference in execution
#targetengine "session"  
var myEventListener = app.eventListeners.add("afterOpen", myDocOpen, false);  
function myDocOpen(myEvent)  
{  
     
var myDoc = app.documents[0];    
// Insert the script code that would usually be run manually from the GUI here…



Thanks again for your time and patience Ole!


Stephen Marsh

Olav Martin Kvern

unread,
Jan 9, 2015, 4:36:14 PM1/9/15
to indesi...@googlegroups.com
Hi Stephen,

I think that removing the "#targetengine indesign" line will prevent the ExtendScript Toolkit from launching.

To get rid of the error on startup, you'll need to handle it in the script. This line is looking for an open document:


var myDoc = app.documents[0];

...when there's no document open, InDesign throws an error. There are a few ways around this--probably the easiest is to put:

if(app.documents.length == 0){
    return;
}

...before that line.

Let me know if these changes work for you! (I'm on digest, so there might be a bit of lag in my response.)

I also have comments on Actions vs. scripting, but they'll have to wait for another day.

Thanks,

Ole

Dave Saunders

unread,
Jan 9, 2015, 5:54:31 PM1/9/15
to indesi...@googlegroups.com
I've retired to the extent that I don't even have the software to try stuff any more, but I do remember problems like this with the afterOpen event. It gets triggered too early so you'd think there's a document to reference but there isn't.

If memory serves, you need to work with window objects. Once a window is open, there must be an associated document.

Dave

--
You received this message because you are subscribed to the Google Groups "InDesign talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to indesign-tal...@googlegroups.com.
To post to this group, send email to indesi...@googlegroups.com.
Visit this group at http://groups.google.com/group/indesign-talk.
For more options, visit https://groups.google.com/d/optout.

Stephen Marsh

unread,
Jan 10, 2015, 7:42:32 PM1/10/15
to indesi...@googlegroups.com
I would just like to publicly thank Olav for his help in getting my script working correctly.

Thanks again Ole, I really appreciate your time and effort mate!


Stephen Marsh
Reply all
Reply to author
Forward
0 new messages