Need Help with JXA Script Using Bookends

Thank you - that script looks quite interesting indeed

Question on JXA syntax - with a bit of help I have gotten some Bookends JXA scripts working.

The pertinent issues I am pondering in one script refers to item.format() in the script below which applies a bibliography format to each item I am displaying. The Javascript dictionary entry says it is possible to to format "as HTML" or "as RTF" but I cannot figure out the syntax for that. I tried item.format.html() and that failed. How do I interpret this dictionary entry?

 pb.forEach(item => {
   titlelist=titlelist+item.format()  +"\n"+"\n";
  return titlelist;

Your post (and replies) have been moved to a new Topic since you have asked a new question. Please follow this policy in the future.


1 Like

My best suggestion is to explore the Bookends object model in AppleScript using Script Debugger 8.

1 Like


Script Debugger would of course help to understand the Applescript syntax

That is actually well documented in the Dictionary - what I cannot figure out is how to do something similar to "as HTML" or "as RTF" in Javascript:

tell application "Bookends"
    set myPubs to every publication item of front library window whose title contains "immunotherapy"
    if myPubs is not {} then
        set plainRefs to format myPubs -- Defaults to the default format specified in Bookends.
        set plainRefs2 to format myPubs using "Vancouver.fmt"
        set rtfRefs to format myPubs using "APA 6th Edition.fmt" as RTF
        set bibtexRefs to format myPubs using "BibTeX.fmt" as BibTeX
        set htmlRefs to format myPubs using "Nature.fmt" as HTML
    end if
end tell

First, keep it very simple when trying to understand syntax.
So just have one command, outside of any loop:

const appBE = Application("Bookends");
const oMyPubs = appBE.libraryWindow[0].publicationItem[0];
const rtfRefs = appBE.format(oMyPubs, 
    with {using: "APA 6th Edition.fmt",  as: "RTF"};

I've never used Bookends, so I'm just guessing at some of the syntax.
But that should give you some ideas.

Have you been thru these: JXA Resources
Especially the first video by Sal.

Many thanks - I will give that a try

yes I saw your excellent collection of materials and I am working my way through those - much appreciated

FYI ... what works is...

format({using: "text", as: "plain text"})

Thanks for all your help

@rkaplan, would you mind proving a full, executable, example for all of us to see and reference?

Yes - certainly

Many thanks @JMichaelTX for your help here and also @chrillek on the Devonthink Forum - both are incredibly knowledgable resources about automation in general and JXA in particular.

This is a working example of a Javascript snippet which can be used in TextExpander to retrieve citation titles from Bookends formatted using any Bookends .fmt format and as plain text / HTML /RTF as desired

(() => {
  const app = Application("Bookends");  
  const w = app.libraryWindows()[0];
  const pb = w.selectedPublicationItems();
  pb.forEach(item => {
 titlelist=titlelist+item.format({using: "BibTex.fmt", as:"plain text"})  +"\n"+"\n";
  return titlelist;
1 Like

FWIW, one alternative to declaring a variable like titleList
(and then having to repeatedly mutate its value)
is to:

  • define a list with .map (rather than .forEach)
  • and then a define a string consisting of the list with its elements all joined up by .join()


(() => {
    "use strict";

    return Application("Bookends")
            item => item.format({
                using: "BibTex.fmt",
                as: "plain text"