Insert Text (multiple) from a list of options

looking for an action to bring up a prompt to choose from a list of option and allow to select more than one option to insert at current cursor.

Insert Text Element action brings up a prompt to choose a text to insert. Perhaps this can be modified to include more than one option? I haven no idea how though. Please help.

Maybe you could tailor this example I put together to meet your needs?

//Set-up a basic prompt
let prMain = Prompt.create();
prMain.title = "Multi-Text Insertion Exammple";
prMain.addButton("OK");

//Put in place multiple optional selections
prMain.message = "Toggle on the entries you would like to insert.";
prMain.addSwitch("opt1", "Red", false);
prMain.addSwitch("opt2", "Orange", false);
prMain.addSwitch("opt3", "Yellow", false);
prMain.addSwitch("opt4", "Green", false);
//This could also be built programmatically from an array or some JSON.

//Example to show how text field can be integrated.
//Here we provide an option to change the separator for the output from a 
//comma space to something else like a pipe or a new line.
prMain.addTextField("optSep", "Separator", ", ");

//Display the prompt and take action on OK selection
if (prMain.show())
{
	//Initialise an array of outputs
	let astrOutput = [];
	
	//For each optonal section, check if it is enabled and if so, output
	//one of the values
	if (prMain.fieldValues["opt1"]) astrOutput.push("Red Apple");
	if (prMain.fieldValues["opt2"]) astrOutput.push("Orange Orange");
	if (prMain.fieldValues["opt3"]) astrOutput.push("Yellow Banana");
	if (prMain.fieldValues["opt4"]) astrOutput.push("Green Lime");

	//Insert the selected entries joined by the specified separator.
	editor.setSelectedText(astrOutput.join(prMain.fieldValues["optSep"]));
	
	//Move the cursor to the end of the selection.
	editor.setSelectedRange(editor.getSelectedRange()[0] + editor.getSelectedRange()[1], 0);
}

//Activate the editor
editor.activate();

@sylumer

This is great. Thank you! I modified your script and set ", " as the default separator.

How do I insert a period at the end of the joined items?

Find this…

	//Insert the selected entries joined by the specified separator.
	editor.setSelectedText(astrOutput.join(prMain.fieldValues["optSep"]));

Replace with this…

	//Insert the selected entries joined by the specified separator.
	editor.setSelectedText(astrOutput.join(prMain.fieldValues["optSep"]) + ".");

You literally just need to add the period string to the end of the join as you described.

This works:

editor.setSelectedText(astrOutput.join(", ") + “.”);

Thank you.

@sylumer One more question. How do I combine two prompts like this together?

Through trial and error (not knowing much about JS other than basic variables set), I came up with this. Am I on the bright track?

//Set-up a basic prompt
let prMain = Prompt.create();
prMain.title = "Multi-Text Insertion Exammple";
prMain.addButton("OK");

//Put in place multiple optional selections
prMain.message = "Toggle on the entries you would like to insert.";
prMain.addSwitch("opt1", "Other Specified Trauma-and Stressor-Related Disorder", false);
prMain.addSwitch("opt2", "AD, with Mixed Anxiety and Depressed Mood", false);
prMain.addSwitch("opt3", "AD, with anxiety", false);
prMain.addSwitch("opt4", "AD, With depressed mood", false);
prMain.addSwitch("opt5", "AD, Unspecified", false);
prMain.addSwitch("opt6", "Specific Phobia (driver related)", false);
prMain.addSwitch("opt7", "Specific Phobia (passenger related)", false);
prMain.addSwitch("opt8", "Specific Phobia (driver and passenger related)", false);
prMain.addSwitch("opt9", "Major Depressive Disorder", false);
prMain.addSwitch("opt10", "Somatic Symptom Disorder", false);

//This could also be built programmatically from an array or some JSON.

//Example to show how text field can be integrated.
//Here we provide an option to change the separator for the output from a 
//comma space to something else like a pipe or a new line.
//prMain.addTextField("optSep", "Separator", ", ");

