Copy link to clipboard
Copied
Hi folks,
I want to add a context menu entry at runtime to 'Layout Context Menu', depending on the content of a frame. If a specific condition is true, the menu entry should be visible, otherwise not.
I tried by adding a 'beforeDisplay' event listener to the 'Layout Context Menu' and add the menu item, if the condition is true. Is this the suggested way to do this?
How can I find the related frames/rectangles for the concrete context menu to evaluate the condition?
Best reagrds, Berschmi
Copy link to clipboard
Copied
Berschmi,
I've never tried beforeDisplay on the context menu, but it's an interesting idea.
If it works and you're able to add a scriptMenuAction, I would think the first step for your beforeDisplay handler would be to look at app.selection. If it has only one item and that item is a textFrame, then you can proceed to examine the contents.
Bob
Copy link to clipboard
Copied
If your question is about finding the string key for the context menu you're interested in, take a look at this post:
If you're trying to get the frames that have been right clicked on, I don't think there's a way to do that (though I'd be interested to know if I'm wrong!). As far as I know, you can't get the mouse coordinates and translate them into the spread coordinates to get the page item you're interested in via the scripting DOM.
Checking selection won't work because you can right click on top of something without it being selected.
Best,
Lawrence
Copy link to clipboard
Copied
Well. OK. But.
If nothing is selected (the user has right-clicked without first selecting a text frame), the beforeDisplayhandler disables the scriptMenuAction in the context menu. Ditto if more than one page item is selected. Ditto if app.selection[0] is not a text frame. Unless you want to let the users select text inside a frame and then use the context menu to modify the frame. In other words, there are still many useful things that could be done by a script that requires the user to select something first.
Thanks for the link on how to identify the context menus.
Bob
Copy link to clipboard
Copied
Absolutely agree that lots of useful things can be done with a script that requires a selection, and that may be the only solution here without creating a C++ plug-in. Saying that it "won't work" was perhaps too broad - it will work, but it's not ideal. An ideal solution would show the correct context menu regardless of selection.
Copy link to clipboard
Copied
Items of the context menus won't show when disabled. It should therefor be sufficient to add the beforeDisplay listener to your menu item, and modify the action's .enabled property matching the app.selection.
Unlike context menus of palette panels, the window context menus consider the regular selection.
E.g. create a document with two text frames, one of them selected. Right-click the other one, choose "Cut". The selected item will get removed, not the right-clicked one.
For comparison, open the caracter styles panel. Create two styles, select one. Right click the other. The menu item "Edit …" will describe the "hit item" style below the click location rather than the highlighted / selected style. This behaviour is discussed in the thread linked by Lawrence.
For most actions I'd not follow the suggestion by Robert to only consider a selection with length 1. Typical actions iterate all selection items and apply only to the applicable items. E.g. select an empty graphics frame and two text frames. The context menu will still give you a "Text Frame Options…" dialog and apply the changes to the two text frames. Use the app.doScript method to combine all changes into a single undo step.