DEV Community

Cover image for Text Improvement Automation with AppleScript & Gemini API πŸ”§
Ernane Ferreira
Ernane Ferreira

Posted on

Text Improvement Automation with AppleScript & Gemini API πŸ”§

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

  1. Open Automator on macOS.
  2. Select "Workflow".
  3. Set "The workflow receives current:" to "text" and "in:" to "any application".
  4. In the sidebar, under Library, search for and drag the "Run AppleScript" block into the workflow.
  5. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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).

2. Install Dependencies

The script uses curl and jq for making requests and processing responses. Install them via Homebrew:

brew install curl jq
Enter fullscreen mode Exit fullscreen mode

3. Set the jq Path

Find the path to the jq binary with:

which jq
Enter fullscreen mode Exit fullscreen mode

Update the jq path in the script if necessary. Homebrew usually installs it to:

/opt/homebrew/bin/jq
Enter fullscreen mode Exit fullscreen mode

4. Save and Run the Automation

  1. Save the automation with the name "Improve Text With Gemini".
  2. 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.
  3. A popup will appear with the enhanced text, and you can copy it directly to your clipboard.

πŸ” Example of the Popup

generated popup

πŸ’‘ Customization Tips

You can customize the script’s behavior by adjusting the prompt variable in the AppleScript:

set prompt to "......."
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
josh_beers_9e96357725cd18 profile image
Josh Beers

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.