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 (1)
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.