I'm currently experimenting with improving my texts using AI tools and it seems to have already become a common practice. Whether it was to correct spelling or refine the tone of my writing, I found myself repeatedly jumping to these AI platforms. It was efficient, but it felt a little tiring to do it every time.
So, I decided to streamline this process and make it a bit more personal and integrated. Instead of going to an external site each time I needed text improvements, I thought, "Why not create an automation that does this on my system?"
The Concept
With the Gemini language model API from Google, I had a powerful tool at my disposal. I created a macOS automation using AppleScript to tap into this API and integrate it directly into my workflow. Here’s how I did it:
🛠️ How to Set Up Your Own Text Improvement Automation
1. Create the Automation with Automator
- Open Automator on macOS.
- Select "Workflow".
- Set "The workflow receives current:" to "text" and "in:" to "any application".
- In the sidebar, under Library, search for and drag the "Run AppleScript" block into the workflow.
- Copy the AppleScript code from GitHub repository and paste it into the AppleScript block in Automator or simply copy the code below.
on run {input, parameters}
set selectedText to input as string
set prompt to "Aprimore a escrita, ortografia e formalidade do seguinte texto a seguir. Seu retorno deve ser SOMENTE o texto ajustado sem conteúdo antes ou depois.\n"
set apiKey to "<YOUR-GEMINI-API-KEY-HERE>"
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"
set jsonResponse to do shell script curlCommand
set textResponse to do shell script "echo " & quoted form of jsonResponse & " | sed 's/^\\\"//; s/\\\"$//; s/\\\\n/\\n/g'"
set creditMessage to "🛠️ Developed by https://github.com/ErnaneJ"
set fullMessage to textResponse & return & return & creditMessage
set dialogResponse to display dialog fullMessage buttons {"Copy", "Close"} default button "Close" with title "🤖 Result - Gemini (1.5 flash)"
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 textResponse
end run
-
Modify the code:
-
Prompt: Adjust the
prompt
variable to specify how you want the AI to process the text (e.g., level of formality or specific instructions). -
API Key: Replace
"<YOUR-GEMINI-API-KEY-HERE>"
with your own Gemini API key (you can get one here).
-
Prompt: Adjust the
2. Install Dependencies
The script uses curl
and jq
for making requests and processing responses. Install them via Homebrew:
brew install curl jq
3. Set the jq
Path
Find the path to the jq
binary with:
which jq
Update the jq
path in the script if necessary. Homebrew usually installs it to:
/opt/homebrew/bin/jq
4. Save and Run the Automation
- Save the automation with the name "Improve Text With Gemini".
- To use it, select text in any application with text field, go to the "Services" menu, and click "Improve Text With Gemini" or whatever name you gave to the file.
- A popup will appear with the enhanced text, and you can copy it directly to your clipboard.
🔍 Example of the Popup
💡 Customization Tips
You can customize the script’s behavior by adjusting the prompt
variable in the AppleScript:
set prompt to "......."
And make sure to replace <YOUR-GEMINI-API-KEY-HERE>
with your actual API key.
🚀 Final Thoughts
Creating this automation was interesting because I had never done anything like this before. It simplified my writing process, saved time, and made everything a little more efficient. If you often use text enhancement tools, consider how you can integrate them into your workflow. Sometimes it's easier than you think!
Do you use similar tools? Share your experiences in the comments below! Feel free to check out the GitHub repository for more code details or suggestions for improvements.
Happy automation! 🤖
Top comments (3)
Hi there,
Thanks for the script. It works great until there are single or double quotes. Is there any easy way to escape them? Thanks in advance.
Hi! I’m glad the script has been helpful for you. To fix the issue with quotation marks, one solution is to add a function to escape them before sending the selected text to the
curl
command. Something like this:Of course, this function should be implemented beforehand, similar to how it was done in
textResponse
. Alternatively, you could also look for an inline solution if you prefer. Either way, this adjustment will automatically escape double quotes so thatcurl
can process the text correctly.I realize now that I focused on handling
textResponse
but didn’t correctly handle theselectedText
provided by the user. Thanks for pointing that out!Great! Thank you! This pointed me in the right direction. If anyone else needs it here you go.
The function for replacing the text via apple script goes above the 'on run' first line of above code.
Then place this line after the 'set prompt to...' line