我是脚本新手,所以我可能不知道如何寻找解决方案;到目前为止我还没有找到答案。可以在循环中更改变量名吗?
我想创建一个循环,为每个列表值创建一个新变量。此代码现在可以正常工作,但是如何将If语句更改为Repeat?
choose from list {"1 Apple", "2 Banana", "3 Cat", "4 Dog", "5 Elephant"} with title "What columns do you want?" with multiple selections allowed
set listreturn to result
set numfields to count of listreturn
if numfields is equal to 1 then
set field1 to word 1 of item 1 of listreturn as integer
else if numfields is equal to 2 then
set field1 to word 1 of item 1 of listreturn as integer
set field2 to word 1 of item 2 of listreturn as integer
else if numfields is equal to 3 then
set field1 to word 1 of item 1 of listreturn as integer
set field2 to word 1 of item 2 of listreturn as integer
set field3 to word 1 of item 3 of listreturn as integer
else if numfields is equal to 4 then
set field1 to word 1 of item 1 of listreturn as integer
set field2 to word 1 of item 2 of listreturn as integer
set field3 to word 1 of item 3 of listreturn as integer
set field4 to word 1 of item 4 of listreturn as integer
end if
尝试这样的事情:
repeat numbfields times
set field(repeatcount) to word 1 of item (repeatcount) of listreturn
end repeat
答案 0 :(得分:0)
您无法在运行时创建动态变量名称。
解决方案是将field
变量放入另一个列表中。
set fieldList to {field1, field2, field3, field4, field5}
choose from list {"1 Apple", "2 Banana", "3 Cat", "4 Dog", "5 Elephant"} with title "What columns do you want?" with multiple selections allowed
set listreturn to result
if listreturn is false then return
repeat with i from 1 to count listreturn
set item i of fieldList to word 1 of item i of listreturn as integer
end repeat