Use smart rules to create pdf properties such as Title and Subject?

I would like to use some automation to edit/create a title and a subject in the pdf document properties from the name of the pdf. Is there a way to do this using smart rules please?

For example the name of my pdf is “doc no 1234 COA5500” - can I somehow have the doc no 1234 in the title and the COA5500 in the subject? Currently i do that by copy and paste manually.

Thanks for any ideas Thomas

I’m probably overlooking something obvious (again), but I think that you’d need a script for that. Currently, I do not see a way to achieve that with a simple smart rule alone.
The metadata property of a DT record is marked as read-only in the scripting dictionary. One can seemingly set it (no error is raised, whysoever), but the metadata itself is not changed a bit.

So, I whipped up a sample script in JavaScript. It adds the string “Testing it” as subject to the currently selected PDF in DT.

ObjC.import('PDFKit'); 
(() => {
  /* get application instance */
  const app = Application("DEVONthink 3");
  /* get first selected record */
  const r = app.selectedRecords()[0];
  /* convert record's path to NSURL object */
  const docURL = $.NSURL.fileURLWithPath($(r.path()));
  /* load the PDF document from this URL */
  const PDFDoc = $.PDFDocument.alloc.initWithURL(docURL);
  /* get the current PDF attributes as a MUTABLE dictionary.
     other dictionaries can't be modified! */
  const PDFAttributes = $.NSMutableDictionary.dictionaryWithDictionary(PDFDoc.documentAttributes);
  /* Set the 'Subject' to a dummy string */
  PDFAttributes.setObjectForKey($("Testing it"), $("Subject"));
  /* Update the PDF attributes */
  PDFDoc.documentAttributes = $(PDFAttributes); 
  /* Write the PDF doc back to the URL */
  const result = PDFDoc.writeToURL(docURL);
})()

That demonstrates that it is possible to modify the metadata in a script. As one can add scripts to smart rules, your problem is solvable – but this script is only a starting point.

TL;DR: The $(...) expression converts a JavaScript object to an Objective-C one. That works for strings (JavaScript string to NSString) and objects (JavaScript object to NSDictionary).

To do what you want, you’d have to modify the code slightly (** this is not tested! **).
It assumes that the title always consists of digits only, preceded and followed by at least a single space, and that the title continues after the space(s) up to the end of the name. If that’s not the case, you have to modify the regular expression accordingly.

ObjC.import('PDFKit');
 function performsmartrule(records) {
  records.forEach(r => {
    /* get record's name */
    const name = r.name();
   /* use a regular expression to split the name */
    const match = name.match(/.*?\s+(\d+)\+(.*)$/);
    if (!match) return; /* skip records not matching the regular expression */
    const title = match[1];
    const subject = match[2];
  /* convert record's path to NSURL object */
  const docURL = $.NSURL.fileURLWithPath($(r.path()));
  /* load the PDF document from this URL */
  const PDFDoc = $.PDFDocument.alloc.initWithURL(docURL);
  /* get the current PDF attributes as a MUTABLE dictionary.
     other dictionaries can't be modified! */
  const PDFAttributes = $.NSMutableDictionary.dictionaryWithDictionary(PDFDoc.documentAttributes);
  /* Set the 'Subject' to a dummy string */
  PDFAttributes.setObjectForKey($(subject), $("Subject"));
  PDFAttributes.setObjectForKey($(title), $("Title"));
  /* Update the PDF attributes */
  PDFDoc.documentAttributes = $(PDFAttributes); 
  /* Write the PDF doc back to the URL */
  const result = PDFDoc.writeToURL(docURL);
})
}
1 Like

Thanks a lot! Will give it a try, kind regards Thomas