Action on a specific draft - not current

A quick follow up to my last question, where I learned a lot about managing content via javascript!

I think this is possible, and probably easy, but I’m missing something obvious.

I would like to write an action that jumps from the current draft to a specific draft (likely via it’s UUID), run my javascript step to do some text processing on that specific draft, and then jump back to the current draft (where I will paste the results).

I tried this by starting my action with a URL step, using the drafts5 url scheme and the uuid. This worked, but then the next step, the javascript, is still being executed against the original draft that I started on, not the draft that I jumped to.

Is there is a way to rethink how I’m doing this?

When you run an action, it runs on the current draft. If you think about how actions can be run against a selection of drafts, then you can see why it works like that.

Since your processing is JavaScript based, you are probably in a good position. If you are using draft as the basis for your JavaScript processing, that is working on the current draft. What if you defined another draft object and set that equal to the draft specified by the desired uuid?

For example, if you set a meaningful uuid and run this script it will just display the contents of a particular draft.

let draft2 = Draft.find("AAB778F7-9952-4AE0-AEC0-FCC7BDCAE39B");
alert(draft2.content);

Once you have your head around that, I think you’ll know exactly what to do.

Hope that helps.

Stephen - thanks for this! I’ve been playing with this for a bit and I’m getting the hang of it. I have to teach myself more javascript because I was doing a lot of the actions using draft.content specific actions that I don’t seem to be able to do via the variable. [Translation: I don’t yet know how to do via a variable.]

For instance, I split the content via lines, like Greg showed me how to do,
Let lines = draft2.content.split("\n");

But then I don’t know how to pull a range of those lines. I know how to access a single line by doing
lines[linenumber]
but not a range of lines.

I have been looking for a good tutorial of javascript text manipulation, but I can’t find anything that’s not very website focused. Any suggestions would be appreciated!

This should do the trick.

let strLinesTwoToFive = Draft.find("AAB778F7-9952-4AE0-AEC0-FCC7BDCAE39B").processTemplate("[[line|2..5]]");
alert(strLinesTwoToFive);

It searches for a Draft by UUID as I did in my earlier example. It then immediately processes a template against it. The template instructs Drafts to return the second through fifth lines of the draft specified. That is then set as a variable which the next line displays in an alert, though you could of course continue processing in any way you wish.

That shouldn’t make any difference. Text is text no matter where it is. Just make sure you read through the Drafts documentation as that will give you all the specifics overall about working with Drafts, and then you can pick up the rest from all sorts of tutorials and web searches.

1 Like

Stephen - after your previous reply, I went back and read up on the difference between a variable and an array. Now that I understand how an array works, I’m definitely feeling more comfortable with all of this, and handling what I’m trying to do! It is good to know I can use the processTemplate on a variable. I had thought you could only use it on draft.content. Thanks!

Last question (have I said that before?):
The goal of my action is to reference another draft that has a list of daily writing prompts. But some prompts are on multiple lines. Pull the most recent prompt and paste it into a new draft, and then delete that prompt from the reference draft (so that tomorrow, the same action will pull the next prompt).
I am now successful in:

  1. Pulling the content from my reference draft (with a list of prompts) into a variable.
  2. Determining how many lines is the next prompt
  3. Pasting that prompt (no matter how many lines it is) into the new draft
  4. Creative a variable that is the entire content of the original, reference draft, without the most recent prompt.

What I can’t figure out how to do is replace my reference draft with the contents of this new variable. Is there a way to replace content in another draft with javascript? I also tried using the drafts replace content url scheme as a separate step in the action, but I can’t seem to pass a variable from the javascript step to the URL step.

Any suggestions???

processTemplate is a function that can be used on any instance of a Draft. It is not specifically used against a draft’s content. If you look at the list of available tags, you’ll see that the identifier, dates & locations, utility, and custom tags are all independent of the draft’s content

Variables contain objects of some sort (or rather direct references to). This could be a string of characters, a number, a date (more a numeric offset to a known origin), or any number of other things. Including a draft.

Try something like this. Hopefully it makes sense - it’s really just a set and save.

let  newContent = "lorem ipsum dolor sit amet";
let  otherDraft = Draft.find("A556C1F9-C2A8-4811-AE1C-88918833C9FC");
otherDraft.content = newContent;
otherDraft.update();

OMG, I can’t wait to put this into action. Thanks!!!