2

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.

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221

2 Answers2

1

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.

pixelistik
  • 7,541
  • 3
  • 32
  • 42
Loic
  • 2,173
  • 10
  • 13
  • The problem is, even after `menu.remove(); delete menu;`, `mainmenu.submenus.items("Test").isValid` will still be `true` and the formerly removed menu will not show up until I restart InDesign, after which I have to add the menu items anew. – Tobias Kienzler Jun 23 '16 at 10:31
  • Hm, I never bothered to check where InDesign places a menu by default, good point though. – Tobias Kienzler Jun 23 '16 at 10:36
  • btw, if I omit the `LocationOptions.before`, the new menu is added _after_ "Help" – Tobias Kienzler Jul 04 '16 at 09:15
1

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 );
Jongware
  • 22,200
  • 8
  • 54
  • 100
Loic
  • 2,173
  • 10
  • 13
  • You're right, `isValid` changes - I must have had a global lying around that I meanwhile managed to `delete`. The remaining problem is still that the menu is not recreated without a restart... Or does that not happen for you if you run the script again? – Tobias Kienzler Jun 23 '16 at 13:41
  • btw according to [this post](https://forums.adobe.com/message/2183887#2183887) even a `#targetengine` is not persistent across InDesign restarts. There seems to be some more fundamental change to InDesigns menus themselves going on... – Tobias Kienzler Jun 23 '16 at 13:58
  • I created a ADD|REMOVE|ADD|REMOVE and can confirm the menu can be added after removal on my side. #targetengine is not persistent across InDesign restarts. Of course it's not but aren't you using your script into the startup scripts folder ? That may explain yours issues. – Loic Jun 23 '16 at 15:03
  • _Now_ we're getting closer: The difference lies between executing the script via startup or the ExtendScript toolkit, where this works, and executing it manually via InDesign's script menu, where this does not work - which was how I originally wanted to test it m-/ So, using startup, there actually _is_ no problem. Thanks for your feedback :) – Tobias Kienzler Jun 24 '16 at 06:26
  • However, the menu entry still remains in InDesign's menu settings... It's not that terrible, but now I will have a "Test" and "&Test" entry until I wipe InDesign. – Tobias Kienzler Jun 24 '16 at 06:29