Script executing on draft and duplicate of draft

Hi, I’ve got a script that backs up my draft (by duplicating it) then runs some replacements using the below script:

editor.setText(editor.getText().split("\n").join("
"));
editor.setText(editor.getText().split(""").join("\""));
editor.setText(editor.getText().split("’").join("’\’’"));
draft.update();

It then runs some more script to generate my child’s age and prepends that to the draft. I believe the relevant part of that script is:

draft.content = “(” + dtDiff[‘years’] + " years, " + dtDiff[‘months’] + " months, and " + dtDiff[‘days’] + " days" + ") " + draft.content;
draft.update();
//Reactivate the editor
editor.activate();

The problem is that the two parts of the script function on different drafts. The first one runs on the duplicated version of the draft, the second half on the original draft. I don’t know enough about scripting here to figure out how to make it function on just one. Any ideas?

OK I got to the bottom of this. The duplicate draft action I was using was this one: http://actions.getdrafts.com/a/1Df. It loads the duplicated draft into the editor, so I assume the “edit” actions in the subsequent code are targeted at that text while the other part of the script works on the “draft”. Still not 100% sure how the edit and draft commands work, so if anyone can clear that up I’d be grateful!

Basically, only use editor functions to manipulate the text in the editor. Per the docs:

NOTE: Generally speaking, editor methods are best used for quick text manipulations of the type used in the extended keyboard. Most substantial updates to draft content are better applied using the draft object.