Action

Modify Selection

Posted by agiletortoise, Last update 1 day ago

Prompt for text transformations description, and apply that transformation to the selected text in the editor, replacing the selection with the result.

The change will be undoable after the update is applied.

This action is meant as an example action to demonstrate the use of ModelSession scripting. (docs)

(requires iOS/macOS 27 + Apple Intelligence)

Steps

  • script

    // get editor values
    let [st, len] = editor.getSelectedRange()
    let text = editor.getSelectedText()
    
    let p = new Prompt()
    p.title = "Modify Selection"
    p.message = "Rewrite the text selected in the editor, requesting the transformations below."
    p.addTextView("prompt", "Transformations")
    p.addButton("Modify")
    
    if (p.show()) {
    	let mods = p.fieldValues["prompt"]
    	let prompt = `Rewrite the text below using the transformations "${mods}", and returning only the rewritten text:
    	
    	${text}`
    
    	// Configure model
    	let mode = "pcc" // try Private Cloud first
    	if (!ModelSession.isAvailable(mode)) {
    		mode = "onDevice"
    	}
    	
    	// CREATE MODEL OBJECT
    	let m = new ModelSession()
    	m.mode = mode
    	let response = m.respond(prompt)
    	
    	// IF TAGS RETURNED, PROMPT TO SELECT AND ASSIGN
    	if (response) {
    		editor.setSelectedText(response)
    		editor.setSelectedRange(st, response.length)
    	}
    	else {
    		alert(m.lastError)
    		context.fail()
    	}
    }
    
    
    
    

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.