I’m observing odd behavior of an action of mine. I have a 2 step action — first action is a script that does some reformatting of the current Draft. The last step of the script is d.update (where d was previously set as draft.content).
The draft updates correctly per the script action. ie, if I re-open the draft after the script runs, all updates are correct. The script is working fine.
The second action step is to create a new Evernote note based on the draft.
Some times, but not always, the Evernote note is created with the content of the draft PRIOR to the script running. Periodically, the Evernote note does get created with the correct Draft content.
Head scratcher for me. Should I introduce some sort of pause or confirmation following the d.update (last line) in the script but prior to moving on to the next action step of creating the Evernote note?
Could you post the action. Hard to troubleshoot without seeing the script. You could put it up as an unlisted action in the directory, or put the scripts in here.
Still work in progress, in terms of formatting the draft and eventual Evernote note, but this is complete in terms of a working action that exhibits the problem I described.
There a couple of things going on here, let me try explain as best as I can.
First, calling the assignment
var d = draft.content; is creating a string object that is a copy of the content of the current draft. You are fine to manipulate that string as you are doing, but it is a copy and no longer references the original draft in anyway.
Second, using the
editor object is directly manipulating the user interface, not the draft. It’s best used for things like keyboard keys where you don’t care about the draft you are editing, you are just manipulating the text.
When you start an action, the current draft is saved and passed to the action as it stands. Updating the user interface via the editor will not update the draft object which has already been passed to the action.
What you want to do is update the draft itself, because that is what the subsequent action steps are referencing.
This will work fine if you change these two lines:
editor.setText(begin + " \n" + end);
draft.content = begin + " \n" + end;
You do not need to also update the editor. Drafts will take care of that when the draft is saved with changes.
Hope this made some sense.
Thank you so much for the response and guidance. My script is symptomatic of my great interest in leveraging the capabilities of Drafts (such a wonderful product) tempered by my limited knowledge of the scripting environment. Learning though… and loving it.
and… this fix solved another problem I was having with wonky markdown to Evernote conversion.
Again I appreciate the support and the product. Great stuff — jay