Javascript: Getting plain text of current record...

I’m on a mission here, I’m either making something enormously useful or something which will have everyone telling me I’m using tags wrong :slight_smile:

I’m trying to access the plain text and metadata of the currently selected record into an array, which I can merge with plain old Javascript Array.join(). Here’s my code:

		var record = Application('DevonThink Pro').contentRecord;
		var textPool = [	 record.plainText, 
						 record.metaData.kMDItemTitle,
						 record.metaData.kMDItemHeadline,
						 record.metaData.kMDItemURL,
						 record.metaData.kMDItemSubject,
						 record.metaData.kMDItemDescription,
						 record.metaData.kMDItemComment,
						 record.metaData.kMDItemKeywords,
						 record.metaData.kMDItemCreator,
						 record.metaData.kMDItemCopyright,
						 record.name,
						 record.url,
						 record.comment  ];		
		body = textPool.join(" ");

However, I get the error

Error: Can’t convert types.

from


 record.plainText

Where am I going wrong? Am I accessing the currently selected record incorrectly? I got the impression that it was an object whose properties I could access.

My next question will be about setting tags on the object, based on the plain text string of the tags, if anyone’s interested. :slight_smile:

  • Dave

Okay. It seems record.plainText is a method, not a propery.


record.plainText()

:slight_smile:

Property getters (across the JXA Automation interface) are generally functions, so


record.plainText()

(to actually apply the function and retrieve data, rather than just get a reference to the function.


.metaData()

``` returns a dictionary, so things like:

(() => {
‘use strict’;

// (++) :: [a] -> [a] -> [a]
const append = (xs, ys) => xs.concat(ys);

// maybeRec :: Maybe DT Record
var maybeRec = (function () {
    var mb = Application('com.devon-technologies.thinkpro2')
        .contentRecord();
    return mb !== null ? {
        just: mb,
        nothing: false
    } : {
        nothing: true
    }
})();

// Record -> String
return maybeRec.nothing ? (
    ''
) : (function () {
    var
        rec = maybeRec.just,
        dctMeta = rec.metaData(); // Dictionary

    return Object.keys(dctMeta)
        .map(function (k) {
            return k + ': ' + dctMeta[k]
        })
        .concat([rec.plainText()])
        .join('\n');
})();

})();

Thanks Houthakker, I’m learning some (hairy) approaches with your amazing Javascript.

I liked Javascript before it turned into Java-as-Javascript…

Java I would try only in a transformed version (probably this one: eta-lang.org/)

But I do like this use of JavaScript:

purescript.org/

and I find the Haskell Prelude a very useful set of composable, well-reasoned and well-tested abstractions ( my snippets macro draws on c.200 Prelude-derived functions for JavaScript, and close to that number for AppleScript too)

(Hoogle is a good route to them)

haskell.org/hoogle/?hoogle=concatMap