DEV Community

Cover image for DYI AI Text "Cleaner"
Adam
Adam

Posted on

DYI AI Text "Cleaner"

I was looking through hacker news an saw this post about some app. The App will take any highlighted text and "enhance" it using chatgpt after the user does some keybinding. And then lower someone said that they just created a script it do it for them and save $$ a month.

So I said screw it, I'll do it myself as well and started chatting with my personal E2 engineer, ChatGPT.

In Action
(I'm clicking a keyboard shortcut to make this generate)

Setting up the bash script

First off if you don't have jq installed make sure you have it.

brew install jq
Enter fullscreen mode Exit fullscreen mode

Create a folder somewhere easy to access and store your script. For the past of this guide it will be ~/ai_text_cleaner.

mkdir ~/ai_text_cleaner
Enter fullscreen mode Exit fullscreen mode

Now download my script to that folder.

wget -O ~/ai_text_cleaner/ai_text_cleaner https://raw.githubusercontent.com/nrgapple/scripts/main/ai_text_cleaner/ai_text_cleaner
Enter fullscreen mode Exit fullscreen mode

The repo is here

Next add a .env file in the directory and add your OpenAI key to it.

OPENAI_API_KEY=[your-key-here]
Enter fullscreen mode Exit fullscreen mode

Last we need to make the script executable.

chmod +x ~/ai_text_cleaner/ai_text_cleaner
Enter fullscreen mode Exit fullscreen mode

Lets test if it works.

~/ai_text_cleaner/ai_text_cleaner "ths is sme text that i wrot poorly and rlly fast to show if you can cln up stuff well."
Enter fullscreen mode Exit fullscreen mode

I get this as a response:

This is some text that I wrote poorly and really fast to see if you can clean up stuff well.
Enter fullscreen mode Exit fullscreen mode

Nice! Lets move on.

Set up Automator on Mac

Run this command to pull down repo, pull out the automator action folder and remove the repo. (I recommend doing this in your ~/Downloads directory.

git clone https://github.com/nrgapple/scripts.git && mv scripts/ai_text_cleaner/automator/AI\ Text\ Clean\ Up.workflow ./ && rm -rf scripts
Enter fullscreen mode Exit fullscreen mode

Open up the file ai_text_cleaner/automator/AI Text Clean Up.workflow/Contents/document.wflow. ctr+f for ai_text_cleaner. You should see a line like so:

<string>$HOME/git/scripts/ai_text_cleaner/ai_text_cleaner "$@"</string>
Enter fullscreen mode Exit fullscreen mode

Change it to where you saved your script from above. If you saved it to where I said it would be:

<string>$HOME/ai_text_cleaner/ai_text_cleaner "$@"</string>
Enter fullscreen mode Exit fullscreen mode

Save and Lets move on.

Copy the whole directory (AI Text Clean Up.workflow) to your services folder in automator:

cp -r AI\ Text\ Clean\ Up.workflow ~/Library/Services/AI\ Text\ Clean\ Up.workflow 
Enter fullscreen mode Exit fullscreen mode

Nice. Now last is to create a keybinding for running it.

Set up the keybindings

Go to Settings on Mac and search for Keyboard. Click Keyboard Shortcuts.

Keyboard Shortcuts Settings

Click Services and look for it under the different categories. Mine was under Text

Action in Keybindings

Double click it and set your keybinding.

Nice. You're ready to test it out!

Test it out!

Open a text document and type some sentence or paragraph. Highlight all the text and do your keybinding. It will take a second or two and bam! You should see your highlighted text be replaced by Chatgpt's enhanced cleaning!

Modify the prompt

If you think what Chatgpt gave you sucked, you can try to change the prompt.

In our bash script from above look for this line:

# Define the prompt in a variable
PROMPT="You are a helpful assistant. Your task is to correct any grammatical errors, improve sentence structure, and fix spelling mistakes in the following text."
Enter fullscreen mode Exit fullscreen mode

You can try and change this to your hearts desire to get better results!

Conclusion

We have created a tool worth $6.90 a month! You'll only have to pay for what you use on OpenAI's api rates.

Let me know if something doesn't work for you and I'll make some edits. Let me know if you would like me to do some more DYI AI tools in the future. I have already adapted with flow to create a command line command generator is well (Can be found in the same repo).

Thanks! 👋

Top comments (1)

Collapse
 
cyi101 profile image
Chris Y.

This is awesome Adam, thanks! But do you know if there's any way to automatically select and replace the previous line with one keyboard shortcut (like Fixkey), instead of having to highlight the text manually? I feel like this is the one thing that would really enable that flow with typing.