detect string language

Hi guys,

I want to the detect of a string and I found this ObjC code:

NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
[spellChecker setAutomaticallyIdentifiesLanguages:YES];
NSString *spellCheckText = @"Guten Herr Mustermann. Dies ist ein deutscher Text. Bitte löschen Sie diesen nicht.";

[spellChecker requestCheckingOfString:spellCheckText
    range:(NSRange){0, [spellCheckText length]}
    types:NSTextCheckingTypeOrthography
    options:nil
    inSpellDocumentWithTag:0
    completionHandler:^(NSInteger sequenceNumber, NSArray *results, NSOrthography *orthography, NSInteger wordCount) {
        NSLog(@"dominant language = %@", orthography.dominantLanguage);
}];

Could you please help me in translating it to ASOC? I managed to to do this:

set SpellChecker to current application's NSSpellChecker's sharedSpellChecker
        SpellChecker's setAutomaticallyIdentifiesLanguages_(true)
        set spellCheckText to NSString's stringWithString_("This is a trial")
        set textrange to {0, (spellCheckText's |length|)}

but I got no idea how to translate the requestCheckingOfString method? Could you please give me some help?

Thank you advance.

Pier

Hi

you cannot translate the requestCheckingOfString. method because it uses a Objective-C block which is not supported in AppleScriptObjC. But there is also a synchronous method which can be translated


set spellChecker to current application's NSSpellChecker's sharedSpellChecker()
spellChecker's setAutomaticallyIdentifiesLanguages:true
set spellCheckText to "Guten Herr Mustermann. Dies ist ein deutscher Text. Bitte löschen Sie diesen nicht."
set textrange to {0, count spellCheckText}
set {theResult, theOrthography, theWordCount} to spellChecker's checkString:spellCheckText range:textrange |types|:(current application's NSTextCheckingTypeOrthography) options:(missing value) inSpellDocumentWithTag:0 orthography:(reference) wordCount:(reference)
log (theOrthography's dominantLanguage() as text)

1 Like

Thank you so much Stefan. It works but I got one question. What’s reference used for? And if it used to refer to the variables I want to set, where is the one for theResult? Thank you.

Pier

the method returns an array of NSTextCheckingResult objects which is represented by theResult variable.
Furthermore there are two return by-reference parameters, orthography and wordCount.
In AppleScriptObjC the references are returned with the regular result as a list