Use Aspell (Homebrew Package) to Spell Check a Word

Howdy folks, I've been trying to learn some how to work with shell scripts lately, so I've been making great use of Homebrew packages to make some basic macros.

My latest one is a simple spell check using Aspell (a Homebrew package). It selects the word to the left of the cursor, runs it through Aspell's cli to verify correct spelling or produce a list (comma delimited) of possible corrections.

If the word is spelled correctly a notification is displayed indicating that. If not, Aspell returns a list of possible matches. Using some simple RegEx, I remove Aspell's version info and then change the comma delimiters to new line feed delimiters and then display the tidied up list in a prompt from list action. This allows you to choose the correct word. Once you select a word, it pastes over the incorrectly spelled word and moves to the end of the line of text.

Attached is a screen recording of it in action (note that the display in a window is only because QuickTime doesn’t grab system notifications, but the actual macro has a system style notification for correctly spelled words).

While the macro works quite well for me (and why I posted it here, and not in the Questions and Suggestions category), I'm still interested in everybody's thoughts and ideas if this macro can be improved. Thanks in advance, and if this macro proves useful to anybody else then enjoy!

-Chris

NOTE: This macro has special requirements to run

This macro makes use of the following KM version 10 action(s):
Position next engine window
If you are running a previous version of KM, simply disable or remove said action(s).

This macro requires Homebrew to be installed. Click the link for more info.

This macro requires Aspell (a Homebrew package) to be installed. Click the link for more info.
Also see this page for a list of language codes and also see this page for a list of Aspell command options.

This macro requires the end-user to set a ENV_PATH variable (or specify the path in the shell script). Click the link for more info.

CURRENT VERSION: 1.2.1 (Wednesday, May 18, 2022)

v1.2.1: Wednesday, May 18, 2022
Added instructions about changing the triggers.

v1.2.0: Wednesday, February 02, 2022
If you use this in conjunction with Typinator, the spell check prompt list will be positioned next to the Create new Typinator Item window if it is open.

v1.1.2: Friday, January 28, 2022
Added a step to delete past clipboard so as not to lose whatever was previously on the clipboard

v1.1.1: Thursday, January 27, 2022
I added KM10's Set Next Engine Window action to ensure the list appears on the front screen.

v1.1: Sunday, January 23, 2022
I added a few extra actions to allow for different languages based on the %TriggerValue% token. There are obviously many different ways to do this, such as a prompt allowing you to select a language but I only speak two languages so my needs are few :sweat_smile:. See the comments in the macro for a more detailed explanation of spell checking other languages.

v1.0: Friday, January 21, 2022
Initial macro

spell check screen recording

51)Spell check.kmmacros (58 KB)

Macro screenshot (click to expand/collapse)

7 Likes

Very interesting. Thank you for bringing Aspell back to my attention.

In Aspell and Hunspell: A Tale of Two Spell Checkers three example words were given. Hunspell with British dictionary indeed didn't give any correct suggestion.

I'll test with Aspell later.

EDIT: Tested with Aspell. Very impressive algorithm.

1

1 Like

Could you please help me with installing another language pack, like Dutch or German? And how to pass the desired language to Aspell?

You don't need to install anything actually, just use the correct command after aspell -a. You can specify a specific language a couple of different ways. One I just tested was appending -d to the end followed by the two character language code. I tried Spanish using the following: aspell -a -d es and it worked fine from Terminal. and in KM

For Dutch it would be aspell -a -d nl
For German it would be aspell -a -d de

See this page for a list of language codes and also see this page for a list of Aspell command options.

This is another method but I haven't tried it yet:

EDIT: @ALYB I updated the macro in the initial post to v1.1 which has support for English and Spanish and can obviously be modified for your needs in case you wanted to see how I did it.

2 Likes

UPDATE: Version 1.1.1 is up.

The only change is the macro now uses KM10's Set Next Engine Window position to ensure the list appears on the front screen.

See the original post for more info.

Hi @cdthomer, this macro is super cool. Thank you so much for sharing.

