Action

Paragraph Mode

Posted by Martin Packer, Last update over 4 years ago

Prototype “Paragraph Mode” dialog where you can apply actions to selected paragraphs and then save back to the current draft.

“Apply” applies transformations. “Save” saves back to current draft.

It’s easy-to-extend javascript.

Steps

  • script

    function numberParagraph(p, text){
    	return p.toString().padStart(8) + " " + text
    }
    
    function getParagraphNumber(text){
    	return parseInt(text.substring(1,8))
    }
    
    function getParagraphText(text){
    	return text.substring(9)
    }
    
    function doSave(numberedParagraphs){
    	textToSave=""
    	paragraphCount = numberedParagraphs.length
    	for(p = 0; p < paragraphCount; p++){
    		textToSave += getParagraphText(numberedParagraphs[p])
    		if(p < paragraphCount - 1){
    			textToSave += "\n\n"
    		}
    	}
    	
    	draft.content = textToSave
    }
    
    function doApply(numberedParagraphs, selectedNumberedParagraphs, action){
    	// Add action number and functions below
    	switch(action){
    	case 0:
    		// Upper case
    		doUpperCase(numberedParagraphs, selectedNumberedParagraphs)
    		break
    
    	case 1:
    		// Lower case
    		doLowerCase(numberedParagraphs, selectedNumberedParagraphs)
    		break
    
    	case 2:
    		// Bulleted list with asterisk
    		doMakeListItem(numberedParagraphs, selectedNumberedParagraphs,"* ")
    		break
    	
    	case 3:
    		// Numbered list
    		doMakeListItem(numberedParagraphs, selectedNumberedParagraphs,"1. ")
    		break
    
    	}
    }
    
    function doUpperCase(numberedParagraphs, selectedNumberedParagraphs){
    	selectedCount=selectedNumberedParagraphs.length
    	for(p = 0; p < selectedCount; p++){
    		paragraphNumber = getParagraphNumber(selectedNumberedParagraphs[p])
    		numberedParagraphs[paragraphNumber] = numberedParagraphs[paragraphNumber].toUpperCase()
    	}
    }
    
    function doLowerCase(numberedParagraphs, selectedNumberedParagraphs){
    	selectedCount=selectedNumberedParagraphs.length
    	for(p = 0; p < selectedCount; p++){
    		paragraphNumber = getParagraphNumber(selectedNumberedParagraphs[p])
    		numberedParagraphs[paragraphNumber] = numberedParagraphs[paragraphNumber].toLowerCase()
    	}
    }
    
    function doMakeListItem(numberedParagraphs, selectedNumberedParagraphs,marker){
    	selectedCount=selectedNumberedParagraphs.length
    	for(p = 0; p < selectedCount; p++){
    		paragraphNumber = getParagraphNumber(selectedNumberedParagraphs[p])
    		numberedParagraphs[paragraphNumber] = numberParagraph(paragraphNumber,marker + getParagraphText(numberedParagraphs[paragraphNumber]))
    	}
    }
    
    
    draftContents = draft.content
    
    paragraphs = draftContents.split("\n\n")
    
    paragraphCount = paragraphs.length
    
    // Create array of paragraphs' text, with line number prepended to each item
    numberedParagraphs = []
    for(p = 0; p < paragraphCount; p++){
    	numberedParagraphs[p] = numberParagraph(p, paragraphs[p])
    }
    
    actions = ["Upper Case", "Lower Case", "Bullet With *","Numbered List"]
    
    keepGoing = true
    
    while(keepGoing){
    
    	p = Prompt.create()
    
    	p.title = "Run Paragraph-Level Actions"
    
    	p.addButton("Apply")
    	p.addButton("Save")
    
    	p.message = "Select paragraphs and an action. 'Apply' runs the action. 'Save' updates the draft. 'Cancel' cancels."
    	
    	p.addSelect("paragraphs", "", numberedParagraphs, [], true)
    	
    	p.addPicker("actions", "", [actions])
    		
    	if(p.show()){
    		// Didn't press cancel
    		switch(p.buttonPressed){
    		case "Save":
    			doSave(numberedParagraphs)
    
    			keepGoing = false
    
    			break;
    		
    		case "Apply":
    			doApply(numberedParagraphs, p.fieldValues["paragraphs"], p.fieldValues["actions"][0])
    		
    			keepGoing = true
    
    			break;
    		
    		}
    	}else{
    		// Cancel pressed
    		keepGoing = false
    	}
    }
    

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.