Formatting numbers (again)

I think this is the third time this has come up.

I need to take a number like this:
65432.10

and format it like this:
#,###.##
5,5432.10

First time I asked the answer was the SATI Osax/app.

That’s not working for me.

Second time the answer was Has’ Number script library.

That seemed to work but has suddenly stopped working with a not helpful error message;


Error number: -1708``

So, before reinvent the wheel, is there a reliable way to format numbers via appleScript (or ASObjC)?

Seems to work here:

use script "Number"

format number 6.54321E+4 using "#,###.##"

--> "65,432.1"

Send us your code and I’ll test it.

Hi Ed,

Try this:

use framework "Foundation"
use framework "AppKit"
use scripting additions

my formatNumber:6.54321E+4 byPattern:"#,##0.00" --> 65,432.10
my formatNumber:6.54321E+4 byPattern:"#,###.##" --> 65,432.1
my formatNumber:"65432,1044" byPattern:"#,##0.00" --> 65,432.10

on formatNumber:theNumber byPattern:thePattern
	if class of theNumber = text then set theNumber to theNumber as real
	set theFormatter to current application's NSNumberFormatter's new()
	theFormatter's setFormat:thePattern
	theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US")
	set theResult to theFormatter's stringFromNumber:theNumber
	return theResult
end formatNumber:byPattern:

For some reason this has stopped working.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "Number" version "1.0"
set dowClosing to 3.456789E+4
try
   set dowClosingText to format number dowClosing ¬
      using "#,###.##"
on error errText number errNum
   set errorString to "Error Message: " & errText & return & return & "Error number: " & errNum as text
   set buttonList to {¬
      ("Copy"), ¬
      ("Okay") ¬
         }
   set errDialogReply to display dialog errorString ¬
      with title ("Error: " & errNum) ¬
      default answer (errNum as text) & ": " & errorString ¬
      buttons buttonList
   if button returned of errDialogReply contains "Copy" then
      set the clipboard to errorString
   end if
end try

→ Script Debugger 8.0.4 (8A52)
→ Mac OS 11.6.5 (20G527)

Thank you, Jonah, that works just fine.

One question, it doesn’t seem to need both

use framework "Foundation"
use framework "AppKit"

It works with either one. Is one better than the other? (The script has Foundation already and I hate adding dependencies if not needed)

It’s an old habit.
AppKit is for building user interface.

Works here (12.4). Have you changed your Script Libraries folder? Check the TypeSupport library is present. If you run it without the try block, what’s the error?

Jonah, FYI, I made one tweak:

Illustrator was choking on the NS string format.

It seems to ignore the format’s dictate that it returns two decimal places, which AppleScript wouldn’t allow if the second decimal place was a zero, but as a string, one would expect the trailing zero to be present.

Try
"#,###.00"

That could be, it it required for the Numbers library?

Yes. TypeSupport is used by all libraries to ensure that parameters are the correct type, and provide descriptive error messages if not.

(While AS handler parameters can be directly annotated with types, AS doesn’t tell you which handler or parameter failed which is next to useless; hence the extra library to do the job right.)