Tutorial on Drafts Scripting?

I’m a fairly advanced Drafts user and am relatively familiar with scripting mainly in Python, with very little experience in JavaScript. I’m trying to get my head around the Scripting action in Drafts to create some more advanced and personalized scripts, but I’m really hitting a wall. I’ve tried editing some scripts from the Action Directory to suit my needs, but usually can’t figure out enough of what they’re doing to get the script working the way I need it.

Does anyone have a suggestion or link to a tutorial that serves as an introduction to how scripting in Drafts works? Or should I really be starting with a JavaScript tutorial?

(As an example, the one script I’m trying to get right is to append the current draft contents to a specific draft in my archive.)

So just as a quick update, I ended up figuring out how to edit @agiletortoise’s action to append to a draft based on a prompt to instead append to a fixed draft based on UUID.

I did this by poking around the documentation and a little trial and error. I really found I was stumbling around in the dark on it though, so would be nice again to have a really basic tutorial to get me up to speed.

My (working!) script:

// append draft contents to an existing draft

let d = Draft.find("your uuid goes here")

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

Here are my suggestions. While they have a loose order, you can absolutely approach them in parallel with relatively no issue. But you do need to have at least a few of the basics of core JavaScript to understand what you come across in the others.

  • Read Eloquent JavaScript.
    • This will give you a grounding in JavaScript.
    • Try to learn what is provided via the browser and the document object model (DOM), and what is the core language. You can utilise the latter in Drafts.
  • Read the Drafts scripting documentation.
    • They describe things pretty well for anyone who has worked with any sort of programming language that has a modicum of object support.
  • Read through some examples on the forum.
    • This forum has lots of examples posted on it already. Try searching if you are looking for an example of something in particular.
    • If you get stuck, post a question and ask for assistance.
  • Continue to look at actions in the action directory.
    • Pick them apart, change them, destroy them and bend them to your will :nerd_face:

Hope that helps.

3 Likes