//Display the prompt and take action on OK selection
if (prMain.show())
{
	//Initialise an array of outputs
	let astrOutput = [];
	
	//For each optonal section, check if it is enabled and if so, output
	//one of the values
	if (prMain.fieldValues["opt1"]) astrOutput.push("F43.8 Other Specified Trauma-and Stressor-Related Disorder (Adjustment-like disorder with prolonged duration of more than six months)");
	if (prMain.fieldValues["opt2"]) astrOutput.push("F43.23 Adjustment Disorder, with Mixed Anxiety and Depressed Mood");
	if (prMain.fieldValues["opt3"]) astrOutput.push("F43.22 Adjustment Disorder, with anxiety");
	if (prMain.fieldValues["opt4"]) astrOutput.push("F43.21 Adjustment Disorder, With depressed mood");
	if (prMain.fieldValues["opt5"]) astrOutput.push("F43.20 Adjustment Disorder, Unspecified");
	if (prMain.fieldValues["opt6"]) astrOutput.push("F40.248 Specific Phobia, Situational Type (driver related)");
	if (prMain.fieldValues["opt7"]) astrOutput.push("F40.248 Specific Phobia Situational Type (passenger related)");
	if (prMain.fieldValues["opt8"]) astrOutput.push("F40.248 Specific Phobia Situational Type (driver and passenger related)");
	if (prMain.fieldValues["opt9"]) astrOutput.push("F32.1 Major Depressive Disorder, Single Episode, moderate, with anxious distress");
	if (prMain.fieldValues["opt10"]) astrOutput.push("F45.1 Somatic Symptom Disorder, With predominant pain, Persistent, (Moderate)");

varDX= ("Mr. XX's interview data as well as the psychometric testing support the ICD-10 and DSM-5 criteria of " + astrOutput.join(" and ") + ".");
}
	
//Set-up a basic prompt - Impairment Level
let prClass = Prompt.create();
prClass.title = "Impairment Level";
prClass.addButton("OK");

//Put in place multiple optional selections
prClass.message = "Toggle on the entries you would like to insert.";
prClass.addSwitch("opt1", "Class 2 - Mild impairment", false);
prClass.addSwitch("opt2", "Class 3 - Moderate impairment", false);
prClass.addSwitch("opt3", "Class 4 - Marked impairment", false);
//This could also be built programmatically from an array or some JSON.

//Display the prompt and take action on OK selection

if (prClass.show())
{
	//Initialise an array of outputs
	let classOutput = [];
	
	//For each optonal section, check if it is enabled and if so, output
	//one of the values
	if (prClass.fieldValues["opt1"]) classOutput.push("Class 2 - Mild Impairment (impairment levels are compatible with most useful functioning");
	if (prClass.fieldValues["opt2"]) classOutput.push("Class 3 - Moderate Impairment (impairment levels are compatible with some, but not all, useful functioning)");
	if (prClass.fieldValues["opt3"]) classOutput.push("Class 4 - Marked Impairment (impairment levels significantly prevent useful functioning");

varClass = ("According to the American Medical Association Classification Table of Impairments Due to Mental and Behavioural Disorders, his condition is consistent with " + classOutput + ".");
}

//Insert the selected entries joined by the specified separator.
editor.setSelectedText(varDX + "\n\n" + varClass);

//Activate the editor
editor.activate();

Well, it depends what you want it to do. If it works for you, then great.

I’ve given it a quick try and it seems functional in the way I might expect. But I can make a few suggestions.

  1. You may wish to account for cancelling.
  2. I think you want to change the title of the first prompt to something more appropriate.
  3. Presumably all of your patients/clients are not called “Mr. XX”, so how about capturing the desired details for that?

I’ve taken a quick round at picking up all three of those, and renaming the example prompt too. Given it is using multiple prompts, I’ve switched up the code a little to use some functions to wrap each of the prompts. It could potentially give you some flexibility and re-use if you continue taking this further.

