I've created many tools that use the "NewBookmark" menu item to create bookmarks. It is the only way from JS to create bookmarks with destinations. But it is tricky because of timing issues. The bookmark is not created synchronously when "app.execMenuItem" is called. The menu item execution happens asynchronously. So the newly created menu item may not be available to JS code for an unknown amount of time. So it's best not to look for then new item right after creating it. The technique described below handles this issue by creating and manipulating the bookmarks in different loops. Not perfect but it usually works.
To create the bookmark tree with JavaScript you'll need to know the pattern of the tree up front. The most important bit is the pages that need bookmarks. As a simple example, you could use an array, where each entry in the array is another array containing the zero based page number and a name. like this: var BKData = [ [0,"title page"], [1, "TOC"], [5, "intro"] ]
The code should work as follows:
1. Loop on array to create a flat set of unnamed bookmarks
for(var i=0;i<BKData.length;i++)
{
this.pageNum = BKData[0];
app.execMenuItem("NewBookmark",this);
}
2. Loop on bookmarks to set names
var bkMarks = this.bookmarkRoot.children;
for(var i=0;i<bkMarks.length;i++)
{
bkMarks.name = BKData[1];
}
The "NewBookmark" menu item creates the new bookmark at the bookmark with the current focus. The assumption in this code is that there are no bookmarks initially, so the new bookmarks are created from the top in order in which they appear in the "BKData" array. And they are the only bookmarks in the document.
You can make this much fancier by putting hierarchy data, such as a level number, into the "BKData", Then adding a 3rd loop to sort the bookmarks into the correct hierarchy.