1

I'm having the craziest time trying to get a thousands separator (comma) into my numbers using Applescript. I found two subroutines on the net that supposedly will do the trick, but nothing seems to be going right.

It's giving me all sorts of random formatting. I tried changing up from integer, to real, to small integer, to string, etc. Nothing seems to help.

What I'm passing:

set perCapita2014 to avgHHinc2014 / avgPopHH2014 as integer -- results 17005

The call:

set finalPerCapita2014 to (comma_delimit(perCapita2014)) -- results 0.,0.5

Subroutines: (apparently it requires both of these)

on number_to_string(this_number)
set this_number to this_number as string
if this_number contains "E+" then
    set x to the offset of "." in this_number
    set y to the offset of "+" in this_number
    set z to the offset of "E" in this_number
    set the decimal_adjust to characters (y - (length of this_number)) thru ¬
        -1 of this_number as string as number
    if x is not 0 then
        set the first_part to characters 1 thru (x - 1) of this_number as string
    else
        set the first_part to ""
    end if
    set the second_part to characters (x + 1) thru (z - 1) of this_number as string
    set the converted_number to the first_part
    repeat with i from 1 to the decimal_adjust
        try
            set the converted_number to ¬
                the converted_number & character i of the second_part
        on error
            set the converted_number to the converted_number & "0"
        end try
    end repeat
    return the converted_number
else
    return this_number
end if
end number_to_string

on comma_delimit(this_number)
    set this_number to this_number as string
    if this_number contains "E" then set this_number to number_to_text(this_number)
    set the num_length to the length of this_number
    set the this_number to (the reverse of every character of this_number) as string
    set the new_num to ""
    repeat with i from 1 to the num_length
        if i is the num_length or (i mod 3) is not 0 then
            set the new_num to (character i of this_number & the new_num) as string
        else
            set the new_num to ("," & character i of this_number & the new_num) as string
        end if
    end repeat
    return the new_num
end comma_delimit
4

2 回答 2

1

最简单的事情是使用 do shell 脚本使用复杂的 perl 调用:

set perCapita2014 to (avgHHinc2014 / avgPopHH2014) as integer
set finalPerCapita2014 to do shell script "echo " & perCapita2014 & " | perl -lpe'1 while s/^([-+]?\\d+)(\\d{3})/$1,$2/'"

Applescript 不适合这样的文本处理。(我第一次尝试使用 printF,但没有为 do shell 脚本调用设置语言环境。)

于 2014-08-05T19:22:27.577 回答
0

您可以使用 PHP 以更简单的方式执行此操作,因为 PHP 具有用于将逗号添加到数字的内置函数。

on comma_delimit(theNumber)
    set thePHPScript to "echo number_format(" & theNumber & ");"
    set theShellScript to "php -r" & space & quote & thePHPScript & quote
    set theNumberTextWithCommas to do shell script theShellScript
    return theNumberTextWithCommas
end comma_delimit

一般来说,您不应该在 AppleScript 本身中进行任何真正的处理,因为它不是为此而设计的。它是为应用程序和用户之间的消息传递而设计的——用于向应用程序或用户发送输入并取回输出并将其用作下一个应用程序或用户的输入。基本上与 bash 管道相同,但在 GUI 中。

因此,AppleScript 不擅长处理文本的原因是 AppleScript 擅长向 BBEdit 或 Perl 等应用程序发送消息以使它们处理文本。而且 AppleScript 不擅长处理图像,因为 AppleScript 擅长向 Photoshop 或 Image Events 等应用程序发送消息以使它们处理图像。一个理想的 AppleScript 只是一堆通过“告诉应用程序”块、“执行 shell 脚本”命令、“执行 javascript”命令以及对于用户:“显示对话框”或“选择”在应用程序和用户之间来回传递的消息从列表”或“选择文件”消息。

于 2014-08-06T21:38:25.770 回答