Drafts Action Directory

Insert Text Element - easier maintenance

Posted by davenicholls, Last updated: 2018-07-04 08:44:24 UTC

Based on @nahumck’s original, this version holds the replacement options in a JavaScript Object, which allows entries to be added/changed without the need to modify the main code.

This action allows prompts for a selection of text to insert. You can modify this to be a single insert or expand it to contain multiple prompts.



  • script

    //Insert Text Elements
    //Script by @nahumck
    // Modified by @davenicholls
    // Create an object containing all the text element choices
    // Format 'property':'value'
    // property is the text that will appear in the prompt
    // value is the value that will be returned
    // Value can be text (enclosed in "s) or a javascript function name
    // Functions are used where the inserted value is to be calculated at runtime
    // Add extra items as required. Each item should have a comma on the end, apart from the last one (although it should work if the last one has a comma as well
    myOptions = {
    	'script divider':"\/* --------------------- *\/",
    	'current draft link':getDraftLink
    // Function to calculate and return the correct URL
    // Functions need to return a string
    function getDraftLink() {
    	var dtext = draft.processTemplate("[[uuid]]");
    	return "drafts5://x-callback-url/open?uuid="+dtext;
    var selRange = editor.getSelectedRange();
    var p = Prompt.create();
    p.title = "Text Element";
    p.message = "What do you want to insert?";
    // Iterate all properties in the myOptions object adding a button for each one
    for (const [key, value] of Object.entries(myOptions)) {
    var con = p.show();
    if (con) {
      var option = myOptions[p.buttonPressed]
    	// Set text to a blank string. Assumes that no entry in MyOptions will ever return like this
    	var text = ""
    	// we need to check the type of the value so we know how to handle it
    	switch (typeof(option)) {
    		// For strings just use the value as is
    		case 'string':
    		// Run the function and set text to the return value
      		case 'function':
    		// Used to catch any problems with myOptions
    			console.log("Item error - "+ p.buttonPressed + "<->" + option + "<->" + typeof(option));
      			context.fail("Bad entry in myOptions");
      if (text!=="") {
    else {


  • After Success Default
    Notification Error
    Log Level Info


Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.