Here’s the code feel free to ignore, or to tailor/build on it should it happen to help in any way. It starts with the three functions, and then it’s actually the code beneath that which will control the flow of what is happening, by first prompting for the ‘who’ details. Then if it get’s a non-empty result (which a cancel produces), it will prompt for the ‘criteria’ details. Then, assuming a non-empty result, it will prompt for the ‘classification’. If this third and final result gives a non-empty result, the output of the three prompts will be combined and inserted into the current cursor position in the editor. As well as reactivating the editor, I’ve also added a line that will move the cursor to the end of the selection so you can continue writing straight away if any further details are required.

function checkWho()
{
	let prWho = Prompt.create();
	prWho.title = "Patient Information";
	prWho.addButton("OK");
	prWho.addSelect("optTitle", "Title", ["Mr", "Miss", "Ms", "Mrs", "Dr", "Rev"], ["Mr"], false)
	prWho.addTextField("optName", "Surname of Patient", "XX");

	if (prWho.show())
	{


		return prWho.fieldValues["optTitle"] + " " + prWho.fieldValues["optName"] + "'s";
	}
	else return "";
}

function checkCriteria()
{
	//Set-up a basic prompt
	let prCriteria = Prompt.create();
	prCriteria.title = "Criteria Selection";
	prCriteria.addButton("OK");
	prCriteria.message = "Toggle on the entries you would like to insert.";
	prCriteria.addSwitch("opt1", "Other Specified Trauma-and Stressor-Related Disorder", false);
	prCriteria.addSwitch("opt2", "AD, with Mixed Anxiety and Depressed Mood", false);
	prCriteria.addSwitch("opt3", "AD, with anxiety", false);
	prCriteria.addSwitch("opt4", "AD, With depressed mood", false);
	prCriteria.addSwitch("opt5", "AD, Unspecified", false);
	prCriteria.addSwitch("opt6", "Specific Phobia (driver related)", false);
	prCriteria.addSwitch("opt7", "Specific Phobia (passenger related)", false);
	prCriteria.addSwitch("opt8", "Specific Phobia (driver and passenger related)", false);
	prCriteria.addSwitch("opt9", "Major Depressive Disorder", false);
	prCriteria.addSwitch("opt10", "Somatic Symptom Disorder", false);

	if (prCriteria.show())
	{
		let astrOutput = [];
		if (prCriteria.fieldValues["opt1"]) astrOutput.push("F43.8 Other Specified Trauma-and Stressor-Related Disorder (Adjustment-like disorder with prolonged duration of more than six months)");
		if (prCriteria.fieldValues["opt2"]) astrOutput.push("F43.23 Adjustment Disorder, with Mixed Anxiety and Depressed Mood");
		if (prCriteria.fieldValues["opt3"]) astrOutput.push("F43.22 Adjustment Disorder, with anxiety");
		if (prCriteria.fieldValues["opt4"]) astrOutput.push("F43.21 Adjustment Disorder, With depressed mood");
		if (prCriteria.fieldValues["opt5"]) astrOutput.push("F43.20 Adjustment Disorder, Unspecified");
		if (prCriteria.fieldValues["opt6"]) astrOutput.push("F40.248 Specific Phobia, Situational Type (driver related)");
		if (prCriteria.fieldValues["opt7"]) astrOutput.push("F40.248 Specific Phobia Situational Type (passenger related)");
		if (prCriteria.fieldValues["opt8"]) astrOutput.push("F40.248 Specific Phobia Situational Type (driver and passenger related)");
		if (prCriteria.fieldValues["opt9"]) astrOutput.push("F32.1 Major Depressive Disorder, Single Episode, moderate, with anxious distress");
		if (prCriteria.fieldValues["opt10"]) astrOutput.push("F45.1 Somatic Symptom Disorder, With predominant pain, Persistent, (Moderate)");

		return "interview data as well as the psychometric testing support the ICD-10 and DSM-5 criteria of " + astrOutput.join(" and ") + ".";
	}
	else return "";
}
	

