2016-03-24 28 views
0

我想訴諸一堆數字與Applescript。我對這門語言很陌生,我想我會請你幫忙。Applescript度假村堆數字

我有一組數字,看起來像這樣在我的文本編輯文件:

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 "" 

但我現在卡住了,我希望尋求幫助。

+0

您好!如果你可以用縮進或反引號包圍你的代碼示例,這將有所幫助,因此它們以固定寬度的字體顯示,並且更易於編輯和複製/粘貼。歡迎來到堆棧溢出! – Martin

+0

後續問題應該在新問題中提出。請在那裏詢問並在此處刪除更新,您將得到幫助。之後我會刪除此評論。 –

回答

0

要在AppleScript中查找和替換字符串,原生方法是使用text item delimiters。在每行上有用空格(或製表符)分隔的固定數量的值,使用text item delimiters,text 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 
+0

非常感謝!奇蹟般有效!不過我想我遇到了一個小問題。該代碼似乎將所有負值返回爲正值。我相信這是一個簡單的解決方法,但我的技能僅限於檢測和解決問題。任何幫助? –

+0

我已經更新了帖子Järv,它現在使用'text items'而不是'words'並保留負值的前導連字符。 –