Add an action to a script

Hi,

I would like to take the New Linked Draft action a step further:

I would like to add an action to the script when it executes so the the new draft created includes more than just the [[title]].

I believe I am dealing with this portion of the script:

	// create the new linked draft
	let d = Draft.create();
	d.content = p.fieldValues["title"];
	d.content = d.processTemplate(template);
	let tags = p.fieldValues["tags"].split(",").map(s => s.trim());
	for (let tag of tags) {
		d.addTag(tag);
	} 
	d.update();

Many thanks for your guidance.

1 Like

This specifies the template being used, which defines the draft content.

let template = `# [[title]]

< [[backlink]]`;

But, you have not actually mentioned anywhere what the “more” is or where in the draft it should appear.

Also keep in mind that actions (can, and here do) contain script steps in Drafts parlance … you might not want to generally refer to adding an “action” into a script as that would normally then be interpreted as using some script to run another action, which is something that can be done; but I don’t think that was what you are after.

@sylumer thanks for your reply… let me rephrase what I am looking to accomplish without prescribing a solution… an open ended inquiry may yield a better result:

I use the New Linked Action many times a day.

When invoked I name a new draft which is then inserted at the cursor position of the Draft I am viewing.

I can then open the new Draft which has the [[Title]] and backlink to the Draft where the action was invoked.

I would like to further automate my use of the New Linked Draft in this way:

In addition to the title and backlink automatically being inserted into the new Draft, I would like to run another action, which I call “Short Meeting” that will add additional information to the new Draft as follows:

  1. Insert text below the title of the draft
  2. Runs the Backlinks Action
  3. Title action (copies the title of the new draft to my clipboard.)

Running this amended version of New Linked Draft would look something like this:

# Test New Linked Draft with Short Meeting - 20220506

::::::::::::::::::::::::::::::::::::: 
Location: Drafts Community
Attendees: Me + anyone else willing to help
Date: [[2022-05-06d]]
::::::::::::::::::::::::::::::::::::: 

## Agenda


## Notes



:::::::::::::::::::::::::::::::::::::

## Todoist Tasks
- [ ] 


## Action Plan / Agenda for Next Meeting



:::::::::::::::::::::::::::::::::::::



---

## Backlinks

[[d:2022-05-06d]]

Thanks to you or anyone else who can guide me further.

With appreciation.

Let me see if I can summarise what you have set out. You want a single action to run when in the current draft (A) that…

  1. Inserts a link to a new draft (B) in current draft (A).
  2. Creates new draft (B) (in the background - i.e. staying with the current draft (A) in the editor) based on the template text above.
  3. Includes a set of back links as text in draft (B) - which presumably would only ever be one link, draft (A), at tie of creation.
  4. Places the title of the new draft (B) on the clipboard.

Yes, that is correct, and:

On #2 Never considered I could open the new draft (B)… in this case I think it would be helpful.
On #4 The title of the draft should be in “[[Draft]]” format, so I can add the link elsewhere too.

The action below should do what I summarised in points 1-4 above plus accommodate your revised notes on point 2 and 4.

  • This action is heavily based on the New Linked Draft action linked in the first post.
  • The action has no dependencies (i.e. it does not call any other actions)
  • I have set the action up to utilise a couple of template tags to make maintenance of the new draft template a little easier.
    • NEWDRAFTCONTENT is the content of the new draft as a template that will be evaluated. Note the ise of the [[backlink]] tag - the use is the same as for the original action.
    • NEWDRAFTTITLEPREFIX is the text to prefix to the title of the new draft. I have set this to be # to match the prefix in your example.
  • I have made some assumptions in the construction of the template based on making what you had included in your example dynamic; if not perfect, it should be close - you can simply modify it accordingly.
  • I have not made any use of the Backlinks action mentioned earlier, for the reason set out in point #3 above.

Hope that helps.

2 Likes

@sylumer this works brilliantly… better than I could have hoped… Many thanks!

Great. Glad to hear that it helps :sunglasses:

1 Like