Appending to current Draft

Hello, I have figured out how to create a new Draft and put text into it (using the editor object), but am having trouble adding text to a Draft that is open. Ex, I want to put the current date in the draft where the cursor is. I see how to do it using the “Insert Text” option, but not when using a script I want my script to do some other things, so I can’t just rely on “Insert Text”. Can someone guide me on how to do this?

Text insertion is pretty straight forward using the editor object. Inserting a date, there are probably dozens of way to do. Here’s one example of inserting the date using scripting.

editor.setSelectedText(draft.processTemplate("[[date]]"));

Note that the Insert Text action could be preceded by a script step and followed by a script step, so there isn’t necessarily anything stopping you from using that.

Thanks. I thought I had to create a new editor to use the methods there, but now I see I can use it to set the text on the currently open draft.

This is may be not so easy to spot. Some classes in drafts are used to control existing application objects in Drafts. Others need to a be created.

You can check the difference in the script documentation.

There is NO constructor for the editor;

A global editor object is available in all action scripts

but in the Drafts class has a constructor:

constructor

Create new instance.

Returns Draft

There is also a global draft object that is pointing to the active draft that is opened.

Now the difference of Draft and draft is hard to spot. More complex by the name of the app. Including code examples that use let draft = new Draft which would create a local variable that hides the global variable.