You can write a proc that checks the selection for the object and if true, carries out the task, then call that proc using a scriptJob that leans on the "SelectionChanged" event. You can parent the scriptJob to your custom UI so it's killed when the UI is closed.