AppleScript and JS to make openai call form clipboard.

I try to use the clipboard content to make an openai call

I set the clipboard content to {{clip}} with AppleScript

Then I make the JS function call

And again use the result and AppleScript to push to clipboard

But it is not working.

Can you help me ?

Or all in AppleScript ?

Thank you

When I run, I see that something happens but result is empty

{/AppleScript set clip to {{clip}} set clip to {/JavaScript async function beautifyEmailContent(clip) {     const apiKey = 'YOUR_OPENAI_API_KEY';      const apiUrl = 'https://api.openai.com/v1/chat/completions';     const prompt = Please beautify the following text to make it suitable for an email:\n\n${clip}`;

const data = {
    model: "gpt-4",
    messages: [
        { role: "system", content: "You are an assistant that formats and beautifies text for emails." },
        { role: "user", content: prompt }
    ],
    max_tokens: 150
};

try {
    const response = await fetch(apiUrl, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${apiKey}`
        },
        body: JSON.stringify(data)
    });

    const result = await response.json();
    const beautifiedText = result.choices[0].message.content;
    
    console.log("Beautified Email Content:", beautifiedText);
} catch (error) {
    console.error("Error:", error);
}

}

const clipboardContent = "{{clip}}"; beautifyEmailContent(clipboardContent); } set the clipboard to {text:(clip as string)}`