Copy link to clipboard
Copied
I have a script that creates a listener via the following code:
app.notifiers.add('Cls ', onClose);
app.notifiersEnabled = true;
It works fine when I use Ctrl W to close the document.
However when I trigger an Action that does:
Save + Close
It doesn't seem like that Close Document event is fired even though the document does close.
Any insight into this would be amazing!
Copy link to clipboard
Copied
Yes that's right. The launch of a script, action, plug-in is perceived by Photoshop as an independent event. Notifications about actions that occur within this event are not received.
There is no easy solution - it all depends on your script. The easiest way is to keep track of the IDs (or number) of open documents BEFORE the action is run, and compare them to what's left AFTER the action runs.
Action completion notification can be obtained with:
app.notifiers.add('Ply ', handler)
Copy link to clipboard
Copied
I've been finding that this notification event is not shooting off consistently - is that possible?
Copy link to clipboard
Copied
You don't need that. You have code that closes the document, so you already know you are doing so. Just add in whatever routine you want run when the document is closed.
Copy link to clipboard
Copied
By the way, an alternative option - if we received a notification about the execution of an action, then we can simply read the list of commands in the active action and find out whether the close command was in it or not.
Copy link to clipboard
Copied
Is this possible? My understanding is that it only shoots off upon completion.
Copy link to clipboard
Copied
Yes, it's fired when the action is ended. With app.notifiers.add('Ply ', handler) we get a notification about this. At the same time, the last launched action remains active on the action panel, then we simply go through all its commands and look for whether there is a 'close' command there (taking into account localization). For example:
#target photoshop
var s2t = stringIDToTypeID;
try { var evt = arguments[0] } catch (e) { }
if (evt) {
var action = evt.getReference(s2t('target')).getName(),
set = evt.getReference(s2t('target')).getContainer().getName();
(r = new ActionReference()).putName(s2t('action'), action);
r.putName(s2t('actionSet'), set);
var numberOfChildren =executeActionGet(r).getInteger(s2t('numberOfChildren')),
found = false;
for (var i = 1; i <= numberOfChildren; i++) {
(r = new ActionReference()).putIndex(s2t('command'), i),
r.putName(s2t('action'), action);
r.putName(s2t('actionSet'), set);
if (executeActionGet(r).getString(s2t('name')) == localize("$$$/Actions/Event/Close")) {
found = true;
break;
}
}
alert ('Fired Set: ' + set +'\nAction: '+ action + (found? '\n\n Close command found!' : '\n\n Close command not found!'))
} else {
app.notifiers.add('Ply ', File($.fileName))
app.notifiersEnabled = true
}
Copy link to clipboard
Copied
Amazing thank you so much for this! I will try this out.
Copy link to clipboard
Copied
I'm trying to build an autoloader that will open 1 document at a time in a directory and will automatically open the next one when the previous one has closed. I'm not quite sure how you would go about doing this without listeners.
Copy link to clipboard
Copied
I have an old script that I wrote for similar tasks - advanced batch. The script keeps track of both simple closes and closing of the document after the action has been launched. You can see how it is implemented (or perhaps the script will suit you in the form in which it is).
P.S. my coding style was very different two years ago and now I can hardly figure it out myself 🙂
Find more inspiration, events, and resources on the new Adobe Community
Explore Now