Accessing Variable Values from Scripted Prompts

I am struggling to use a prompt to create a draft. I can set up multiple prompts one after another, but my preference would be to gather them all on one form and go through them part by part. I have been playing around with a lot of ideas. I tried the Mustache Prompt method, but I would like more control over how the prompt appears like I can if I script it myself.

For getting the text into the actual draft, I have looked into both template strings/literals and I have also tried setTemplateTag. My problem is that I don’t know how to access the value of the prompt text field.

Below is a minimal example of my plan. I think if I understand how to access the value for one addTextField, I can duplicate that for my other addTextField and addPicker. I could find an example I was able to adapt of the setTemplateTag to tell if I was getting results and had it formatted correctly.


// Minimal Code Example

var p = Prompt.create();

p.title = "Title";
p.message = "Description";
p.addTextField("textFieldVariable", "Text Field Variable: ", "");

isCancellable: true;
p.addButton("Ok");

var didSelect = p.show();

// create template
const template = `# Title of the draft
Insert info from prompt...

${textFieldVariable}
or
[[textFieldVariable]]

`;

// create the draft
var d = Draft.create();
d.content = d.processTemplate(template);
d.update()

// load in editor and focus for editing
editor.load(d);
editor.activate()

Would it be better to use the template strings or setTemplateTag? Would anybody be able to help show me how to format my script to add it into a new draft?

This seems to me to be the simplest option.

let p = Prompt.create();
p.title = "Title";
p.message = "Description";
p.addTextField("textFieldVariable", "Text Field Variable: ", "");
p.isCancellable = true;
p.addButton("OK");

if (p.show())
{
	// create the draft
	let d = Draft.create();
	d.content = `# Title of the draft
Insert info from prompt...

${p.fieldValues["textFieldVariable"]}`;
	d.update();

	// load in editor and focus for editing
	editor.load(d);
	editor.activate();
}

You might also want to take a look at this action from Greg for some tips.

https://actions.getdrafts.com/a/1OV

I would also suggest looking into HTML prompts if you want even more control over your capture prompt.

Hope that helps.

Thank you so much for the help. The idea of HTML prompts is interesting, but I think I will need to wait for that. I’ve already probably used too much time on this. I was able to make this concept work. I figured I would share the full working code for my action in case it helps anybody. I will say that I have no real skill with JavaScript and just bang my head against my keyboard trying variations until something works. That being said, this is probably not the most beautiful code ever.

First, a little background: I’m working on my Ph.D. currently. With buying a new M1 MacbookAir, I’ve been more heavily relying on MacOS. I had a shortcut that created this same file in drafts for me. It also uses DataJar to pull the Arrays that I use, and I don’t like having to “hard code” them into my script, but until I make a plan for what to do about not currently having access to DataJar on MacOS, this might be what I do.

The way I do my academic writing is that the first line of a draft is the document’s title. The second line of the draft is the folder where I will save it. I have an action that will convert my markdown to HTML, and I can post discussion replies to our Canvas. After completing and publishing my discussion, I can later process it and decide if I need to do anything else with the text or save a version in my OneDrive account.

The code creates a prompt that has some pickers and a text field for my classes. It sets up the file name/folder variables, along with some data that I want to save as YAML metadata. I had to use if(){}, if else(){} statements to determine the array number of the selected response and provide text for the addPicker(). It also splits one of the variables so that my metadata can have separate data about the Course ID and Course Title.

It creates a new draft with a template pulling the variables gathered from the prompt. The draft processes itself again to use other templates (e.g., the date). It also adds tags. I separate my tags by category (i.e., "l - " is a location or area of my life, "a - " is an action, and "s - " is a status).

Anyways, I hope that my banging my head against my keyboard is useful for others.

//  Template Set Up for new writing projects for classes

//  Create Prompt

var p = Prompt.create();
p.title = "CIIS New Writing Project";
p.message = "Fill in form to create new CIIS writing project.";

p.addPicker("pickerCourseDescription", "Course: ", [["Reverse Imagineering", "Methodology Comprehensive Examination", "Literature Review Comprehensive Exam", "Learning Community IV"]], [0]);
p.addPicker("pickerLocation", "Location: ", [["Personal Journal Space", "Research Journal", "Small Group Discussion", "Large Group Discussion", "Paper", "Document"]], [3])
p.addTextField("textFieldTopic", "Topic: ", "");

isCancellable: true;
p.addButton("Ok");

if (p.show())

// Set up frontmatter and project variables

var topic = p.fieldValues["textFieldTopic"];

// Course variable
var courseIDandTitle;
if (p.fieldValues["pickerCourseDescription"] == "0") {
	courseIDandTitle = "TSD 6137 - Reverse Imagineering";
} else if (p.fieldValues["pickerCourseDescription"] == "1") {
	courseIDandTitle = "TSD 9611 - Methodology Comprehensive Examination";
} else if (p.fieldValues["pickerCourseDescription"] == "2") {
	courseIDandTitle = "TSD 9610 - Literature Review Comprehensive Exam";
} else if (p.fieldValues["pickerCourseDescription"] == "3") {
	courseIDandTitle = "TSD 8420 - Learning Community IV";
} else {
	courseIDandTitle = "There is an error in pickerCourseDescription";
}

// Split Course ID and Title Variable

var res = courseIDandTitle.split(" - ");
var courseID = res[0];
var courseTitle = res[1];


// Location variable
var location;
if (p.fieldValues["pickerLocation"] == "0") {
	location = "Personal Journal Space";
} else if (p.fieldValues["pickerLocation"] == "1") {
	location = "Research Journal";
} else if (p.fieldValues["pickerLocation"] == "2") {
	location = "Small Group Discussion";
} else if (p.fieldValues["pickerLocation"] == "3") {
	location = "Large Group Discussion";
} else if (p.fieldValues["pickerLocation"] == "4") {
	location = "Paper";
} else if (p.fieldValues["pickerLocation"] == "5") {
	location = "Document";
} else {
	location = "There is an error in pickerLocation";
}

{
	
	// create the draft
	let d = Draft.create();
	d.content = `[[date]], ${location} Topic - ${topic}
${courseIDandTitle}
---
Course ID: ${courseID}  
Course Title: ${courseTitle}  
Date: [[date]]  
Location: ${location}  
Topic: ${topic}  

---

# ${location} Topic

**Topic**: ${topic}  
**Date**: [[date|%m/%d/%Y]]  
**Title**: 

---


`;

wholeContent = d.content;
d.content = d.processTemplate(wholeContent);
d.addTag("l - ciis");
d.addTag("a - process");
d.addTag("s - draft");
d.update()

// load in editor and focus for editing
editor.load(d);
editor.activate()

}