Freigeben über


Test-Spelling

The Integrated Scripting Environment has many things: A rich debugger, a nifty object model,  and support for multiple runspaces, but it doesn’t have spell check.  Since I end up writing a lot of documentation in the ISE for my functions, I decided to write a quick Spellchecker function.

In order to write this I used some functions from IsePack and some functions from WPK.  Both IsePack and WPK can be found as part of the PowerShellPack

WPF text controls actually all have a spell check built into them, which saves almost all of the trouble of getting this to work.  Unfortunately, it means that the spell check as it is written pops up a small UI for each thing you want to check.

 Import-Module IsePack, WPK            
            
function Test-Spelling {            
    <#
    .Synopsis
        Tests the spelling on some text
    .Description
        Launches a text box with spell check enabled to highlight any spelling errors.
    .Example
        Test-Spelling "Can I speel?"
    .Example
        # Uses IsePack's Add-IseMenu to add a spellcheck hotkey to the Ise
        Add-IseMenu -Name "SpellCheck" -Menu @{
            "Test-Spelling" = {
                Select-CurrentText -NotInOutput | 
                    Where-Object { $_ } | 
                    Test-Spelling
            } | Add-Member NoteProperty ShortcutKey F7 -PassThru
        }
    #>            
    param(            
    # The phrase to check for spelling errors            
    [Parameter(ValueFromPipeline=$true)]            
    $phrase            
    )            
                
    process {            
        New-TextBox -Resource @{Phrase=$phrase} -On_Loaded {            
            $phrase = $this.Resources.Phrase            
            $this.Text = $phrase            
            $this.SpellCheck.IsEnabled = $true            
        } -ASJob            
    }            
}            
            
Add-IseMenu -Name "SpellCheck" -Menu @{            
    "Test-Spelling" = {            
        Select-CurrentText -NotInOutput |             
            Where-Object { $_ } |             
            Test-Spelling            
    } | Add-Member NoteProperty ShortcutKey F7 -PassThru            
}            
            

The script imports IsePack and WPK, writes the test spelling function Test-Spelling (with an example from the command line and an example that adds the menu item), and adds it to the IseMenu all in a scant 42 lines. Go ahead and give it a try. F7 becomes the shortcut key for spell check, just as it is in Word.

Hope this helps,

James Brundage [MSFT]