Skip to main content
Legend
February 5, 2018
Answered

How to remove an event handler?

  • February 5, 2018
  • 2 replies
  • 3204 views

Is there a supported way to remove an app event handler?

Sample snippet:

f = function(event){

     if(event.object instanceof Document && event.type == 'loaded'){

          //do stuff

          }

     return {handled:false};

     }

app.eventHandlers.push({handler:f});

So this works great, I have a floating palette and want to force a display refresh when the user changes folders in the Bridge browser window. I tried registering the click event in the palette (where the event handler can be removed) but window events don't work right or even the same cross-platform.

My problem is that after closing the palette, the app event handler and function f() remain active and continue to fire. So every time the browser loads, f() is executed.

I tried a window.onClose() event but can't figure out how to remove the handler (beyond deleting app.eventHandlers[0] which risks deleting an event handler from a different script.) Redefining the function f() doesn't work. Adobe's documentation only talks about setting this handler and that its a simple array entry, not how to remove it. No listing of methods and nothing in Event Model Viewer.

Any ideas?

This topic has been closed for replies.
Correct answer Kukurykus

With closing palette you use, additional code can be triggered, that wlll set environmental variable to true. And vice versa, when you open palette again, that variable will be set to false. This way when you close palette, handler won't work untill you open it again. I assume you saved your handler code inside Bridge Scripts folder, right? while palette script is separate code (in Bridge Scripts of Common Files that are in Program Files folder). At least it's how it should be done that worked.

Here you have example - two scripts: Handler and Switch. Study them to use your way in your codes (with get / set env):

f = function(event) {

     if (!$.getenv('h') && event.type == 'loaded'

     && event.object instanceof Document) {

          alert('Hello!')

     }

     return {handled:false}

}

app.eventHandlers.push({handler:f})

#target bridge

onoff = new MenuElement('command',

'onoff', 'at the end of Thumbnail')

onoff.onSelect = function() {

     if ($.getenv('h')) {

          $.setenv('h', '')

     }

     else {

          $.setenv('h', ' ')

     }

}

The only thing I don't understand why Handler is executed twice (you'll see it in my example when 'Hello!' pops up 2 times). Maybe there is double refreshment like first normal, and then another when current folder gets selection? - I have no idea!)

2 replies

bagonterman
Inspiring
February 8, 2019

Cached folders cause this event to fire twice but I don't know how to get around.

Legend
February 8, 2019

I have a bug filed where event handlers aren't removed from the array when I use pop.

Kukurykus
KukurykusCorrect answer
Legend
February 5, 2018

With closing palette you use, additional code can be triggered, that wlll set environmental variable to true. And vice versa, when you open palette again, that variable will be set to false. This way when you close palette, handler won't work untill you open it again. I assume you saved your handler code inside Bridge Scripts folder, right? while palette script is separate code (in Bridge Scripts of Common Files that are in Program Files folder). At least it's how it should be done that worked.

Here you have example - two scripts: Handler and Switch. Study them to use your way in your codes (with get / set env):

f = function(event) {

     if (!$.getenv('h') && event.type == 'loaded'

     && event.object instanceof Document) {

          alert('Hello!')

     }

     return {handled:false}

}

app.eventHandlers.push({handler:f})

#target bridge

onoff = new MenuElement('command',

'onoff', 'at the end of Thumbnail')

onoff.onSelect = function() {

     if ($.getenv('h')) {

          $.setenv('h', '')

     }

     else {

          $.setenv('h', ' ')

     }

}

The only thing I don't understand why Handler is executed twice (you'll see it in my example when 'Hello!' pops up 2 times). Maybe there is double refreshment like first normal, and then another when current folder gets selection? - I have no idea!)

Legend
February 5, 2018

Yeah it looks like a variable is the best bet. I can use array.pop() or delete to remove the array entry but I would risk deleting another script's handler. I tried walking the array but can't figure out how to match the elements.

I'm trying to show a confirm dialog when the user selects a Collection, to choose an export folder. Once the palette is closed, I don't want a dialog popping up.

Kukurykus
Legend
February 5, 2018

I never worked in Bridge with handlers, as I even didn't know they are part of. I played with them only in Photoshop, but never met similar problem like this you came with. That was interesting challange for me. Like I said closing palette sets variable to true, so handler won't work untill your user call palette again what will trigger handler as well and so on. This action won't affect other handlers, at least there's no visible risk Test it and tell me how that worked, so I'll use it too...