Panels einblenden per Skript
For (almost?) all menu commands and even more – for example all tools in the tool palette – there are menuActions to trigger them. menuActions can be found like all collections: app.menuActions.itemByName('handle')
For instance, the conditional text panel has the handle: $ID/#CondTextUI_PanelName. The syntax is cryptic because the handle works in every language, even in a Japanese version of InDesign. So this works:
var ma = app.menuActions.item('$ID/#CondTextUI_PanelName');
if (ma.isValid) ma.invoke();
Problem: If the panel was visible before, it will no longer be visible afterward – and vice versa. What if I want to make sure it is shown?
Then this works:
var panel = app.panels.item('Bedingter Text');
if (panel.isValid) panel.visibility = true;
Problem: this, or course, only works in a German InDesign.
As far as I know, there is no explicit link between a panel and the menuAction that opens it.
So what to do?
- We record the visibility state of all panels.
- We trigger the
menuAction. - We check which panel changed its visibility.
- We set that panel’s visibility to the desired value.
panel_visibility();
/**
* Merkt sich Sichtbarkeit aller Panels. Toggelt ein Panel. Checkt, welches Panel sich geändert hat. Setzt dessen Sichtbarkeit.
* @param {string} menu_action
* @param {boolean} visibility
* @returns {any} null=menu_action invalid, true=visibility set, false=error on setting visibility
*/
function panel_visibility ( menu_action, visibility ) {
visibility = !! visibility;
var panels = app.panels.everyItem().getElements();
var vis = [];
for ( var n = 0; n < panels.length; n++ ) {
try {
vis[n] = panels[n].visible;
} catch(e) {
}
}
var ma = app.menuActions.item(menu_action);
if ( ma && ma.isValid ) {
ma.invoke();
} else {
return null;
}
for ( var n = 0; n < panels.length; n++ ) {
try {
if ( vis[n] !== panels[n].visible ) {
panels[n].visible = true;
return true;
}
} catch(e) {}
return false;
}
}
