Adobe Acrobat: Summoning Bookmark Properties Results in Error 1708

Per Acrobat AppleScript commands dictionary, the code below should work but it won’t:

tell application "Adobe Acrobat" to tell document 1 to get destination page number of bookmark 20

The document in question has one thousand and a half bookmarks, and in Acrobat parlance, bookmarks correspond to the familiar TOC metaphor. I have another PDF that I want to copy the TOC (in the form of “bookmarks”) from to the PDF I’m working with. However, the destination page number property that I need, to make the identical bookmark in the latter, errors out, and so do fit type, destination rectangle and zoom factor.

Does anybody have one or two magical solutions in the toolbox?

Hi scrutinizer82.

It could be something to do with the document itself. I only have Adobe Acrobat Reader, but changing the app name to that, your script works perfectly well with the latest manuals for BBEdit and my car, but errors with an older Apple PDF which also has TOC bookmarks. In the last case, the bookmarks can be found by script, but trying to read the properties you mention gets an error. So the document’s age or creator, perhaps?

Hello scrutinizer82
everyone in this community,
Nice to meet you all. This is my first post, and since English isn’t my strong suit, I may not be able to provide detailed explanations. However, I’d like to suggest an alternative.
Just Bookmark ROOT only
I hope you find it helpful.

Thank you.

tell application "Adobe Acrobat"
	tell active doc
		set numCntBkmRoot to (do script ("this.bookmarkRoot.children.length")) as integer
		log "BookMarkROOTitems : " & numCntBkmRoot
	end tell
	repeat with itemIntNo from 0 to (numCntBkmRoot - 1) by 1
		tell active doc
			set strBookMarkName to (do script ("this.bookmarkRoot.children[" & itemIntNo & "].name")) as text
			log "BookMarkName :" & strBookMarkName
			do script ("this.bookmarkRoot.children[" & itemIntNo & "].execute();")
		end tell
		delay 0.2
		tell front PDF Window
			set strPageNO to (page number) as text
			log "BookMarkGoToPageNO: " & strPageNO
		end tell
	end repeat
end tell

AcrobatJavascriptAPI
Example: Dump all bookmarks in the document.

Ironically, the PDF I used as a testing ground to source a TOC was the Acrobat manual by Adobe, PDF specification version 1.6.

I’m a greenhorn concerning all things JavaScript. Applying only conventional wisdom and standard logic out of experience in AppleScript and general shell scripting, I can catch the overall meaning of it. It tells me the snippet refers to the members of the array of bookmarks. Still, I need a valid reference for every member to make a copy thereof in the other document, preferably using AppleScript. What would you suggest?

Using a combination of IceFole’s suggestion (Hi @IceFole. Welcome to MacScripter!) and information gleaned from the linked Web site, here’s a script that works with all three of the PDFs I mentioned above. It returns an AppleScript list of lists, each sublist containing a bookmark name and destination page number. I don’t know how useful an AppleScript result will be for @scrutinizer82’s ultimate aim, but it’s been fun working it out. :grin:

The returned page numbers are 1-based and correspond to the PDF’s page numbers, which may not necessarily be the same as those in the text.

set JS to "function getBmkNamesAndPages(bmk) {
	bmk.execute();
	var output = '{\"' + bmk.name + '\", ' + (this.pageNum + 1) + '}';
	if (bmk.children != null) {
		for (var i = 0; i < bmk.children.length; i++)
			output += ', ' + getBmkNamesAndPages(bmk.children[i]); 
	}
	return output ;
}
'{' + getBmkNamesAndPages(this.bookmarkRoot) + '}';"

tell application "Adobe Acrobat"
	tell active doc to set jsResult to do script JS
end tell

set bookmarkNamesAndPages to (run script jsResult)'s rest
1 Like

Mr. Nigel Garvey,

Nice to meet you. Thank you for your comment and alternative suggestion. It’s very simple, and even nested bookmarks of nearly 300 are processed in just a few seconds, plus the return value is a list, which is very convenient to use. I’ll make use of it.
Thank you