Action

Insert Date (Prompt)

Posted by sylumer, Last update about 2 months ago - Unlisted

UPDATES

about 2 months ago

Amended comment typo (missing ā€™sā€™ in process)

Insert current date from a button list in a prompt,
Format taken from a list in OPTIONS tag definition (one option per new line), and based on Date.js valid formats.

Steps

  • defineTemplateTag

    name
    OPTIONS
    template
    yyyy-MM-dd
    MMMM d, yyyy
    MM/dd/yyyy
    MM-dd-yyyy
    MM/d/yy
    MM-d-yy
  • script

    //Create prompt
    let promptDate = Prompt.create();
    promptDate.title = "Insert Formatted Date";
    draft.getTemplateTag("OPTIONS").split("\n").map(strDateOption => promptDate.addButton(strDateOption));
    
    //Process the prompt response
    if (promptDate.show())
    {
    	let strDate = new Date().toString(promptDate.buttonPressed);
    	if (strDate != undefined)
    	{
    		//Replace selection
    		const [intSelStart, intSelLen] = editor.getSelectedRange();
    		editor.setTextInRange(intSelStart, intSelLen, strDate);
    		//Position cursor at end of insertion
    		editor.setSelectedRange(intSelStart + strDate.length, 0);
    	}
    	else app.displayErrorMessage(`"${promptDate.buttonPressed}" could not be converted to a date.`);
    }
    
    //Activate editor
    editor.activate();

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.