Unsurprisingly, you are correct. Thanks a bunch.
Here’s a JavaScript version of @MrSkooby’s script, following the path I suggested before:
ObjC.import('PDFKit');
(() => {
const app = Application("DEVONthink 3")
/* Get the record to add keywords to */
const rec = app.getRecordWithUuid('x-devonthink-item://83E31EAE-991E-41A3-A146-20E4A77295E8');
/* create a PDFDocument from the record's path converted to an URL */
const PDFDoc = $.PDFDocument.alloc.initWithURL($.NSURL.fileURLWithPath($(rec.path())));
/* get the documentAttributes as a mutable array so we can add keywords to it */
const PDFAttributes = $.NSMutableDictionary.dictionaryWithDictionary(PDFDoc.documentAttributes);
/* Set the "Keywords" entry of the PDFAttributes to something unique
for testing purposes */
PDFAttributes.setValueForKey($([$('blurbsel')]), $('Keywords'));
/* Update the PDF's documentAttributes */
PDFDoc.documentAttributes = PDFAttributes;
/* Write back the PDF */
PDFDoc.writeToFile($(rec.path()));
/* Force DT to update it's index re this record */
app.synchronize({record: rec});
})()
The setValueForKey
call is a bit weird because it needs Objective-C data structures. $([…])
converts a JavaScript array into an NSArray
, while $('…')
converts a JavaScript string into an NSString
. Therefore, $([$('blurbsel')])
is an NSArray
with an NSString
as its only element.