Action

ChatGPT: Modify Selection

Posted by agiletortoise, Last update 5 months ago

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 perform that instruction on the selected text, replacing the selection with the result.

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

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

Steps

  • script

    // 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 ChatGPT to do with the selected text?"
    	p.addTextView("instruction", "ChatGPT Prompt", "")
    	p.addLabel("caption", "Provide an instruction to transform the selected text in the draft, for example `capitalize`, `translate into spanish`")
    	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 = new OpenAI()
    	let answer = ai.quickChatResponse(chatPrompt)
    
    	// if we got no reply, cancel
    	if (!answer || answer.length == 0) {
    		return false
    	}
    	
    	editor.setSelectedText(answer)
    	editor.setSelectedRange(st, answer.length)
    	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.