Tuesday, September 27, 2022

#1 2022-08-10 08:58:08 am

bwill
Member
Registered: 2019-07-17
Posts: 33

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:

Applescript:

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:

Applescript:

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:

Applescript:

set theNSString to theNSString's Normalize:0

error "-[__NSCFString Normalize:]: unrecognized selector sent to instance 0x600000107ed0" number -10000

Offline

 

#2 2022-08-10 10:04:59 am

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 163

Re: Get decomposed form of string

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.

Applescript:


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

Last edited by Mark FX (2022-08-10 11:57:37 am)

Offline

 

#3 2022-08-10 10:10:21 am

bwill
Member
Registered: 2019-07-17
Posts: 33

Re: Get decomposed form of string

Perfect! Thanks.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)