Skip navigation
Currently Being Moderated

ScriptUI palette doesn't display when created inside an event listener

Apr 18, 2012 10:14 AM

The script below creates a script menu action, adds an item to InDesign's Window menu, which, when selected, invokes an event listener. This listener in turn creates a ScriptUI palette if necessary and shows it. But the problem is that the palette isn't shown. When I create a dialog instead of a palette it works fine. And if I first create the palette in a separate script, close it, and then run the below script, everything works as expected (the palette is shown). It looks as if I don't understand some interaction between target engines, event listeners, and palettes. Hope anyone can enlighten me.

 

Thanks,

 

Peter

 

 

#target indesign;
#targetengine "Grrrr";
 
var utils = app.menus.item("Main").menuElements.item("Window").menuElements.item("Utilities");
var sma = app.scriptMenuActions.add("No break");
 
sma.addEventListener('onInvoke',
    function()
        {
        var w = Window.find ('palette', 'NoBreak');
        if (w == null)
            {
            var w = new Window ('palette', 'NoBreak')
                w.orientation = "row";
                w.add ('statictext', undefined, "+/\u2212");
                w.add ('statictext', undefined, "No break");
            }
        w.show();
        }
    );
 
app.menus.item("$ID/Main").submenus.item("$ID/&Window").menuItems.add(sma, LocationOptions.after, utils);
 
Replies
  • Currently Being Moderated
    Apr 18, 2012 10:22 AM   in reply to Peter Kahrel

    I think similar issues came up in the past. I don't remember details. What happens if you create the dialog as a global variable?

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 18, 2012 12:43 PM   in reply to Peter Kahrel

    Hi Peter,

     

    I think the variable w—which by the way is redeclared in your code—should be declared outside of the anonymous event handler, i.e. at the session scope level. The reason for that is not obvious to me, but I suspect that the garbage collection mechanism automatically destroys the palette as soon as the last reference to it is out of scope (which is the case of w so far). My assumption is the following: since a palette needs a persistent scope to work, the reference to the palette objet needs to be persistent too. (Maybe…)

     

    @+

    Marc

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 18, 2012 1:14 PM   in reply to Peter Kahrel

    Peter,

     

    Just declare w outside the anonymous function:

     

    #target indesign
    #targetengine "Grrrr"
     
    var
        utils = app.menus.item("Main").menuElements.item("Window").menuElements.item("Utilities"),
        sma = app.scriptMenuActions.add("No break"),
        w;
     
    sma.addEventListener('onInvoke',
        function()
            {
            w = Window.find ('palette', 'NoBreak');
            if (w == null)
                {
                w = new Window ('palette', 'NoBreak')
                    w.orientation = "row";
                    w.add ('statictext', undefined, "+/\u2212");
                    w.add ('statictext', undefined, "No break");
                }
            w.show();
            }
        );
     
    app.menus.item("$ID/Main").submenus.item("$ID/&Window").menuItems.add(sma, LocationOptions.after, utils);
    

     

    It worked fine for me.

     

    HTH

     

    --

    Marijan (tomaxxi)

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points