Automation: attach files in a Devonthink group to email via Keyboard Maestro/other automation step

hi all

im trying to automate sending emails with attached files. the attached files change each period and sit within a Devonthink group (pending)

I have this Keyboard Maestro automation step

Does anyone know how to point the Keyboard Maestro attach folder to a specific group I Devonthink to attach the files from there?

thx

Z

If I understand you correctly, you want to send different attachments, all stores in the same DT group, to the same recipients. So, the only thing that changes are the attachments themselves.

In that case, and assuming that you’ll simply attach all documents in this group, I’d go for a script in DT (which you can also trigger with a key combination). Something along these lines:

(() => {
  const app = Application("DEVONthink 3")
  
  /* get the paths of all records in a fixed group */
  const uuid = "A7AEF6B5-3708-4CBF-B211-A868004BEDB2"; /* change to the UUID of your group */
  const group = app.getRecordWithUuid(uuid);
  const attPaths = group.children.path();
  sendMailWithAttachments(attPaths)
})()

function sendMailWithAttachments(paths) {

  const sender = "me@example.com"; // change to sender address
  const recipient = "you@example.com"; // change to recipient address
  const subject = "Test"; // change to subject text
  const content = "Hi there"; // change to content text
  
  const app = Application("Mail");
  
  /* Create a message and push it onto the outgoing list */
  const msg = app.OutgoingMessage({sender: sender,
           subject: subject});
  app.outgoingMessages.push(msg);
  
  /* Create a recipient and push it onto the recipient list */
  const rcpt = app.Recipient(address: recipient});
  msg.toRecipients.push(rcpt);
  
  /* Create message content. 
    Beware: This field must probably be there, even if it's just empty text */
  msg.content = content;
  
  /* Create attachments for every path passed into this function
     and push them onto the attachment.list */
  paths.forEach(p => {
    const att = app.Attachment({fileName: Path(p)});
    msg.attachments.push(att);
  })
  /* Send the message */
  app.activate();
  delay(2); // maybe not necessary? 
  msg.send();
}

You have to change the constants uuid, sender, recipient, content and subject to appropriate values for your use case.

I suppose that you could also put that as a script into KM to run on a keystroke.

thx so much @chrillek !

really appreciate the kind help

If I understand you correctly, you want to send different attachments, all stores in the same DT group, to the same recipients. So, the only thing that changes are the attachments themselves.

exactly!

I did try to use it with no success though. I assume this is written in .js? (sorry this much be trivial :))

I modified all the parameters ( uuid , sender , recipient , content and subject) and tried to launch it via a Keyboard Maestro shortcut (as suggested) with no success.

how does one try it within Devonthink? do I need to save it as a .scptd file?

Yes, it’s a JavaScript script. I don’t know enough about KM to tell you how to run it there.

You can copy/paste it to script editor (set the language to JavaScript there!), turn on its protocol pane (Cmd-3) and then run the script from there (with all the changed constants, of course).

If that works ok for you, you can save it as scpt file in DT’s scripting folder (see the manual for that).

Edit Keyboard-Maestro: This works for me

amazing!! thx so much

I found out that the error was a typo :slight_smile:

           subject: subjct});

there was a e missing :wink: in case anyone wants to use this in the future

really really appreciate the help @chrillek !

Z

Thanks, I fixed that in the listing above.