Skip to main content
Participating Frequently
January 27, 2026
Question

InDesign JavaScript: MenuActions "action is not enabled" error

  • January 27, 2026
  • 5 replies
  • 38 views

Hello,

 

I am writing a script to automatically perform a data merge on a template that contains a table of content that needs to be updated.

I have written the following function for updating the TOC, which works fine:

    function updateTOCs(doc) {
var TOC = doc.textFrames.itemByName("TOC");
if(TOC == "[object TextFrame]"){
TOC.select();
app.menuActions.item("$ID/UpdateTableOfContentsCmd").invoke();}
else($.writeln("There is no text frame called TOC"));
}

However, it doesn't work when I iterate through the merged documents like so:

for (var i = mergeDocs.length - 1; i >= 0; i--){
var openDoc = app.documents[i];
updateTOCs(openDoc);
}

This gives the error “Action is not enabled”.

The TOC.select() seems to work fine. The only thing I can think of that might be a problem is that the app.documents[i] is not set to be the active document, would this matter? If so, how would I go about this? Or any idea what the problem could be otherwise?

 

Cheers,

Heidi

5 replies

rob day
Community Expert
Community Expert
January 28, 2026

Hi ​@Jurre37971467hccf , Here is a capture from the document’s createTOC() method—the 2nd parameter is a replace boolean

 

 

Participating Frequently
January 27, 2026

While others have mentioned createTOC() should be used, I just wanted to post that I managed to get the menu action to work in the loop by changing the for-loop as follows:

for (var i = mergeDocs.length - 1; i >= 0; i--){
app.activeDocument = app.documents[i];
updateTOCs(app.activeDocument);
}

In case it helps anyone.

Peter Kahrel
Community Expert
Community Expert
January 27, 2026

As Manan mentioned, no need to select anything. In fact, selecting things and invoking menu commands should be done only if there aren’t direct script commands.

 

createTOC() is a function of the document, not of a text frame.

Participating Frequently
January 27, 2026

Thanks Peter, I was only using it because I thought it could not be done otherwise. I did not know createTOC() could be used for updating, I’ll look into it.

Community Expert
January 27, 2026

I suppose you don’t need to either find the TOC frame nor execute the menu. You can call the createTOC method it has an argument which says replacing if set to true should update the existing TOC. See the details at https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html#d1e49740__d1e53775

-Manan
Participating Frequently
January 27, 2026

Hi Manan, thanks, unfortunately the URL does not seem to be pointing to the relevant web page (it says Not Found), would you mind posting it again?

Community Expert
January 28, 2026
Community Expert
January 27, 2026
function updateTOCs(doc) {
if (!doc) return;

// Find the text frame named "TOC"
var tocFrame = doc.textFrames.itemByName("TOC");
if (!tocFrame.isValid) {
$.writeln("No text frame named 'TOC' in document: " + doc.name);
return;
}

// Check if this text frame has a TableOfContents object
if (tocFrame.tablesOfContents.length === 0) {
$.writeln("No TOC found in text frame 'TOC' in document: " + doc.name);
return;
}

// Update all TOCs in this frame
for (var i = 0; i < tocFrame.tablesOfContents.length; i++) {
tocFrame.tablesOfContents[i].update();
$.writeln("Updated TOC in document: " + doc.name);
}
}

// Iterate through all open documents
for (var i = 0; i < app.documents.length; i++) {
updateTOCs(app.documents[i]);
}


Not the best at this - but just to add a few bits see if it works for you now

Participating Frequently
January 27, 2026

Thanks Eugene, this gives me the error (in line 12):

I have personally never used .tablesOfContents. Anyway it gives the error also when performing it on a single document.

Community Expert
January 27, 2026

I’ll have to leave you in better hands - I think I know what’s causing it but I’m only guessing. ​@Peter Kahrel and ​@Manan Joshi are better suited here.