Action

ChatGPT: Modify Selection

Posted by agiletortoise, Last update 6 months ago

UPDATES

6 months ago

Default to new “gpt-4o” model

show all updates...

6 months ago

Default to new “gpt-4o” model

7 months ago

Added prompt options to select how to handle the result.

NOTE: For more information on using this action, visit our OpenAI integration guide. You will need to setup an API key before use of any ChatGPT actions.

Prompt for a text manipulation instruction and request ChatGPT to perform that instruction on the selected text. By default, the selection will be replaced with the result, but the prompt also allows you to select other output options (append, clipboard).

For example, select the text “my text” in the draft, run this action, and give the instruction “uppercase” in the prompt. The result will be “MY TEXT.”

This becomes more useful when you want to apply a series of instructions. For example, select the text “Happy Birthday Joe!” in a draft and use the instruction “uppercase and insert an 🎉 emoji between each word” to get “HAPPY 🎉 BIRTHDAY 🎉 JOE!”

Steps

  • script

    // change variable to alter models available
    const models = [
    	"gpt-4o",
    	"gpt-3.5-turbo"
    ]
    // get your input selected text, and store range for later
    const selection = editor.getSelectedText()
    const [st, len] = editor.getSelectedRange()
    
    let f = () => {
    	let p = new Prompt()
    	p.title = "ChatGPT: Modify Selection"
    	p.message = "What do you want AI to do with the selected text?"
    	p.addTextView("instruction", "Prompt", "")
    	p.addLabel("caption", "Provide an instruction to transform the selected text in the draft, for example `capitalize`, `translate into spanish`")
    	p.addSelect("result", "Result", ["Replace Selection", "Append to Selection", "Place in Clipboard"], ["Replace Selection"], false)
    	p.addSelect("model", "Model", models, [models[0]], false)
    	p.addButton("Modify")
    	
    	if (!p.show()) {
    		return false
    	}
    	const instruction = p.fieldValues["instruction"]
    	if (instruction == 0) { return false }
    
    	// build prompt with instruction and selection
    	let chatPrompt = `${instruction} the following text, returning only the result: ${selection}`
    	// send that to ChatGPT
    	let ai = OpenAI.create()
    	ai.model = p.fieldValues["model"]
    	let answer = ai.quickChatResponse(chatPrompt)
    
    	// if we got no reply, cancel
    	if (!answer || answer.length == 0) {
    		return false
    	}
    
    	switch(String(p.fieldValues["result"])) {
    	case "Replace Selection":
    		editor.setSelectedText(answer)
    		editor.setSelectedRange(st, answer.length)
    		break
    	case "Append to Selection":
    		editor.setSelectedText(`${selection}
    
    ${answer}
    `)
    		editor.setSelectedRange(st + selection.length + 2, answer.length)
    		break
    	default: // clipboard
    		app.setClipboard(answer)
    		break
    	}
    	return true
    }
    
    if (!f()) {
    	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.