文本文件中每三行的Applescript列表
假设我有这样一个文本文件:文本文件中每三行的Applescript列表,applescript,Applescript,假设我有这样一个文本文件: apples pencils juice apricots John Doe the string system is amazing 我希望为文本文件中的每3项创建一个列表,如下所示(当然是字符串): 但是,文本文件将包含6行以上的行和行的不同文本,因此它不能像以下那样具体: set line1 of myfile to item 1 of mylist set line2 of myfile to item 2 of mylist set line3 of
apples
pencils
juice
apricots
John Doe
the string system is amazing
我希望为文本文件中的每3项创建一个列表,如下所示(当然是字符串):
但是,文本文件将包含6行以上的行和行的不同文本,因此它不能像以下那样具体:
set line1 of myfile to item 1 of mylist
set line2 of myfile to item 2 of mylist
set line3 of myfile to item 3 of mylist
set line4 of myfile to item 1 of mySecondlist
set line5 of myfile to item 2 of mySecondlist
set line6 of myfile to item 3 of mySecondlist
试试这个。代码应该是不言自明的,但如果需要帮助,请提问
-- get the text from the file
set theFile to choose file
set theText to read theFile
-- turn the text into a list so we can loop over it
set theTextList to paragraphs of theText
-- put every 3 items into a list and add it to the finalList variable
set listCount to count of theTextList
set finalList to {}
repeat with i from 1 to listCount by 3
if (i + 2) is not greater than listCount then
set end of finalList to items i thru (i + 2) of theTextList
else
set end of finalList to items i thru listCount of theTextList
end if
end repeat
-- show the list of lists result
return finalList
如果您的文件包含您显示的值,则结果如下
{{"apples", "pencils", "juice"}, {"apricots", "John Doe", "the string system is amazing"}}
试试这个。代码应该是不言自明的,但如果需要帮助,请提问
-- get the text from the file
set theFile to choose file
set theText to read theFile
-- turn the text into a list so we can loop over it
set theTextList to paragraphs of theText
-- put every 3 items into a list and add it to the finalList variable
set listCount to count of theTextList
set finalList to {}
repeat with i from 1 to listCount by 3
if (i + 2) is not greater than listCount then
set end of finalList to items i thru (i + 2) of theTextList
else
set end of finalList to items i thru listCount of theTextList
end if
end repeat
-- show the list of lists result
return finalList
如果您的文件包含您显示的值,则结果如下
{{"apples", "pencils", "juice"}, {"apricots", "John Doe", "the string system is amazing"}}