I found a Shane Stanley's handler designed to write numbers in words.
It works well but it suffers of an oddity introduced by Apple engineers which decided - what a surprise - that they are the only ones knowing how numbers are written in french.
which is wrong.
As I'm quite sure that getting a corrected code from Apple would require years, I decided to write customised handlers.
use framework "Foundation"
my convertNumberToWords(9876.45)
log result
my convertNumberToWords(98765432)
log result
my convertNumberToLocaleWords:(9876.45) forLocale:"en"
log result
my convertNumberToLocaleWords:(98765432) forLocale:"en"
log result
my convertNumberToLocaleWords:(9876.45) forLocale:"fr"
log result
my convertNumberToLocaleWords:(98765432) forLocale:"fr"
log result
on convertNumberToWords(thisNumber)
# I guess that it's not the best way to grab which locale is used
tell current application's NSNumberFormatter
set midText to localizedStringFromNumber_numberStyle_(1.1, current application's NSNumberFormatterSpellOutStyle) as text
end tell
if midText is "un virgule un" then # we are speaking french
set strNumber to thisNumber as text
if strNumber contains "," then
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {","}
set theParts to text items of strNumber
set AppleScript's text item delimiters to saveTID
tell current application's NSNumberFormatter
set begText to localizedStringFromNumber_numberStyle_((theParts's item 1) as integer, current application's NSNumberFormatterSpellOutStyle)
set endText to localizedStringFromNumber_numberStyle_((theParts's item 2) as integer, current application's NSNumberFormatterSpellOutStyle)
end tell
return (begText as string) & " virgule " & endText
else
tell current application's NSNumberFormatter to set resultingText to localizedStringFromNumber_numberStyle_(thisNumber, current application's NSNumberFormatterSpellOutStyle)
return (resultingText as text)
end if
else
tell current application's NSNumberFormatter to set resultingText to localizedStringFromNumber_numberStyle_(thisNumber, current application's NSNumberFormatterSpellOutStyle)
return (resultingText as text)
end if
end convertNumberToWords
on convertNumberToLocaleWords:thisNumber forLocale:localeString
set theFormatter to current application's NSNumberFormatter's new()
theFormatter's setNumberStyle:(current application's NSNumberFormatterSpellOutStyle)
set theLocale to current application's NSLocale's localeWithLocaleIdentifier:localeString
theFormatter's setLocale:theLocale
if localeString = "fr" then
# Grab the local decimal separator
set deciDelim to item 2 of (0.2 as text) # I'm not sure that we may assume that it's the comma
# Convert the number into text
set strNumber to thisNumber as text
if strNumber contains deciDelim then
# Split the string on the decimal delimiter
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {deciDelim}
set theParts to text items of strNumber
set AppleScript's text item delimiters to saveTID
set begText to theFormatter's stringFromNumber:((theParts's item 1) as integer)
set endText to theFormatter's stringFromNumber:((theParts's item 2) as integer)
return (begText as string) & " virgule " & endText
else
set resultingText to theFormatter's stringFromNumber:thisNumber
return (resultingText as text)
end if
else
set resultingText to theFormatter's stringFromNumber:thisNumber
return (resultingText as text)
end if
end convertNumberToLocaleWords:forLocale:
I know that both handlers apply only to numbers which AppleScript is able to display without using the scientific format.
Alas, I'm not at ease with the help pages of xCode and given that, I found no clean way to get the current localeIdentifier.
I decided to use a workaround which is far from pretty but it does the job.
Of course, if somebody is fair enough to build better handlers, I'm all ears.