One note, I had to modify the Execute a Shell Script action:

Keyboard Maestro Export

PATH=$PATH:/usr/local/bin
aspell -a

This has to do with my installation of HomeBrew. I suspect that other users might encounter the same issue. Note that this change will not effect those that don't need it.

Thanks again!

Thanks Jim! That's high praise coming from you :grin:

Thank you too for the heads up about the path in your copy of the macro. Does setting an ENV_PATH variable not work for you for some reason?

Also, thanks for the PM about the clipboard; I made an adjustment to version 1.1.2 to restore the original system clipboard.

-Chris

Oh, I'm sure it would; however, I try to build macros that I can share and not have worry about the users' configuration.

Thanks again for sharing!

1 Like

Good point. It makes me think I should edit my recent macro posts to make sure end-users are aware of the need to setup an ENV_PATH variable. I assume if they're familiar with Homebrew and installing packages they would already know this... but at the same time I think that was something that tripped me up initially.

Back to the drawing board haha! :laughing:

UPDATE: Version 1.2.0 is up.

If you use this in conjunction with Typinator, the spell check prompt list will be positioned next to the Create new Typinator Item window if it is open.

I don't know what I am doing wrong or is ENV_PATH doesn't work because files in m1 macs are a little different.

I downloaded Aspell from homebrew and its working great in terminal.
I downloaded ENV_PATH macro and run it.

When I run your macro nothing happen no list appear nothing. I also tried @_jims way but didn't work.

I don't have Typinator and I disable this group.

CleanShot 2022-05-18 at 13.32.35

I hope you can help me.

Hi there! I'd be glad to help you troubleshoot the issue. If possible, post your macro here (or at least a screenshot of the entire macro including triggers). I just want to confirm that nothing critical has been changed, perhaps even unknowingly.

Also, could you clarify what you mean by this part?

There's no ENV_PATH macro; there's only this macro. The ENV_PATH is a variable you have to add yourself to ensure this runs properly. It's easy to do, and this page gives more information on how to do it.

If you could get back to me with that info we'll go from there!

-Chris

That's what I meant, sorry. I ran his macro and this what I got.

Photo

Gif

CleanShot 2022-05-18 at 17.43.37

I only disable the Typinator group in the macro nothing else.

That just means you already have a variable called "ENV_PATH".

Go to KM's Preferences, click the Variables tab, find "ENV_PATH" and let us know what it is set to.

Without seeing your macro itself... it looks like maybe you changed the trigger? I see you pressing ⇧⌘C, or something to that affect, but the macro is designed by default to use ⌃⇧S for English and ⌥⇧S for Spanish.

If you changed the triggers can you verify you changed the corresponding lines...

...in this action? (click to expand/collapse)

I just realized I don't mention that in the release notes... i'll update that and repost here shortly.

It's the same ⌃⇧S I also wrote "-spell" to make sure.

photo

02)Homebrew: spell check using aspell.kmmacros (74.7 KB)

Ok great, thanks for verifying that.

So your copy works just fine on my end, which leads me to believe it's an issue with the Homebrew package; perhaps where it's installed.

Do this: open Terminal, type brew info aspell and note the location where it is installed. On mine it is /usr/local/Cellar/aspell/0.60.8. Verify that your ENV_PATH includes the /usr/local/Cellar part or at least /usr/local/ in it. Report back and we'll go from there.

1 Like

Hi @A_A. If I'm tracking your problem correctly, I think this might help...

Open the Terminal application; at the shell prompt type:

where aspell

On my system, the where command returns:

/usr/local/bin/aspell

That will display the path you need. You can use it with the method I suggested or use it with the aforementioned ENV_PATH variable.

1 Like

Hey Jim! Thanks for chiming in, your CLI skills are way beyond mine (as are most if not all of your other skills too haha).

1 Like

Haha, age has some advantages. On a good day we call it experience.

BTW, here's some trivia on the matter: What is the difference between which and where - Unix & Linux Stack Exchange

My system also includes the whereis utility.

1 Like