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:
How do I get the date/time information into “draft.content” or similar?
Would it be possible to prepend this data after the title line in the target draft?
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]]")
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()
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()
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();