Tobias Kienzler
Tobias Kienzler

Reputation: 27403

How to remove a (sub)menu and all its traces of existence?

Using the code

#targetengine "mine"
var mainmenu = app.menus.items("$ID/Main");
var menu = mainmenu.submenus.add("Test");

I added a submenu to InDesign's menu, that I later removed via

menu.remove();
delete menu;

Unfortunately, when I try to recreate the menu, nothing happens since InDesign seems to think it is still there - until I restart InDesign, after which the menu appears (but without any items added). How can I permanently remove the menu such that it could be recreated later on without having to restart InDesign? The menu also still shows up in InDesign's menu preferences (worse yet, even twice since I tried "Test" and "&Test" as menu names), which I would like to fix as well...

edit Turns out this weird behaviour only occurs when executing the script via InDesign's script panel, but neither via startup nor the ExtendScript toolkit. However, the menu entries still remain after removal.

Upvotes: 2

Views: 882

Answers (2)

Loic
Loic

Reputation: 2193

When you use a persistent session, it's good practice to double check what you are doing. In your case that means checking for menu possible preexistence before trying to add it.

#targetengine "mine"
var mainmenu = app.menus.item("$ID/Main");

var menu = mainmenu.submenus.item("Test");
!menu.isValid && menu = mainmenu.submenus.add("Test", LocationOptions.before, mainmenu.submenus.item("$ID/#Keyboard_Help"));

//comment ON/OFF to remove or not the menu
//menu.remove();

Secondary point, I don't think you need to set a location option as any menu added in the main menu bar can't be placed after the help menu. Unless I am wrong InDesign will always place the help menu on last position on the right.

Upvotes: 1

Loic
Loic

Reputation: 2193

It's likely that you create numerous references to the menu object in previous attempts and has it's a persistent engine, references stay alive.

Restart InDesign and it should do it.

Here are some screenshots I made from the attached script. isValid varies from true to false depending of the stage of the script and menu existence.

The menu has been added and is valid The menu has been removed and is no longer valid

#targetengine "mine"
var mainmenu = app.menus.item("$ID/Main");

var menu = mainmenu.submenus.item("Test");
!menu.isValid && menu = mainmenu.submenus.add("Test", LocationOptions.before, mainmenu.submenus.item("$ID/#Keyboard_Help"));
alert( menu.isValid );
//comment ON/OFF to remove or not the menu
menu.remove();

alert( menu.isValid );

Upvotes: 1

Related Questions