Applescript 求助于成堆的数字

Applescript to resort piles of numbers

我正在尝试使用 Applescript 求助于一堆数字。我对这门语言还很陌生,我想我会向你寻求帮助。

我的 TextEdit 文件中有一组数字如下所示:

v 0.186472 0.578063 1.566364
v -0.186472 0.578063 1.566364
v 0.335649 0.578063 1.771483

我需要的是一个脚本来计算这些数字,使其看起来像这样:

(0.186472, 0.578063, 1.566364), 
(-0.186472, 0.578063, 1.566364),
(0.335649, 0.578063, 1.771483),

所以每个数字后面必须有一个逗号,并且总是一行上的三个数字必须放在括号()中。最后,在每个括号中的三个组之后必须有另一个逗号,并且必须删除每行之前的 v

到目前为止,我只是设法摆脱了每一个 "v" 使用:

set stringToFind to "v"
set stringToReplace to ""

但现在我卡住了,希望得到帮助。

要在 AppleScript 中查找和替换字符串,本机方法是使用 text item delimiters。每行有固定数量的值,由空格(或制表符)分隔,使用 text item delimiterstext items 和字符串连接我们可以解决您的问题。

我在字符串的前后添加了换行符,以显示不包含 4 个单词的行将被忽略。

set theString to "
v 0.186472 0.578063 1.566364
v -0.186472 0.578063 1.566364
v 0.335649 0.578063 1.771483
"
set theLines to paragraphs of theString

set oldTIDs to AppleScript's text item delimiters


repeat with i from 1 to count theLines
    set AppleScript's text item delimiters to {space, tab}
    if (count of text items of item i of theLines) = 4 then
        set theNumbers to text items 2 thru -1 of item i of theLines
        set AppleScript's text item delimiters to ", "
        set item i of theLines to "(" & (theNumbers as string) & "),"
    else
        set item i of theLines to missing value
    end if
end repeat

set theLines to text of theLines
set AppleScript's text item delimiters to linefeed
set newString to theLines as string
set AppleScript's text item delimiters to oldTIDs
return newString