function checkClass()
{
	//Set-up a basic prompt - Impairment Level
	let prClass = Prompt.create();
	prClass.title = "Impairment Level";
	prClass.addButton("OK");
	prClass.message = "Toggle on the entries you would like to insert.";
	prClass.addSwitch("opt1", "Class 2 - Mild impairment", false);
	prClass.addSwitch("opt2", "Class 3 - Moderate impairment", false);
	prClass.addSwitch("opt3", "Class 4 - Marked impairment", false);

	if (prClass.show())
	{
		let classOutput = [];
		if (prClass.fieldValues["opt1"]) classOutput.push("Class 2 - Mild Impairment (impairment levels are compatible with most useful functioning");
		if (prClass.fieldValues["opt2"]) classOutput.push("Class 3 - Moderate Impairment (impairment levels are compatible with some, but not all, useful functioning)");
		if (prClass.fieldValues["opt3"]) classOutput.push("Class 4 - Marked Impairment (impairment levels significantly prevent useful functioning");

		return "According to the American Medical Association Classification Table of Impairments Due to Mental and Behavioural Disorders, his condition is consistent with " + classOutput + ".";
	}
	else return "";
}

//Enter the person's details
let strWho = checkWho();
//If we didn't cancel then we'll continue with classification
if (strWho.length > 0)
{
	//Select the criteria
	let strCriteria = checkCriteria();
	//If we didn't cancel then we'll continue with classification
	if (strCriteria.length > 0)
	{
		//Select the classification
		let strClass = checkClass();
		//If we didn't cancel then we'll continue with outputting
		if (strClass.length > 0) editor.setSelectedText(strWho + " " + strCriteria + "\n\n" + strClass);
	}
}

//Reposition the cursor at the end of the inserted selection and activate the editor.
editor.setSelectedRange(editor.getSelectedRange()[0] + editor.getSelectedRange()[1], 0);
editor.activate();

Hope that helps.

Thank you for including a prompt. This will be useful for other scenarios. For the current scenario, I use a Find & Replace RegEx after the report is written to fix the name of the person. However, a prompt is useful to manually choose between Diagnosis and Diagnoses depending on if I select one or more criteria. I assume this could be done with this script automatically?

Here is what I have at this point:

function checkHowMany()
{
	let prHowMany = Prompt.create();
	prHowMany.title = "Single or Multiple Diagnosis";
	prHowMany.addButton("OK");
	prHowMany.addSelect("optDX", "", ["Diagnosis", "Diagnoses"], ["Diagnosis"], true);

	if (prHowMany.show())
	{


		return prHowMany.fieldValues["optDX"];
	}
	else return "";
}

function checkCriteria()
{
	//Set-up a basic prompt
	let prCriteria = Prompt.create();
	prCriteria.title = "DSM-5 Diagnosis";
	prCriteria.addButton("OK");
	prCriteria.message = "Choose Diagnosis.";
	prCriteria.addSwitch("opt1", "Other Specified Trauma-and Stressor-Related Disorder", true);
	prCriteria.addSwitch("opt2", "AD, with Mixed Anxiety and Depressed Mood", false);
	prCriteria.addSwitch("opt3", "AD, with anxiety", false);
	prCriteria.addSwitch("opt4", "AD, With depressed mood", false);
	prCriteria.addSwitch("opt5", "AD, Unspecified", false);
	prCriteria.addSwitch("opt6", "Specific Phobia (driver related)", false);
	prCriteria.addSwitch("opt7", "Specific Phobia (passenger related)", false);
	prCriteria.addSwitch("opt8", "Specific Phobia (driver and passenger related)", false);
	prCriteria.addSwitch("opt9", "Major Depressive Disorder", false);
	prCriteria.addSwitch("opt10", "Somatic Symptom Disorder", false);

	if (prCriteria.show())
	{
		let astrOutput = [];
		if (prCriteria.fieldValues["opt1"]) astrOutput.push("F43.8 Other Specified Trauma-and Stressor-Related Disorder (Adjustment-like disorder with prolonged duration of more than six months)");
		if (prCriteria.fieldValues["opt2"]) astrOutput.push("F43.23 Adjustment Disorder, with Mixed Anxiety and Depressed Mood");
		if (prCriteria.fieldValues["opt3"]) astrOutput.push("F43.22 Adjustment Disorder, with anxiety");
		if (prCriteria.fieldValues["opt4"]) astrOutput.push("F43.21 Adjustment Disorder, With depressed mood");
		if (prCriteria.fieldValues["opt5"]) astrOutput.push("F43.20 Adjustment Disorder, Unspecified");
		if (prCriteria.fieldValues["opt6"]) astrOutput.push("F40.248 Specific Phobia, Situational Type (driver related)");
		if (prCriteria.fieldValues["opt7"]) astrOutput.push("F40.248 Specific Phobia Situational Type (passenger related)");
		if (prCriteria.fieldValues["opt8"]) astrOutput.push("F40.248 Specific Phobia Situational Type (driver and passenger related)");
		if (prCriteria.fieldValues["opt9"]) astrOutput.push("F32.1 Major Depressive Disorder, Single Episode, moderate, with anxious distress");
		if (prCriteria.fieldValues["opt10"]) astrOutput.push("F45.1 Somatic Symptom Disorder, With predominant pain, Persistent, (Moderate)");

		return "Mr. XX's interview data as well as the psychometric testing support the ICD-10 and DSM-5 criteria of " + astrOutput.join(" and ") + ".";
	}
	else return "";
}
	

