Ai | paragraph headlines

Posted by zeebee, Last update about 1 month ago

This snippet for the Drafts app utilizes the OpenAI GPT-4 API to generate three sample headlines based on a selected text. When a range of text is selected in the Drafts app and the script is run, it sends a request to the OpenAI API, prompting it to generate three sample headlines for the selected text.

The generated headlines are then automatically inserted in the document directly after the selected text.

This snippet is helpful for generating creative headline ideas for articles, blog posts, essays, or any piece of writing. Note: This script requires an OpenAI API key to function.


  • script

    const textInput = editor.getSelectedText();
    const paragraph = textInput.length ? textInput : editor.getText()
    let ai = new OpenAI()
    const payload = {
          model: "gpt-4",
          messages: [
              "role": "system",
              "content": "you are a creative philosopher technologist. You understand process, people and tools in sphere of digital product creations, software engineering"
              "role": "user",
              "content": `Compress following paragraph into simple, succinct 3-5 words headline. Generate 3 samples.\n\nparagraph : ${paragraph}`
          temperature: 0.8,
          max_tokens: 200,
          top_p: 1,
          frequency_penalty: 0,
          presence_penalty: 0,
    let response = ai.request({ 
    "path": "/chat/completions", 
    "method": "POST", 
    "data": payload })
    // Get the current cursor position
    let cursorPosition = editor.getSelectedRange()[0];
    let aiGenText = `\n${response.responseData.choices[0].message.content}`	
    // Insert the line with the word count after the selected text
    editor.setTextInRange(cursorPosition + paragraph.length, 0, aiGenText);
    // Move the cursor to the end of the newly inserted text
    let newCursorPos = cursorPosition + paragraph.length + aiGenText.length;
    editor.setSelectedRange(newCursorPos, 0);


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