According to Shane’s book," AppleScriptObjC Explored", you can’t use stringWithFormat_ in ASOC. You’ll have to use an NSNumberFormatter to do what you want.
the code looks ok, but the format specifiers you are using are a bit strange looking… what do you wish to obtain exactly?
If you look at the “String Format Specifiers” in the document none of the available specifiers are used in your code. Are you sure you want this kind of formatter? If you need a number formatter, this is an example:
set number_formatter to current application's NSNumberFormatter's alloc()'s init()
tell number_formatter
setFormat_("0.####")
setRoundingMode_(7)
set theRoundedValueAsString to stringFromNumber_(1.2345678) --returns "1.2346"
set theRoundedValueAsReal to numberFromString_("1.2345678") as real --returns 1.2346
end tell
Would this be what you are looking for maybe?
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)
That solves it. Well, number formatters and Date formatters work fine. If you need more info on either, please specify what you want the result to be, it will help.
Fred
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)
According to the documentation, you can use this method to get a formatted string with a printf c-style (see string format specifiers in the xcode documentation).
I have been using this type of formatting with the the do shell script "printf … way with success.
The problem is inherent in the way the scripting bridge works. When you pass an integer or a real to a method, the bridge automatically converts it to an NSNumber object – they aren’t seen as integers or floats by Cocoa, so you can’t use the various tokens like %d and %f.
Everything from AppleScript gets turned into an object of some kind, so the only token you can use is %@. And that doesn’t give you a lot of options.
Thanks Shane,
I indeed started using the number formatters.
I also wrote a simplified function to use the printf style in some cases:
For the interested people:
Comments are welcome
on printf_value_(formatstr, theValue)
try
set formatstr to formatstr as string
if formatstr contains "i" or formatstr contains "u" or formatstr contains "x" then
set theValue to theValue as integer
else if formatstr contains "f" or formatstr contains "e" or formatstr contains "g" or formatstr contains "a" then
set theValue to theValue as real
else if formatstr contains "s" then
set theValue to quoted form of (theValue as string)
end if
set formattedNumber to do shell script "printf " & formatstr & " " & theValue
return formattedNumber as string
on error errmsg
return missing value
end try
end printf_value_
I don’t quite understand what you’re trying to do with this method. If you want to use printf type formatting, why not just use “do shell script” directly. What does the rest of this code do for you?
I understand your comment, but such a mehod can be placed in a library and called in a structured way in different projects.
If changes have to be done, you only need to make the changes in one place.