function checkClass()
{
	//Set-up a basic prompt - Impairment Level
	let prClass = Prompt.create();
	prClass.title = "Impairment Level";
	prClass.addButton("OK");
	prClass.message = "Toggle on the entries you would like to insert.";
	prClass.addSwitch("opt1", "Class 2 - Mild impairment", false);
	prClass.addSwitch("opt2", "Class 3 - Moderate impairment", true);
	prClass.addSwitch("opt3", "Class 4 - Marked impairment", false);

	if (prClass.show())
	{
		let classOutput = [];
		if (prClass.fieldValues["opt1"]) classOutput.push("Class 2 - Mild Impairment (impairment levels are compatible with most useful functioning");
		if (prClass.fieldValues["opt2"]) classOutput.push("Class 3 - Moderate Impairment (impairment levels are compatible with some, but not all, useful functioning)");
		if (prClass.fieldValues["opt3"]) classOutput.push("Class 4 - Marked Impairment (impairment levels significantly prevent useful functioning");

		return "According to the American Medical Association Classification Table of Impairments Due to Mental and Behavioural Disorders, his condition is consistent with " + classOutput + ".";
	}
	else return "";
}

//Enter the person's details
let strHowMany = checkHowMany();
//If we didn't cancel then we'll continue with classification
if (strHowMany.length > 0)
{
	//Select the criteria
	let strCriteria = checkCriteria();
	//If we didn't cancel then we'll continue with classification
	if (strCriteria.length > 0)
	{
		//Select the classification
		let strClass = checkClass();
		//If we didn't cancel then we'll continue with outputting
		if (strClass.length > 0) editor.setSelectedText(strHowMany + "\n\n" + strCriteria + "\n\n" + strClass);
	}
}

//Reposition the cursor at the end of the inserted selection and activate the editor.
editor.setSelectedRange(editor.getSelectedRange()[0] + editor.getSelectedRange()[1], 0);
editor.activate();

@sylumer Thanks to your examples and help I got inspired to learn basic JavaScript. I purchased “The Complete JavaScript Course 2020” from Udemy yesterday. Hope it will be a good intro to become better at doing Drafts magic :slight_smile:

Following up on my own post. I learned some basic Javas Script skills and updated and simplified my own script. This one also includes an instance where if more than one diagnosis is selected, the word diagnosis becomes diagnoses.

//Set-up a basic prompt - Diagnosis

let pr = Prompt.create();

