Append to Selected Lines

Posted by RosemaryOrchard, Last update over 2 years ago

Append text you input through a prompt to every selected line (or if no line are selected, all lines in the draft).


  • script

    (() => {
    	const indexPlaceholder = '[X]';
    	// Define the text that's going to be numbered.
    	let st, len;
    	if (editor.getSelectedRange()[1] > 0) { // use selected lines
    		[st, len] = editor.getSelectedLineRange();
    	else { // use full text
    		[st, len] = [0, editor.getText().length];
    	// Ask for the text to append
    	let p = Prompt.create();
    	p.title = "Append to Lines";
    	p.message = indexPlaceholder + " will be replaced with the index (number) of the line"
    	p.addTextField("appendText", "Append", "");
    	p.addTextField("appendPrefix", "Before Text", " "); //default to adding a space before the text to be appended
    	let promptOk =;
    	if (!promptOk) {
    	let appendText = p.fieldValues["appendPrefix"] +  p.fieldValues["appendText"];
    	let lnText = editor.getTextInRange(st, len).trim();
    	// Break the text into lines and append to them
    	let lines = lnText.split('\n');
    	let newLines = [];
    	lines.forEach(function(line, index) {
    		let thisAppend = appendText.replace(indexPlaceholder, index);
    		newLines.push(line + thisAppend);
    	// Replace the original text with the appended text
    	let newText = newLines.join('\n');
    	editor.setTextInRange(st, len, newText);
    	editor.setSelectedRange(st, newText.length);


  • 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.