Get decomposed form of string

I do most of my coding in Xojo. There I have the following code to get the decomposed form of a string:

const kCFStringNormalizationFormD  = 0 // Canonical Decomposition
dim s as CFStringMBS = NewCFStringMBS("é")
dim m as CFMutableStringMBS = s.Normalize(kCFStringNormalizationFormD)

MsgBox str(s.Len)+" "+str(m.len)

How do I do the same in AppleScript? I can make a MutableString:

use framework "Foundation"
set theApp to current application
set theNSString to theApp's NSMutableString's stringWithString:"è"

When I try the following code I get an error:

set theNSString to theNSString's Normalize:0

Your error message is because the ‘NSString’ class does not have a ‘Normalize’ function.

If you look at the Developer Documentation for the ‘NSString’ class, you will find there are four functions for Normalizing NSString, depending on the form you require.
There is NFD, NFKD, NFC, and NFKC normalization formulas available.

I see you are also using MBS PlugIn’s for your Xojo code, you obviously won’t be able to use those in AppleScript.
Guessing by your constants name ‘kCFStringNormalizationFormD’, I’m assuming you require NFD normalization, so you would use the ‘decomposedStringWithCanonicalMapping’ NSString function as my example below.


use scripting additions
use framework "Foundation"

set theApp to a reference to current application

set theNSString to theApp's NSString's stringWithString:"è"
set theMutableNSString to theApp's NSMutableString's stringWithString:(theNSString's decomposedStringWithCanonicalMapping())

display dialog (theNSString's |length|() as text) & " " & (theMutableNSString's |length|() as text)

The above AppleScriptObjC code is about as close as you will get to your Xojo code example.

Regards Mark

Perfect! Thanks.