Skip to content

Instantly share code, notes, and snippets.

@ErnaneJ
Last active October 2, 2025 21:20
Show Gist options
  • Select an option

  • Save ErnaneJ/f986de2032dcfaff043ee171da11beaa to your computer and use it in GitHub Desktop.

Select an option

Save ErnaneJ/f986de2032dcfaff043ee171da11beaa to your computer and use it in GitHub Desktop.
AppleScript to create a service to improve text with Gemini on macos system ✨
--[[
AppleScript: Text Enhancement with Gemini 1.5 API
Version: 1.0
Developed by: Ernane Ferreira (https://github.com/ErnaneJ)
Description:
This script refines and formalizes selected text by utilizing the Gemini 1.5 Flash model from Google's API.
It integrates spelling, grammar, and stylistic improvements based on a given prompt.
Dependencies:
- curl (for making API requests)
- jq (for JSON parsing)
Note: Ensure that both 'curl' and 'jq' are installed on your system and accessible via the command line.
API Key:
Replace the placeholder <YOUR-GEMINI-API-KEY-HERE> with your actual Gemini API key to enable the script.
--]]
on run {input, parameters}
-- Convert the input to string format
set selectedText to input as string
-- Define the prompt for improving the text
set prompt to "Enhance the writing, spelling, and formality of the following text. Your response should ONLY contain the adjusted text, with no content before or after.\n"
-- Set the API key for Gemini
set apiKey to "<YOUR-GEMINI-API-KEY-HERE>"
-- Construct the curl command for the API request
set curlCommand to "curl -s 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" & apiKey & "' -H 'Content-Type: application/json' -X POST -d '{\"contents\": [{\"parts\": [{\"text\": \"" & prompt & " " & selectedText & "\"}]}]}' | /opt/homebrew/bin/jq .candidates[0].content.parts[0].text"
-- Execute the curl command and get the API response
set jsonResponse to do shell script curlCommand
-- Process the JSON response to extract the relevant content
set textResponse to do shell script "echo " & quoted form of jsonResponse & " | sed 's/^\\\"//; s/\\\"$//; s/\\\\n/\\n/g'"
-- Append credit information to the final output
set creditMessage to "🛠️ Developed by https://github.com/ErnaneJ"
set fullMessage to textResponse & return & return & creditMessage
-- Display the dialog with the formatted response and options
set dialogResponse to display dialog fullMessage buttons {"Copy", "Close"} default button "Close" with title "🤖 Result - Gemini (1.5 flash)"
-- Handle the user's response to the dialog
if button returned of dialogResponse is "Copy" then
set the clipboard to textResponse
display notification "Text copied to clipboard" with title "Copy"
end if
-- Return the formatted response
return textResponse
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment