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 (3)

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.

Collapse
 
ernanej profile image
Ernane Ferreira

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:

set selectedText to replaceText("\"", "\\\"", selectedText)
Enter fullscreen mode Exit fullscreen mode

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 that curl can process the text correctly.

I realize now that I focused on handling textResponse but didnโ€™t correctly handle the selectedText provided by the user. Thanks for pointing that out!

Collapse
 
josh_beers_9e96357725cd18 profile image
Josh Beers

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.

on findAndReplaceInText(theText, theSearchString, theReplacementString)
    set AppleScript's text item delimiters to theSearchString
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to theReplacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end findAndReplaceInText
Enter fullscreen mode Exit fullscreen mode

Then place this line after the 'set prompt to...' line

set selectedText to findAndReplaceInText(selectedText, "\"", "\\\"")
Enter fullscreen mode Exit fullscreen mode