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

Script to add a bookmark to every page

Explorer ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

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

Views

4.8K

Translate

Translate

Report

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;

  

    }

}

Votes

Translate

Translate
Engaged ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

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;

  

    }

}

Votes

Translate

Translate

Report

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
Engaged ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

destination is a page object, so if you want to change the name of the bookmark go to InDesign ExtendScript API (12.0).

This a a great resource from Jongware. Use it to change the name property to something else.

Votes

Translate

Translate

Report

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
Explorer ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

Thanks Jake, that worked, brilliant. I do struggle to understand where to start, I'm new to Javascript. I have been fine adding shapes and images, but this was a step to far for me, thanks for the Jongware link, I have been using that loads, very useful.

I am getting one issue though, it seems to not want to name some of the bookmarks, just leaves them as Bookmark 1, see image, it's not a big problem though.

indesign_screen_shot.jpg

Votes

Translate

Translate

Report

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
Engaged ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

Hmm, that's strange. It worked ok for me.

Try adding this line before addBookmark ( pages 😞

doc.bookmarks.everyItem().remove();

This line removes all the bookmarks before adding them.

Votes

Translate

Translate

Report

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
Explorer ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

Jake, it seems like it is every other bookmark it does not name. I added the remove in, no joy, but I am testing it on a fresh new document, tried 10 pages this time, see screen shot:

indesign_screen_shot_2.jpg

Script:

var doc = app.activeDocument;

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

doc.bookmarks.everyItem().remove();

addBookmark ( pages );

function addBookmark (destination) {

  

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

        

    myBookmark = doc.bookmarks.add ( destination );

    myBookmark.name = "P" + i;

  

    }

}

Votes

Translate

Translate

Report

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
Engaged ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

The only other thing I can think of is that InDesign needs more time to change the name.

Add this line after myBookmark.name= "P" + i;

$.sleep(500);

Votes

Translate

Translate

Report

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
Explorer ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

Still failing to every other, its fine though. I am just going to strip the name out, I know bookmark 1 is page 1 etc. It is a little frustrating, I don't like it when things don't work that should.

Thanks your help Jake

Votes

Translate

Translate

Report

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 ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

Hi Jim,

I can confirm the issues with wrong naming of bookmarks.

However it's a bit strange * and I suspect a bug.

First time I ran the script on a document all seems to work as expected.
Then I added a new document and ran the script again: The issue came up.

* Even ran one version of the script where I added the name at creation time using an object with method add().
And asked the bookmark just created to write the name to the JavaScript console of the ESTK.


The name as I intended was written to the console, but the name in the document was still the wrong one.

Regards,
Uwe

Votes

Translate

Translate

Report

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
Explorer ,
Jul 06, 2017 Jul 06, 2017

Copy link to clipboard

Copied

Uwe

I saw the same, stepped through the script in debugging and it looks like it is naming the bookmark but then doesn't show in Indesign. As I said in my previous post I can cope with the name Bookmark1, 2 etc, but it would be nice to sort it.

Votes

Translate

Translate

Report

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 ,
Jul 06, 2017 Jul 06, 2017

Copy link to clipboard

Copied

Jim,

I think, I have an idea where the bug is.

The issues only occured if the Bookmarks Panel is open.

If the panel is closed naming was no problem.

Just tested that with InDesign CS6 v 8.1.0 on OS X 10.6.8.

Nevertheless we should report the bug here:

Wishform - Adobe InDesign

What exact version of InDesign and OS are you using?

Regards,
Uwe

Votes

Translate

Translate

Report

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
Explorer ,
Jul 06, 2017 Jul 06, 2017

Copy link to clipboard

Copied

Excellent work Uwe, I have just run it with panel closed and it worked fine. Thanks again, I will report it on the link provided.

Cheers

Votes

Translate

Translate

Report

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 ,
Jul 06, 2017 Jul 06, 2017

Copy link to clipboard

Copied

Jim,

you could close the Panel before:

var bookmarksPanel = app.panels.itemByName("$ID/Bookmark_PanelName");

bookmarksPanel.visible = false;

I used a locale independent string for the panel.

In my German InDesign I gathered this with:

app.findKeyStrings("Lesezeichen").join("\r");

/*

Returned array of strings:

$ID/Bookmark_PanelName

$ID/Bookmark_Tree

$ID/Bookmarks

$ID/$$$/Dialog/Behaviors/GoToView/Bookmark

*/

Regards,
Uwe

Votes

Translate

Translate

Report

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
Explorer ,
Jul 06, 2017 Jul 06, 2017

Copy link to clipboard

Copied

Uwe

Added in the close bookmark panel script, it works a treat, full proof now. Thanks again for your support on this.

Cheers

Jim

Votes

Translate

Translate

Report

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 ,
Jul 06, 2017 Jul 06, 2017

Copy link to clipboard

Copied

I tested with older versions.

InDesign CS3 is working fine.