pr.title = "DSM-5 Diagnosis";
pr.addButton("OK");
isCancellable: true;
pr.message = "Select Diagnosis.";
pr.addSwitch("opt1", "Other Specified Trauma-and Stressor-Related Disorder", true);
pr.addSwitch("opt2", "AD, with Mixed Anxiety and Depressed Mood", false);
pr.addSwitch("opt3", "AD, with anxiety", false);
pr.addSwitch("opt4", "AD, With depressed mood", false);
pr.addSwitch("opt5", "AD, Unspecified", false);
pr.addSwitch("opt6", "Specific Phobia (driver related)", false);
pr.addSwitch("opt7", "Specific Phobia (passenger related)", false);
pr.addSwitch("opt8", "Specific Phobia (driver and passenger related)", false);
pr.addSwitch("opt9", "Major Depressive Disorder", false);
pr.addSwitch("opt10", "Somatic Symptom Disorder", false);

if (pr.show())
{

let dxOutput = [];

if (pr.fieldValues["opt1"]) dxOutput.push("F43.8 Other Specified Trauma-and Stressor-Related Disorder (Adjustment-like disorder with prolonged duration of more than six months)");
if (pr.fieldValues["opt2"]) dxOutput.push("F43.23 Adjustment Disorder, with Mixed Anxiety and Depressed Mood");
if (pr.fieldValues["opt3"]) dxOutput.push("F43.22 Adjustment Disorder, with anxiety");
if (pr.fieldValues["opt4"]) dxOutput.push("F43.21 Adjustment Disorder, With depressed mood");
if (pr.fieldValues["opt5"]) dxOutput.push("F43.20 Adjustment Disorder, Unspecified");
if (pr.fieldValues["opt6"]) dxOutput.push("F40.248 Specific Phobia, Situational Type (driver related)");
if (pr.fieldValues["opt7"]) dxOutput.push("F40.248 Specific Phobia Situational Type (passenger related)");
if (pr.fieldValues["opt8"]) dxOutput.push("F40.248 Specific Phobia Situational Type (driver and passenger related)");
if (pr.fieldValues["opt9"]) dxOutput.push("F32.1 Major Depressive Disorder, Single Episode, moderate, with anxious distress");
if (pr.fieldValues["opt10"]) dxOutput.push("F45.1 Somatic Symptom Disorder, With predominant pain, Persistent, (Moderate)");

dxOutput.length < 2 ? dxWord = "Diagnosis" : dxWord = "Diagnoses";

var dx = `### ${dxWord}\n\nMr. XX's interview data as well as the psychometric testing support the ICD-10 and DSM-5 criteria of ${dxOutput.join(" and ")}.`;
}
	else {
	editor.activate();
	}

//Set-up a basic prompt - Impairment Level

let prClass = Prompt.create();
prClass.title = "Impairment Level";
prClass.addButton("OK");
///prClass.message = "Chioose a class level.";
prClass.addSwitch("class2", "Class 2 - Mild impairment", false);
prClass.addSwitch("class3", "Class 3 - Moderate impairment", true);
prClass.addSwitch("class4", "Class 4 - Marked impairment", false);

if (prClass.show())
{
let classOutput = [];
if (prClass.fieldValues["class2"]) classOutput.push("Class 2 - Mild Impairment (impairment levels are compatible with most useful functioning");
if (prClass.fieldValues["class3"]) classOutput.push("Class 3 - Moderate Impairment (impairment levels are compatible with some, but not all, useful functioning)");
if (prClass.fieldValues["class4"]) classOutput.push("Class 4 - Marked Impairment (impairment levels significantly prevent useful functioning");

var classLevel = "According to the American Medical Association Classification Table of Impairments Due to Mental and Behavioural Disorders, his condition is consistent with " + classOutput + ".";
}
else {
editor.activate();
}

// Paste Results

editor.setSelectedText(dx + "\n\n" + classLevel);

//Reposition the cursor at the end of the inserted selection and activate the editor.
editor.setSelectedRange(editor.getSelectedRange()[0] + editor.getSelectedRange()[1], 0);
editor.activate();
2 Likes

Thank you. I think I can use this technique for creating a draft from a selectable template. (addSwitch had passed me by.)