Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to add a bookmark to every page

Explorer ,
Jul 05, 2017 Jul 05, 2017

My javascripting has come on a long way recently, creating buttons, adding images etc, but I am totally stumped.

I have been trying for a while to create a script to add a bookmark to every page within an Indesign document to use in go to destination buttons. I can create these manually, but that is not time efficient when you have to 100+. I haver found script that add to paragraph etc. but not the page. I am totally stumped, I planned to place the add bookmark script within a loop like this:

var myDoc = app.activeDocument;

var myDocL = myDoc.pages.length;

for (i=0; i<myDocL; i++){

}

Any help would be greatly appreciated

TOPICS
Scripting
7.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Jul 05, 2017 Jul 05, 2017

try this:

var doc = app.activeDocument;

var pages = doc.pages.everyItem().getElements();

addBookmark ( pages );

function addBookmark (destination) {

  

    for ( var i = 0; i < destination.length; i++ ) {

        

    myBookmark = doc.bookmarks.add ( destination );

    myBookmark.name = destination.name;

  

    }

}

Translate
Enthusiast ,
Aug 08, 2025 Aug 08, 2025

Minor correction to this code to get it to work.

destination needs an array value to work in the loop destination[i]

var doc = app.activeDocument;
var pages = doc.pages.everyItem().getElements();
addBookmark(pages);
function addBookmark(destination) {
    for (var i = 0; i < destination.length; i++) {
        myBookmark = doc.bookmarks.add(destination[i]);
        myBookmark.name = destination[i].name;
    }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 10, 2025 Aug 10, 2025
LATEST

Hi @wckdtall ,

thanks for correcting this. The cause of the missing iterator was a switch of forum software some years ago that wrecked a lot of code samples in the forum.

 

Thanks,
Uwe Laubender
( Adobe Community Expert )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 20, 2023 Jun 20, 2023

Great post! I had written a script to add bookmarks based on a list of desired bookmark names and pages but I was getting the "every other name" bug. After finding this thread I closed bookmarks panel and my script worked perfectly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 20, 2023 Jun 20, 2023

Hi @1234wqesfdc ,

so the bug is still there...

What's your exact version of InDesign on what version of OS?

 

Thanks,
Uwe Laubender
( Adobe Community Expert )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 20, 2023 Jun 20, 2023

I tested 2 systems and the problem happens on both. Every other (alternating) bookmark name is wrong if script is run with bookmarks panel is open. Close panel, script runs fine. 
Windows 10, InDesign 18.2.1

Mac OS 10.14.4, InDesign 14.0.2

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 20, 2023 Jun 20, 2023

It's a super basic script. I'll put it here for reference. To test, just make a blank document with at least 3 pages in it and then run the script.

var BMlist=[
["name1",1],
["name2",2],
["name3",3]
];

var mydoc=app.documents[0];
var bmd;//bookmark data pulled from BMlist
var bm;//actual bookmark
for (var i=0; i<BMlist.length; i++){
bmd=BMlist[i];
bm=mydoc.bookmarks.add(mydoc.pages[bmd[1]-1]);
bm.name=bmd[0];
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines