Skip to main content
May 7, 2008
Question

(CS3) Does anyone know how to place request for...

  • May 7, 2008
  • 14 replies
  • 890 views
I was wondering if anyone knew the process to request change or ask for improvement.

I am using EventListener object and it seems that the beforeClose event run the script AFTER asking to save the file. So I had to use beforeSave but this is not quite what I want. I prefer my script to run BEFORE saving the file at closing time and if I choose to CANCEL the save, my document won't get closed on me!

So the beforeClose.event should RUN the script associated with, THEN ask to save the file if it needs to!!!!
Alex.
This topic has been closed for replies.

14 replies

August 11, 2008
Thank you Olev.

Regarding the Menu Actions, it make sense. So no shortcut, no action to associate to.

As for the app.eventListener. I don't know either. Why would a script work with menuAction & manually? And not if associated to an app.eventListener instead. Beats me.
The one I tried is linked to Open. In the case of app.eventListeners, it will open, execute the whole script and let me do anything afterwards, until I close the document. Then InDesign hangs up. To make sure nothing else was the culprit, I only defined that 1 event.

Alex.
Known Participant
August 11, 2008
Hi Alex,

re: "But any defined to app.eventListeners crashes InDesign when closing the document!"

That doesn't happen for me, so I have no idea what's wrong.

re: "My question is can you assign an action to "Open Recent"?"

I don't know. My first thought would be that there isn't really a menu item for Open Recent--just a submenu, and you can't associate event listeners with submenus (the quick way to tell is to look at the keyboard shortcuts editor--if the menu/submenu is not available for a keyboard shortcut, then you probably can't add an event listener to it). Next, attaching event listeners to the menu items on the Open Recent submenu is going to be very fragile.

re: "You can have 2 different actions pointing to the same function. Can you? ie: Open & Open Recent"

Yes, you can attach as many event listeners as you want. I believe they'll be processed in the order in which they were created, but I've never tested to be certain.

Thanks,

Ole
August 11, 2008
Olev,

Good afternoon,

Scripts have been working great. But any defined to app.eventListeners crashes InDesign when closing the document!

My question is can you assign an action to "Open Recent"?
I tried:
app.menuActions.item("Open Recent").addEventListener("afterInvoke",EventOpen, false);
But got an error.

You can have 2 different actions pointing to the same function. Can you? ie: Open & Open Recent

Thank you, Alex
May 12, 2008
Olav,

It works w/ the exception of Closing the actual Window. And I did figured out the menuAction.eventListeners[xxx].remove.

Thanks for your help.

I am still working on having the Open Stable. This HAS to be an app.eventListener because we open file using a program that passes the document as an argument to InDesign. As long as as we don't close the document, everything is OK. As soon as we close it, InDesign crashes. The script works fine when triggered manual.
Alex.

Debug Result
------------
Main ();
...
app.menuActions.item("Close").addEventListener("beforeInvoke",EventClose, false);
...

function EventClose (itsEvent)
{

var myExeSrcFile = new File (app.scriptArgs.get("Event_Close"));
if (myExeSrcFile.exists)
{
myExeSrcFile.open ('r:(read)');
app.doScript(myExeSrcFile, ScriptLanguage.javascript);
myExeSrcFile.close();
}
else
alert ("Error! Missing File:\n\n" + myExeSrcFile.fsName);

return;
}
May 9, 2008
Olav,

Quick question. How do I remove the actionMenu.item("Close") eventListners?
I am doing some testing as well

====================================
Event Script

#targetengine "session"
app.menuActions.item("Close").addEventListener("beforeInvoke",EventClose, false);

I am trying to remove that event.
May 8, 2008
Olav,

Actualy, it came to me AGAIN, just to associate to the menuActions. Later I can looks into scriptMenuAction. Same but different location and trigger.
But the close of the window would still be an issue OR a way to close without running the script. Which could be handy.

Thanks, Alex.
May 8, 2008
Olav,

It came to me, maybe I should associate my script to scriptMenuAction("beforeInvoke", function....)"

At the moment (Abbreviated version), my STARTUP script

#target indesign
#targetengine "session"
app.scriptPreferences.version = 5.0;
var myScriptPath = app.activeScript.path;
app.scriptArgs.set("Event_Close", myScriptPath + "/PrePressCLOSE.jsx");
app.addEventListener( "beforeSave", EventClose, true);
//*** END ***
function EventClose (itsEvent)
{
app.scriptArgs.set("Event_Listener", itsEvent.parent.toSource()); //*** PASS Argument

var myExeSrcFile = new File (app.scriptArgs.get("Event_Close"));
if (myExeSrcFile.exists)
{
myExeSrcFile.open ('r:(read)')
app.doScript(myExeSrcFile, ScriptLanguage.javascript);
myExeSrcFile.close();
app.scriptArgs.set("Event_Listener", ""); //*** RESET Argument
//alert ("Finished Execution");
}
else
alert ("Error! Missing File:\n\n" + myExeSrcFile.fsName);

app.scriptArgs.set("Event_Listener", ""); //*** Reset

return;
}
===============
Passing the Parent as an argument allows my script to be run manually or by eventListener.

Script: PrePressClose.jsx

try
{
if (app.scriptArgs.get("Event_Listener") == "")
var myDoc = app.activeDocument;
else
{
var myDoc = eval(app.scriptArgs.get("Event_Listener"));
}
}
catch (e)
{
var myDoc = app.activeDocument;
}
..... more code .....
try
{
if (app.scriptArgs.get("Event_Listener") == "")
myDoc.close();
}
catch (e)
{
myDoc.close();
}
//**** END ***

If this is unclear I can post the WHOLE Startup and Close scripts.

Thanks, Alex.
May 8, 2008
Olav,
Thanks for being so quick
I guess I would replace after myEvent.stopPropagation()with my doScript... portion?
Yes, we usualy close using CTRL-F4 or CTRL-W. But there are always the possibilty of the ones closing the window.

I still wonder why the code was written that way. Logicaly, I would think the event.beforeClose should be trapped BEFORE actually closing the document, since it's BEFORE.

Do we agree or another reason is escaping me?
Alex.
Known Participant
May 8, 2008
Hi Alexandre,

The trouble with trying to disable the close event is that we can't--when you look at the event (using the EventListenersOn.jsx script), you can see that the event is not cancelable. So, instead, we can trap the close menu action. The following will take care of attempts to close the document using File>Close or Command-W/Ctrl-W.

//InterceptCloseMenuAction.jsx
//An InDesign CS3 JavaScript
#targetengine "session"
main();
function main(){
var myCloseMenuAction = app.menuActions.item("Close");
var myEventListener = myCloseMenuAction.eventListeners.add("beforeInvoke",myBeforeInvokeHandler, false);
}
function myBeforeInvokeHandler(myEvent){
myEvent.preventDefault();
myEvent.stopPropagation();
var myResult = confirm("Do you want to save before closing?", false, "Close Document");
if(myResult == true){
app.documents.item(0).close(SaveOptions.ask);
}
else{
app.documents.item(0).close(SaveOptions.no);
}
}

This doesn't deal with closing the window by clicking the window's close box, though--so I have more thinking/experimenting to do.

Thanks,

Ole
May 8, 2008
Olav,

Have you seen my other topic about limiting the scope of a search to all the textframes selected in CS2?

Topic
[JS] (CS2) How to limit scope of search

Alexandre VERCAMMEN, "[JS] (CS2) How to limit scope of search" #, 7 May 2008 11:11 am