0

What am I doing wrong? I don't get this notification. I have this function:

 @objc func onAutocorrection (_ notification: Foundation.Notification) {
      Swift.print("\(notification)")
 }

later in the same class I do use it as follows:

NotificationCenter.default.addObserver(
    self, 
    selector: #selector(onAutocorrection(_:)), 
    name: NSSpellChecker.didChangeAutomaticCapitalizationNotification,
    object: nil)

The addObserver is executed, but the function is never called even when the application is capitalising in an NSTextView.

Why? Many thanks in advance!

Wizard of Kneup
  • 1,863
  • 1
  • 18
  • 35
  • What do you do to trigger the notification? – Willeke Nov 24 '18 at 01:09
  • I type in a NSTextView. I start with lower caps glibberish, I get the little popover suggesting the same characters, but starting capitalist. I press space. I tried as well a few of the other “related” notifications, but onAutocorrection is never called. – Wizard of Kneup Nov 24 '18 at 03:42
  • I found this related question: https://stackoverflow.com/questions/6971396/notification-of-autocorrect – Wizard of Kneup Nov 24 '18 at 13:47
  • 1
    In the header, `NSSpellCheckerDidChangeAutomaticCapitalizationNotification` is described as "These notifications are made available via the default notification center when the global user preference settings mentioned above are changed.". Mentioned above are `isAutomaticCapitalizationEnabled` and its siblings. – Willeke Nov 24 '18 at 22:42
  • Is that an internal header you are referring to? I cannot see this comment. In any case, thank you Willeke. It seems that I simply misunderstood the meaning of the notification. Looks like the delegate function I used in my answer is indeed the right way to detect autocapitalise. It does work but I am not sure that I didn't affect some internals. Return key seems to work different, but I cannot reproduce it. – Wizard of Kneup Nov 25 '18 at 04:42
  • The header is NSSpellChecker.h. Paste `NSSpellCheckerDidChangeAutomaticCapitalizationNotification` somewhere in the code and Command click (Xcode 9). – Willeke Nov 25 '18 at 19:46

1 Answers1

1

It looks like I misunderstood the notification. It is not meant to be triggered when automatic capitalisation happens but when the systems preference of your Mac is changing. See the comment of ever helpful Willeke and see Notification of autocorrect

In order to get to the intended result of reacting to autocapitalisation did I implement this function in the NSTextViewDelegate:

public func textView(_ view: NSTextView, didCheckTextIn range: NSRange, types checkingTypes: NSTextCheckingTypes, options: [NSSpellChecker.OptionKey : Any] = [:], results: [NSTextCheckingResult], orthography: NSOrthography, wordCount: Int) -> [NSTextCheckingResult] {
        if !range.contains(0){
            return results
        }
        var newResult = [NSTextCheckingResult]()
        for result in results {
            if let textToChange = view.string[range].components(separatedBy: " ").first, let replacement = result.replacementString?.components(separatedBy: " ").first {
                let firstLetterCap = textToChange.capitalizingFirstLetter()
                if replacement == firstLetterCap { 
                    continue //don't add to results
                }
            }
            newResult.append(result)
        }
        return newResult
    }

This function will prevent that the first character will be capitalised.

Ultimately, I check whether the capitalised version of the first word of the range that must include position "0" is equal to the first word of the replacement string. And if it is then I remove that result/suggestion from the result list.

Wizard of Kneup
  • 1,863
  • 1
  • 18
  • 35