Copy link to clipboard
Copied
Hi,
Can anyone please help me?
I'm trying to create a bookmark from a text selection. When running the script from indesign, in 80% of the tests, the name will not change to the selected text. In 20% of the cases it does change the name. I can't find any reason why. There is no pattern. No error occurs when running the script. When encapsulation the script in a try/catch, no exception is thrown.
When running the script from extendscript toolkit it ALWAYS works. It's driving me nuts.
The code:
var myText = app.selection[0].contents;
var myDoc = app.activeDocument;
var currPage = app.selection[0].parentTextFrames[0].parentPage;
var curBookmark = myDoc.bookmarks.add(currPage);
curBookmark.label = myText;
curBookmark.name = myText;
Any help would be great.
John
Copy link to clipboard
Copied
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.
Peter
Copy link to clipboard
Copied
@Peter:
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.
@All:
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:
http://forums.adobe.com/message/1106334#1106334
In your code use:
curBookmark.name = myText.replace (/\s+/g, " ");
Otherwise you could end up with something like that:

Uwe
Copy link to clipboard
Copied
Can you loop and keep trying until it works?
Copy link to clipboard
Copied
Hi,
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.
--
Manu
Copy link to clipboard
Copied
CS5, eh? This 3-yr-old post described the same problem for CS3 ...
Copy link to clipboard
Copied
But did you file a bug? If not., well...
Find more inspiration, events, and resources on the new Adobe Community
Explore Now