Using a Script and sending the draft to Evernote

Hi,

I created an Action involving two steps. The first step runs a script to gather some information. I then want to take this draft and append to a note in Evernote.

The issue I have is that when it runs script runs fine and creates the note in Evernote however it is empty. When I run the action again with new information entered the information passed to Evernote is information from the past data entry. Not sure what I need to do to correct.

Here is my script for Step 1. Step 2 is basically an append with the template [[date]] - [[draft]]

thanks,
merlyn

//Gym tracker
 
// Prompt for gym information
 
var p = Prompt.create();
p.title = "Enter Gym Info";
p.message = "Enter details today"
p.addTextField("weight", "Weight Lbs", "",{"keyboard":"numberPad"});
p.addTextField("elliptcal", "Elliptcal", "",{"keyboard":"numberPad"});
p.addTextField("spinbike", "Spin Bike", "",{"keyboard":"numberPad"});
p.addButton("Go");
var con = p.show();
 
if (con) {
       // Get values from prompt step
       var weight = "Weight Lbs: " + p.fieldValues["weight"];
       var weightKG = "Weight KG: " + p.fieldValues["weight"]*.454;	
       var elliptcal = "Elliptcal: " + p.fieldValues["elliptcal"];
       var spinbike = "Spin Bike: " + p.fieldValues["spinbike"];
 
       // Create Draft
       var d = Draft.create();
       d.content = "Gym Info" + "\t" + weight + "\t" + weightKG + "\t" + elliptcal + "\t" + spinbike; 
       d.update();
       editor.load(d);
}
else {
       context.cancel();
}

I changed a few things throughout but many might be due to the formatting in your post above. Try this…

//Gym tracker
// Prompt for gym information
var p = Prompt.create();
p.title = "Enter Gym Info";
p.message = "Enter details today"
p.addTextField("weight", "Weight Lbs", "",{"keyboard":"numberPad"});
p.addTextField("elliptcal", "Elliptcal", "",{"keyboard": "numberPad"});
p.addTextField("spinbike", "Spin Bike", "", {"keyboard":"numberPad"});
p.addButton("Go");
var con = p.show();
if (con)
{
	// Get values from prompt step
	var weight = "Weight Lbs: " + p.fieldValues["weight"];
	var weightKG = "Weight KG: " + p.fieldValues["weight"] * .454;
	var elliptcal = "Elliptcal: " + p.fieldValues["elliptcal"];
	var spinbike = "Spin Bike: " + p.fieldValues["spinbike"];
	// Create Draft
	var d = Draft.create();
	d.content = "Gym Info" + "\t" + weight + "\t" + weightKG + "\t" + elliptcal + "\t" + spinbike;
	d.update();
	editor.load(d);
}
else
{
	context.cancel();
}

Do also look in your run log on the Draft as it tells you what the script is failing on.

Hope that helps.

Hi,

Looking into it more, maybe this can not be done in this manner?

My intent was to create an Action to capture some data and send to Evernote. I notice what is happening is this:

If I run the action the script successfully creates the draft however,

1 - if action runs as a new draft - draft sent to Evernote is empty
2 - if I ran the action from say my inbox, it would create the new draft but send the draft from my inbox that had focus to Evernote. Not the one created in the first step of the Action.

Merlyn

Download these two actions.

Run the capture one. This will queue up the post one , which will then work on your new draft.

Just a couple of potential things to consider:

  1. Do you need to create a new Draft or do you just need to capture the data and pass it on to Evernote?
  2. Is this something an app like Workflow might be better suited to?

2 probably is somewhat dependent on the answer to 1.

But if you do want to use Drafts only and not create a new draft, maybe take a look at setTemplateTag() | Draft | Drafts Script Reference. Pretty sure you could rationalise to one action with two steps.

Actions run on a draft and that draft is the global draft object for the purposes of the all steps in the action.

Scripts can create new drafts and save them and load them, but that does not change the original draft that is in context for the action…so your Step 2 is still running on the new blank draft…thus the confusion.

You can fix this a couple of ways…

  1. Create your new draft object d and call another Evernote action to run on it, like:
let d = Draft.create();
d.content = "whatever";
d.update();
let action = Action.find("My Evernote Action");
app.queueAction(action, d);
  1. Set a temporary template tag containing the new draft content to use in your step 2 Evernote step containing the content…
draft.setTemplateTag("mytag", d.content);

Then in your Evernote step, use the [[mytag]] tag to insert the content you want in Evernote instead of [[draft]] or [[body]]

Number 1. is what I offered up above.

I tried 2. first but it didn’t work. Figured I’d come back later and figure out what was up.

I realised later in the day it had been because I’d attached the tag to the new draft rather then the original. :joy:

Thanks for the support and resolution to my issue. I really enjoy the app so wanted to try and do a few more things with it which pushed me into learning java script.