Action

Convert to comma delimited list

Posted by @dforth, Last update over 6 years ago

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

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);
    	}	
    }
    
    

Options

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