Skip to content

Instantly share code, notes, and snippets.

@khurshid-alam
Created March 13, 2025 21:16
Show Gist options
  • Save khurshid-alam/56663bbbde0c31e78f08ba05a02d5bd3 to your computer and use it in GitHub Desktop.
Save khurshid-alam/56663bbbde0c31e78f08ba05a02d5bd3 to your computer and use it in GitHub Desktop.
Rewrite with AI from clipboard. Source: https://tinyurl.com/223rkcsx
-- Source : https://tinyurl.com/223rkcsx
-- set openAI api key
set apiKey to {OPENAI_API_KEY} -- replace key
-- Get copied text from clipboard
set inputText to (the clipboard)
-- Validate if clipboard is empty
if inputText is "" then
display dialog "Clipboard is empty. Please copy some text first." buttons {"OK"}
return
end if
-- Construct user prompt
set query to "Rephrase the following text: " & inputText
-- Construct JSON payload
set jsonPayload to "{\"model\": \"gpt-4-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"" & query & "\"}], \"temperature\": 0.7}"
-- cURL command
set curlCommand to "curl -s https://api.openai.com/v1/chat/completions -H 'Content-Type: application/json' -H 'Authorization: Bearer " & apiKey & "' -d " & quoted form of jsonPayload
-- Execute cURL request
try
set response to do shell script curlCommand
on error errMsg
display dialog "Error in API call: " & errMsg buttons {"OK"}
return
end try
-- Ensure jq is installed and accessible
set jqPath to "/opt/homebrew/bin/jq"
try
set extractedText to do shell script "python3 -c 'import sys, json; print(json.loads(sys.stdin.read())[\"choices\"][0][\"message\"][\"content\"])' <<< " & quoted form of response
on error errMsg
display dialog "Error extracting content: " & errMsg buttons {"OK"}
return
end try
-- Copy the response back to the clipboard
set the clipboard to extractedText
display notification "Rephrased text copied to clipboard!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment