Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Close Document event not firing when executed from an action

New Here ,
Dec 07, 2022 Dec 07, 2022

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!

TOPICS
Actions and scripting , macOS , Windows
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Mentor ,
Dec 07, 2022 Dec 07, 2022

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)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 08, 2022 Dec 08, 2022

I've been finding that this notification event is not shooting off consistently - is that possible?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 08, 2022 Dec 08, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Dec 08, 2022 Dec 08, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 08, 2022 Dec 08, 2022

Is this possible? My understanding is that it only shoots off upon completion. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Dec 08, 2022 Dec 08, 2022

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
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 08, 2022 Dec 08, 2022

Amazing thank you so much for this! I will try this out. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 08, 2022 Dec 08, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Dec 08, 2022 Dec 08, 2022
LATEST

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 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines