Add text to specific location in a draft

Hey all been creeping for awhile and starting to move most of my actions that launch shortcuts to run natively in drafts with some clean prompts. Just looking for some references to solve the following. I don’t mind figuring it out on my own using some regex and figuring out find and replace etc but I figured this can’t be a unique request.

So I have a draft for all my clients. In that draft I have a header “##### Activities” I also have actions for calling the client and emailing the client which grabs the phone number or my selected text to send as an email from the draft.

What I would like to do is add a step at the end of each action that would add “Phone Call - DATE - PROMPT(what the call was about) under that header.

So questions is would I do a regex on #### Activities until an empty line then add the log and then do a replace?

Any suggestions or better methods would be helpful. Fairly new to scripting but can look up something if I need too.

If activities is always the last section in the draft…

  • You could append your entry to the end for chronological ordering.
  • You could split() the draft content on the heading name and insert the new content between.

If it is not always the last section, I created some functions in TADpoLe for the editor object that allow you to append and prepend to Markdown headed sections that would probably simplify things for you;.

To use those functions, install and setup the ThoughtAsylum Action Group, then include the action TAD at the start of your own action.

RegEx could be a way to do it though if you prefer to roll your own solution from scratch.

@Randy_Kelly
Does this solve your question?

I wonder if something xpath-like would be helpful. Making up syntax here but h2[3] for the fourth H2 heading in a draft.

Looks like exactly what I’m looking for.

Having trouble getting it to work is there another action group aside from core that I need to install?

Have core installed and power user, did the refresh, added the TAD action to my own and copied the example as follows:

editor.TA_sectionAppendByHeading("#### Activities", “This is a test of the thing\n”);

Am I missing something? Or what am I missing :stuck_out_tongue: Thanks for all the help you guys are awesome.

Nvm got it :slight_smile:
Played around with the example in writing and was missing the editor.setText function.

Those libraries are pretty sweet going to have to update a lot of my actions now thanks :stuck_out_tongue:

1 Like

Hi Randy, could you or @sylumer give some more detail to the example? I’m having the same problem you described but I’m not sure how to use the setText function to fix it.

From the example in the docs, you can see the function used returns the updated text.

Therefore to update the editor content, you could use the editor.setText() function using that as the input.

oh, ok, got it. Turns out I was trying to grab the header via the “navigation markers” function and that wasn’t including the “#” that the TAD function was looking for. Here’s what I made: https://actions.getdrafts.com/a/1eS