Prompts and Loops and Field Names

I’ve created a shortcut that uses Data Jar to ease reading and writing to a JSON store. This is working pretty well, but I think it would be better in Drafts, because the prompts are more flexible. However, I am barely a novice with javascript and don’t know much about working with JSON. Everything I can find online is either bare man pages that assume more expertise than I have, or seems to assume some kind of Web / HTML usage that doesn’t apply here to Drafts.

Anyway, I’ve created a Prompt that creates switches and text fields within a loop that gathers labels from key/value pairs in the JSON. However, I’m not sure how to assign unique names to these so that I can retrieve them once I’ve responded to the prompt.

Have you read the documentation on field values for prompts? It explains the key value pairing there.

If that doesn’t effectively cover what you require, perhaps share a simplified version of your JSON (a subset of what you shared on the Automators forum perhaps?), some JavaScript code that you have so far to work with it, and what you want from it. Simple enough that it is easy to explain in either direction :wink:

Are you just asking how to get the values back from the prompt after you have run show() on it?

The fieldValues property of the prompt object will contain all those values based on keys from the name properties you assigned when creating, so, for example:

let p = Prompt.create();
p.addSwitch(“switchName”, “My Switch”, true);

if(p.show()) {
    // get value of switch...
    let value = p.fieldValues[“switchName”];
}

Is that what you needed to know?

I already know how to create fields and how to read back their values after a prompt is created. However, I’m trying to dynamically create the fields, and I’m not sure how to read field values after the prompt that were dynamically created in a for-loop in the prompt.

Here’s my JSON:

{
  "1": {
    "name": "foo",
    "creation date": "2019-12-24",
    "priority": {
      "name": "foobar",
      "completion date": "",
      "creation date": "2019-12-26",
      "daily": [
        {
          "complete": false,
          "date": "2019-12-27",
          "task": "first task"
        }
      ],
      "enough": [
        {
          "date": "2019-12-28",
          "description": "baz",
          "enough?": true
        },
        {
          "date": "2019-12-27",
          "description": "qux",
          "enough?": false
        }
      ]
    }
  },
  "2": {
    "name": "bar",
    "creation date": "2019-12-24",
    "priority": {
      "name": "bazqux",
      "completion date": "",
      "creation date": "2019-12-26",
      "daily": [
        {
          "complete": false,
          "date": "2019-12-27",
          "task": "first task"
        }
      ],
      "enough": [
        {
          "date": "2019-12-28",
          "description": "baz",
          "enough?": true
        },
        {
          "date": "2019-12-27",
          "description": "qux",
          "enough?": false
        }
      ]
    }
  }
}

Here’s my script:

// Goals testing

// Get the JSON file

let fmCloud = FileManager.createCloud();
let file = fmCloud.readString("/Goals/goals.json");
let json = JSON.parse(file);

// // Prompt
/* --------------------- */

var today = Date.today().toString("dddd, MMM d");
var dStamp = Date.today().toString("yyyy-MM-dd");

var p = Prompt.create();
p.title = "Goals Today";

for (var goal in json) {
    let goalName = goal;
    let priority = json[goal].priority.name;
    let toggle = goal+"toggle";
    let field = goal+"field";
   
    var label = goal + ":\nEnough today?";

    p.addSwitch(toggle,label, false); 
    p.addTextView(field, priority,"", {height: "4"});
  }

p.addButton("ok");

var con = p.show();

if (con) {
  
  for (var goal in json){
     let toggle = goal+"toggle";
     let field = goal+"field";
alert(p.fieldValues[toggle] + p.fieldValues[field]);

json[goal]["enough"].unshift('{"date":dStamp,"description":field,"enough?":toggle}');
var output = JSON.stringify(json);
alert(output);
let success = fmCloud.writeString("/Goals/goals3.json",output);
  }
    }
else {





  context.cancel();
}

Okay, I hope I’ve got the gist of what you were attempting to do. I’ve corrected a few errors, reworked the code a little in a couple of places, and added in several more alerts in the hope it will guide you in what it is now doing.

// Goals testing

// Get the JSON file

let fmCloud = FileManager.createCloud();
let file = fmCloud.readString("/Goals/goals.json");
let json = JSON.parse(file);

// // Prompt
/* --------------------- */

let today = Date.today().toString("dddd, MMM d");
let dStamp = Date.today().toString("yyyy-MM-dd");

let p = Prompt.create();
p.title = "Goals Today";

for (let goal in json)
{
	let goalName = goal;
	let priority = json[goal].priority.name;
	let toggle = goal + "toggle";
	let field = goal + "field";
	let label = goal + ":\nEnough today?";

	p.addSwitch(toggle, label, false);
	p.addTextView(field, priority, "", {height: "4"});
}

p.addButton("ok");

if (p.show())
{
	for (let goal in json)
	{
		
		let toggle = goal + "toggle";
		let field = goal + "field";
		alert("INFO PART ONE:\n\ngoal = " + goal + "\n" + "toggle = " + toggle + "\n" + "field = " + field );
		alert("INFO PART TWO:\n\nfield value [" + field + "] = " + p.fieldValues[field] + "\nfield value [" + toggle + "] = " + p.fieldValues[toggle]);
		
		
		alert("JSON{Current goal}:\n\n" + JSON.stringify(json[goal]));
		alert("JSON{Current goal}.priority:\n\n" + JSON.stringify(json[goal].priority));
		alert("JSON{Current goal}.priority.enough:\n\n" + JSON.stringify(json[goal].priority.enough));
		
		json[goal].priority.enough.unshift({"date":dStamp,"description":p.fieldValues[field],"enough?":p.fieldValues[toggle]});
		let output = JSON.stringify(json);
		alert("JSON{Current goal}:\n\n" + output);
		let success = fmCloud.writeString("/Goals/goals3.json", output);
	}
}
else
{
	context.cancel();
}

Let me know if that gets you any closer to what you are trying to do.

Wow, this is super helpful! Thanks! I’m going to need to tinker with it a bit later today. I think for sure this will get me where I need to go.

This does exactly what I need. I now see what to do to adjust this for a variety of uses. Thanks so much!

1 Like

No problem. Glad the changes were what you were looking for. :+1: