Action

Spark Mailer v2

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

Steps

  • script

    // INFORMATION
    
    // The section below defines what the expected extension template for Safari should be defined as.
    // This is as per discussion at https://forums.getdrafts.com/t/need-spark-action-that-sends-copies/12486/
    // Line 1 = Page Title
    // Line 2 = URL
    // Line 3 = {Intentionally Blank}
    // Line 4+ = Selected text (if any)
    
    
    /*
    [[title]]
    [[url]]
    
    [[selection]]
    */
    
    // Maintain the TO tag step with {name,e-mail address,nickname}
    // Maintain the CC tag step with {name,e-mail address}
    // Maintain the BCC tag step with {name,e-mail address}
    
  • defineTemplateTag

    name
    TO
    template
    Scrooge McDuck,boo@disney.com,Cheapskate
    Minipherd Mouse,juno@disney.com,Mini
    Mickalardio Mouse,sashi@disney.com,Mickey
  • defineTemplateTag

    name
    CC
    template
    Dubious Duck,dduck@disney.com
  • defineTemplateTag

    name
    BCC
    template
  • script

    // Build the "To" list of e-mail addresses and nicknames
    let strEmailTo = "";
    let strEmailGreeting = "";
    if (draft.getTemplateTag("TO") != "")
    {
    	let astrToInfo = selPrompt(draft.getTemplateTag("TO"), 0, "Select Recipients").split("\n");
    
    	if (astrToInfo.join("").length > 0)
    	{
    		// Build e-mail address list
    		astrTo = astrToInfo.map(strInfo => strInfo.split(",")[1].trim());
    		strEmailTo = astrTo.join(",");
    
    		// Build nickname list
    		astrNicknameList = astrToInfo.map(strInfo => strInfo.split(",")[2].trim());
    		strNicknameList = astrNicknameList.join(", ").replace(/(.*),/, "$1 and") + ":";
    	}
    	else app.displayErrorMessage("No recipients identified");
    	context.cancel();
    }
    
    // Build the "Cc" list of e-mail addresses
    let strEmailCc = "";
    if (draft.getTemplateTag("CC") != "")
    {
    	let astrCcInfo = selPrompt(draft.getTemplateTag("CC"), 0, "Select Carbon Copies").split("\n");
    	if (astrCcInfo.join("").length > 0)
    	{
    		astrCc = astrCcInfo.map(strInfo => strInfo.split(",")[1].trim());
    		strEmailCc = astrCc.join(",");
    	}
    }
    
    // Build the "Bcc" list of e-mail addresses
    let strEmailBcc = "";
    if (draft.getTemplateTag("BCC") != "")
    {
    	let astrBccInfo = selPrompt(draft.getTemplateTag("BCC"), 0, "Select Blind Carbon Copies").split("\n");
    	if (astrBccInfo.join("").length > 0)
    	{
    		astrBcc = astrBccInfo.map(strInfo => strInfo.split(",")[1].trim());
    		strEmailBcc = astrBcc.join(",");
    	}
    }
    
    // Get the page title, URL, and selection
    let strPageTitle = draft.title;
    let strPageURL = draft.processTemplate("[[line|2]]");
    let strPageSelection = draft.processTemplate("[[line|4..]]");
    
    // Build and call the Spark URL
    // Use the callback object to make adding parameters easier ... but *NOT* running as a call back.
    // Base settings
    let cburlSpark = CallbackURL.create();
    cburlSpark.waitForResponse = false;
    cburlSpark.baseURL = "readdle-spark://compose";
    // Who gets it
    if (strEmailTo != "") cburlSpark.addParameter("recipient", strEmailTo);
    if (strEmailCc != "") cburlSpark.addParameter("cc", strEmailCc);
    if (strEmailBcc != "") cburlSpark.addParameter("bcc", strEmailBcc);
    // Subject line
    cburlSpark.addParameter("subject", strPageTitle);
    // Body
    cburlSpark.addParameter("body",`${strNicknameList}
    
    ${strPageURL}
    
    
    ${strPageSelection}`);
    // Open Spark
    cburlSpark.open();
    
    
    // Selection prompt
    function selPrompt(p_strList, p_intElement, p_strTitle)
    {
    	let astrLines = p_strList.split("\n");
    	
    	let astrChoices = astrLines.map(strLine => {return strLine.split(",")[p_intElement].trim();});
    
    	let promptMulti = Prompt.create();
    	promptMulti.title = p_strTitle;
    	promptMulti.addSelect("selection", "Choose", astrChoices, [], true);
    	promptMulti.addButton("OK");
    
    	if (promptMulti.show())
    	{
    		let astrResults = promptMulti.fieldValues["selection"];
    
    		let astrReturn = astrLines.filter(checkResults)
    
    		function checkResults(p_strLine)
    		{
    			return astrResults.includes(p_strLine.split(",")[p_intElement].trim())
    		}
    		return astrReturn.join("\n");
    	}
    	return "";
    }

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.