Add current date/time to existing draft

I have an action that I use to send the current date and time [[date|%A %d-%m-%y - %R]] prepended to note in Bear and a file in Dropbox. I use this to log my working times.

To end my reliance on any third party app I though I would switch to prepending the data to an existing draft and that probably needs scripting. Something based off the action below.

let d = Draft.find("ENTER YOUR UUID BETWEEN THESE QUOTATION MARKS")

// append current draft
d.content = d.content + "\n" + draft.content
d.update()

that’s obviously an append script

Two questions:

  1. How do I get the date/time information into “draft.content” or similar?

  2. Would it be possible to prepend this data after the title line in the target draft?

Try this:

d.content = d.processTemplate("[[title]] [[date|%A %d-%m-%y - %R]]\n[[body]]";
1 Like

It’s running successfully with no errors but it’s not changing the draft. I ended-up using…


// append draft contents to an existing draft

let d = Draft.find("UUID here")

// prepend current draft
d.content = d.processTemplate("[[title]]\n[[date|%A %d-%m-%y - %R]]\n[[body]]")

That was just the line to set the content, not the whole script.

You should add the update line too.

Thank you! Very grateful for your help.

I’m learning so much. This works and I know how to to append the current daft to a specific UUID.

Is scripting the best way to add "\n[[draft]] + " - " + [[latitude]],[[longitude]] to the end of a different draft? How would one do that.

I use this for logging the location of photos where my camera doesn’t geotag locations.

Yes, if you want to position the insertion point automatically at the end; but I think you have some mismatched double quotes above in your pseudo-string.

The process to use the template tags is exactly the same as before, with the processTemplate() function.

Using the script below I can write the date, time and location metadata to the target draft. How do I include the text I typed into the text window before running the script?


let d = Draft.find("UUID goes here")

// append current draft
d.content = d.processTemplate("[[draft]]\n\n[[date|%A %d-%m-%y - %R]][[latitude]],[[longitude]]")
d.update()

What “text window” are you referring to?

If you mean the current draft rather than the searched for draft, you can reference the draft object for that draft that using draft rather than d. So for the content of the draft from which you triggered the action it would be draft.content.

If that isn’t it, please provide more detail as to what you are referring to.

You correct. I want to take text from the current draft and append it to a specific draft, surrounded by time and locatoion metadata. At the moment the script below:

let d = Draft.find("UUID goes here")

// append current draft
d.content = d.processTemplate("[[draft]]\n\n[[date|%A %d-%m-%y - %R]]d.draft[[latitude]],[[longitude]]")
d.update()

yealds:

Thursday 03-02-22 - 17:17[[draft.content]]55.977555,-3.191679

I need to learn how to refer to typed content correctly to complete the line and then add some formatting characters.

Trying to piece together what you have described so far, I think a script might look something like the below (note it is untested). I’ve broken it down a bit and added in lots of comment to describe the various steps, so it could absolutely be more succinct, but hopefully, this way makes it easier to follow.

// Find an existing draft
let draftFound= Draft.find("1359D178-8D4B-4BEE-96C9-357353C6AC5B")

// Original post wants to do insertion between title and body
// Insertion consists of a date line, the content of the originating/current draft, and the current location coordinates.

// Let's build the new content to insert...
// Start with the date line
let strNewContent = draftFound.processTemplate("[[date|%A %d-%m-%y - %R]]");
// Add the current draft content on a new line after the date and followed by another newline
strNewContent = strNewContent + "\n" + draft.content + "\n";
// Add the coordinates for the current draft (current location should be most reliably based on modified coordinates)
strNewContent = strNewContent + draftFound.processTemplate("[[modified_latitude]],[[modified_longitude]]")

// Insert the new content by replacing the content with the new content between the title and body.
draftFound.content = draftFound.title + "\n" + strNewContent + "\n" + draftFound.processTemplate("[[body]]");

// Update the found draft
draftFound.update();

That seems to work perfectly. Thanks so much for your help (and patience).

All those comments will help me to fish in future I hope… :grinning: