Because Grammarly is just terrible even the pro version. Toolstack - Powershell and Autohotkey.

DIYing a grammar checker with ChatGPT, Powershell and Autohotkey

My grammar is horrible. Well, not horrible just lazy, to be honest. I just tend to forget about writing small syllable words from time to time, like ’to’, ‘on’, ’the’ etc. If English is your second language, you know what I am talking about. In my head, I can phrase things correctly, but when I am writing, I tend to write in keywords, if that makes any sense.

The plan is to replicate the Grammarly experience to some extent. Grammarly checks writing in real time. Implementing this feature is almost impossible for me, and fortunately, I find it distracting. I just find Grammarly to be distracting with their forced positivity expressions and lacking in quality compared to ChatGPT. PERIOD. Therefore, the plan is to:"

  • Select a section of text
  • Press a button
  • Get ChatGPT to fix it
  • Copy the correct text
  • And at the end of it tell me what went wrong

Autohotkey script

Code first

F8::{
    input_prompt := A_Clipboard
    A_Clipboard := ""  ; Start off empty to allow ClipWait to detect when the text has arrived.
    Send "^c"
    ClipWait  ; Wait for the clipboard to contain text.

    TrayTip "Grammar Check Triggered", "Grammar check command recieved please wait..."
    Sleep 3000   ; Let it display for 3 seconds.
    HideTrayTip

    RunWait A_ComSpec ' /c cd "<path_to_powershell_script>" & powershell.exe .\test.ps1'
    MsgBox input_prompt "`n"`n"" A_Clipboard

    ; Copy this function into your script to use it.
    HideTrayTip() {
        TrayTip  ; Attempt to hide it the normal way.
        if SubStr(A_OSVersion,1,3) = "10." {
            A_IconHidden := true
            Sleep 200  ; It may be necessary to adjust this sleep.
            A_IconHidden := false
        }
    }
}

Breakdown:

  • A_Clipboard ⇒ As per docs, A_Clipboard is a built-in variable that reflects the current contents of the Windows clipboard if those contents can be expressed as text.
  • RunWait A_ComSpec ⇒ Runs my powershell script through commandline.
  • Tray stuff ⇒ Just to let me know the script has been triggered. It is helpful as ChatGPT needs some to time to process and AHK sometimes just doesn’t work. IDK why.

Powershell

Code:

$clipboardContent = Get-Clipboard
$OPENAI_API_KEY = ""
$headers = @{
    "Authorization" = "Bearer $OPENAI_API_KEY"
}

$data = @{
    model = "gpt-3.5-turbo"
    messages = @(
        @{
            role = "user"
            content = "Fix the grammar errors and at the end list out the mistakes made:`n`n \'{0}\'" -f $clipboardContent
        }
    )
}

# Convert the hashtable to JSON
$body = $data | ConvertTo-Json


$response = Invoke-RestMethod "https://api.openai.com/v1/chat/completions" `
    -Method Post `
    -Headers $headers `
    -ContentType "application/json" `
    -Body $body
# $jsonData = $response | ConvertTo-Json
$jsonData = $response.choices[0].message.content

Write-Output $jsonData
Set-Clipboard -Value $jsonData

Breakdown:

  • I initially wrote and tested the code with Python.
  • I used ChatGPT’s documentation and converted the curl command to powershell using this tool.
  • Then I fiddled with ChatGPT to come up with this solution.

So how does it work

It works like this

  • First, select the text and then press F8.
  • After that, my AHK script is triggered.
  • AHK runs ctrl+c to copy the selected text.
  • It stores the selected text in the variable input_prompt.
  • Then the PowerShell script is triggered via cmd.
  • The PowerShell script takes the content from the clipboard.
  • It then calls the ChatGPT API with the prompt: “Fix the grammar errors and at the end list out the mistakes made: {text}”.
  • It then receives the results and stores them to the clipboard.
  • In AHK, we show the input prompt and the result prompt.
  • The clipboard contains the result prompt as well.
  • Then just paste the grammar-corrected statement.

At the end of the day

  • I could have written only a Python script and saved myself from the hassle of dealing with PowerShell.
  • I could have written this in Rust and made it efficient and cross-platf… who am I kidding?
  • I’m glad there is no async stuff.
  • The prompt makes the result too professional, which I don’t like.
  • I am not planning to use this often, except for technical writing, which I do A LOT and surprisingly get paid for.
  • You should be using a cliboard manager like Ditto if you are using this.
  • I will try to only put the “mistakes” section on the messagebox and not copy it. I will also remove the double quotes from the output section.

This thing doesn’t deserve it’s Github Repo, but I will make one anyway.

https://github.com/anyfactor/Chatgpt_grammar_checker

Anyway if you have any feedback let me know. Thanks.