Copy link to clipboard
Copied
Hey
I am adding an Event Listener, (beforeSave) to run a script on a document. The event is added to the Document while open within another script, and works swimmingly. However, I was assuming the event type was saved with the Document. However, it does not seem to be the case.
I can add a startup script, and get around it in other ways, but would be nice if I was able to have the event saved with the Doc.
app.activeDocument.addEventListener("beforeSave",lockAndLoad)
Thinking about it now, could it be because the event being fired is a function, that is no longer accessible to the beforeSave event.
I did try and put the function inline,
app.activeDocument.addEventListener("beforeSave",function(){
rawCode;......
})
but still didn't save
Am I asking too much?
Thanks in advance.
Roy
1 Correct answer
BarlaeDC wrote
… The reason I think this is that there is nothing on the opening of a document that checks the document itself for any scripts that are to be run, so there is nothing to call your code to add the event listener. …
Hi Malcom,
but there could be a startup script with a listener attached to an onIdle event that could look for executable code provided by the document. The code could be injected with method insertLabel( "key string" , "value string" ), extracted with extractLabel( "key
...Copy link to clipboard
Copied
HI,
I don't think it is possible to add code that lives inside the document that is opening and also can be run on itself, the only way is as you have noted to add a start up script that adds the event to the document.
The reason I think this is that there is nothing on the opening of a document that checks the document itself for any scripts that are to be run, so there is nothing to call your code to add the event listener.
Regards
Malcolm
Copy link to clipboard
Copied
BarlaeDC wrote
… The reason I think this is that there is nothing on the opening of a document that checks the document itself for any scripts that are to be run, so there is nothing to call your code to add the event listener. …
Hi Malcom,
but there could be a startup script with a listener attached to an onIdle event that could look for executable code provided by the document. The code could be injected with method insertLabel( "key string" , "value string" ), extracted with extractLabel( "key string" ) and then executed with a doScript() or eval().
Regards,
Uwe
Copy link to clipboard
Copied
Hi Laubender​,
Nice, hadn't thought of that, going to have to add that to my arsenal.
Regards
Malcolm
Copy link to clipboard
Copied
Another way to skin the same cat:
Use an afterOpen handler to check the document's metadataPreferences. There are several "fields" available there; I often use the "description". The script then runs predetermined handlers based on the description.
The afterOpen script itself runs at startup, so the afterOpen handler is checking metadataPreferences for everything that gets opened. When it sees a Description that you've enabled, it creates an afterSave event listener attached to the active LayoutWindow. You can, of course, use different afterSave handlers for different descriptions.
I've never tried storing the script language itself in the metadataPreferences. I just use different descriptive labels to attach predetermined handlers to the LayoutWindow.
The potential advantage of using metadataPreferences is that all users have access to the File Info panel and can change or delete the Description field if they want to change the way the script works or disable it altogether. Or is that a disadvantage?
Bob
Copy link to clipboard
Copied
Hi Bob,
try to get the limit ( if there is any ) of characters you can assign to the description field.
With insertLabel() there is a limit. Test this as well. Be careful if you reach an amount of about 25,000 characters.
Be aware that you can do an unlimited amount with insertLabel(), but only the maximum amount would stick.
Saving, closing, opening the document and then extractLabel() will proof that.
Regards,
Uwe