The bug was introduced with InDesign CS4.

Tested with version 6.0.6.

I can see the bug also with CS5, CS5.5 and already tested CS6.

Regards,
Uwe

Votes

Translate

Translate

Report

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 ,
Jul 07, 2017 Jul 07, 2017

Copy link to clipboard

Copied

Correct. This problem has been mentioned here in the Script Forum multiple times over the past years (*). I find in a 2004 script of mine the following – dirty – trick:

titlebm = app.activeDocument.bookmarks.add(titlehd);

dummybm = app.activeDocument.bookmarks.add(titlehd);

titlebm.name = titlestr;

dummybm.remove();

I believe another suggestion was to introduce a delay by forcing a recompose, but since I got it to work I never felt it necessary to adjust that code.

(*) And apparently it has not been fixed.

Votes

Translate

Translate

Report

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 ,
Jul 07, 2017 Jul 07, 2017

Copy link to clipboard

Copied

Hi Jongware,

just tested your "dirty trick".

And it's working fine 🙂

Thanks,
Uwe

Votes

Translate

Translate

Report

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
Contributor ,
Jul 07, 2017 Jul 07, 2017

Copy link to clipboard

Copied

Hi all,

some time ago I also struggled with this issue in a script. You can also try:

myBookmark.move(LocationOptions.AT_END);

myBookmark.name = "P" + i;

Roland

Votes

Translate

Translate

Report

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 ,
Jul 07, 2017 Jul 07, 2017

Copy link to clipboard

Copied

Hi Roland,

moving a new bookmark before naming it works like a charm 🙂

If you are doing this with an open Bookmark Panel you can see that the panel is first filled with wrong names and then updates itself with the right ones.

var panelVisibility = true;

// "$ID/Bookmark_Tree" could be used as alternative to "$ID/Bookmark_PanelName"

var bookmarksPanel = app.panels.itemByName("$ID/Bookmark_PanelName");

//var bookmarksPanel = app.panels.itemByName("$ID/Bookmark_Tree");

bookmarksPanel.visible = panelVisibility;

// Add a new document:

var pagesLength = 100;

var doc = app.documents.add

(

    {

        documentPreferences : { pagesPerDocument : pagesLength }

    }

);

// Do a loop for adding bookmarks with a Page Destination

// and a name that is derived from the name of the destination:

for ( var n = 0; n < pagesLength; n++ )

{

    // Perhaps that is not necessary:

    var page = doc.pages.getElements()[0];

    var bmk = doc.bookmarks.add

    (

        {

            destination : page

        }

    );

    // Suggested WORKAROUND by Roland Dreger:

    bmk.move(LocationOptions.AT_END);

    bmk.name = "P-"+page.name;

};

Thanks,
Uwe

Votes

Translate

Translate

Report

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
New Here ,
May 09, 2022 May 09, 2022

Copy link to clipboard

Copied

I can confirm that the bug still exists in InDesign 2022 17.2.1 !

Votes

Translate

Translate

Report

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 ,
May 09, 2022 May 09, 2022

Copy link to clipboard

Copied

Hi Gregory,

I had no doubt about that …

 

Thanks for testing!

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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
Explorer ,
Jul 07, 2017 Jul 07, 2017

Copy link to clipboard

Copied

Uwe

I have changed it to "$ID/Bookmark_Tree", "$ID/Bookmark_PanelName" hides the whole of the interactive panel and you have to reselect it from the Window's menu, new code below:

var doc = app.activeDocument;

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

var bookmarksPanel = app.panels.itemByName("$ID/Bookmark_Tree");

bookmarksPanel.visible = false;

doc.bookmarks.everyItem().remove();

addBookmark ( pages );

function addBookmark (destination) {

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

    myBookmark = doc.bookmarks.add ( destination );

    myBookmark.name = "P" + (i+1);

    }

}

Votes

Translate

Translate

Report

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 ,
Jul 07, 2017 Jul 07, 2017

Copy link to clipboard

Copied

jimelevens  wrote:

Uwe

I have changed it to "$ID/Bookmark_Tree", "$ID/Bookmark_PanelName" hides the whole of the interactive panel and you have to reselect it from the Window's menu …

Hi Jim,

interesting.

I cannot see any difference in using "$ID/Bookmark_Tree" or "$ID/Bookmark_PanelName" on my OS X InDesign.

Maybe because I set the panel to visible = true at the end of the script?

Regards,

Uwe

Votes

Translate

Translate

Report

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
Explorer ,
Jul 06, 2017 Jul 06, 2017

Copy link to clipboard

Copied

I'm using CC 2015.4 on a Windows 7 machine.

Votes

Translate

Translate

Report

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
Enthusiast ,
Jul 06, 2017 Jul 06, 2017

Copy link to clipboard

Copied

Try this,

myBookmark.showBookmark();

myBookmark.name = "P" + i;

Votes

Translate

Translate

Report

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