I remember that at some stage (CS4, I believe) you had to hide the Bookmarks panel before adding bookmarks (app.panels.item ('Bookmarks').visible = false;). I don't know if that'll help you but you'll find out soon enough.
Can you loop and keep trying until it works?
I recently had the same problem. I tried your solution and it seems to work.
For my part, I used another option in two steps :
1 - create all bookmarks and put their names in a label ;
2 - scan all bookmarks to set their names with thier label values if necessary.
Your solution is better. Thanks.
But did you file a bug? If not., well...
it's all the same with CS5. Same bug as in CS4 and CS3.
Hiding the panel, adding the bookmark, showing the panel.
That's the way it works.
What is really unfortunate with bookmarks:
If you have a couple of bookmarks and ask for myDoc.bookmarks.length you only get the ones at level 0.
You have to loop over bookmarks in bookmarks etc. to get them all. I'm missing a allBookmarks property ;-)
And when we're at it: is it possible to indent a bookmark scriptwise? The property is read-only, but who knows?
Plus: beware of all kind of spaces inside the text you use for the name property.
See example here of a RegEx to eliminate the problem:
In your code use:
curBookmark.name = myText.replace (/\s+/g, " ");
Otherwise you could end up with something like that: