Drafts Action Directory

Convert to comma delimited list

Posted by @dforth, Last updated: 2018-07-04 04:32:25 UTC

Convert selected lines of text to comma delimited list. Removes duplicate items. Option to quote items.

Install

Steps

  • script

    // See online documentation for examples
    // http://getdrafts.com/scripting
    
    
    
    function process(original, quote) {
    
    	let items = original.split("\n");
    	let results = [];
    
    	items.forEach(function(item) {
    		let value = item.trim();
    		if (value != '') {
    			if (!results.find(function(target){
    					return target == value;
    				})){
    				if (quote != null) {
    					value = quote + value + quote;
    				}
    				results.push(value);
    			}
    		}
    	});
    
    	return results.join(",");
    }
    
    // get selected text
    var text = editor.getSelectedText();
    
    if (text && text != "") {
    
    	let p = Prompt.create();
    	p.title = "Settings";
    	p.addSelect(
    		"quotes",		"Quotes:", 
    		["None", "\"", "'"],
    		["None"],
    		false
    	);
    	p.addButton("Ok");
    	
    	let doIt = p.show();
    	if (doIt) {
    	
    		let selectValue = p.fieldValues["quotes"];
    		if (selectValue == "None") {
    			quote = null;
    		} else {
    			quote = selectValue;
    		}
    
    		// process the text
    		let result = process(text, quote);
    		editor.setSelectedText(result);
    	}	
    }
    
    

Comments

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