Hi, Today I started cleaning my code and I’m quite a noob when it comes to repeats and loops in AppleScript.
I’ve got a drag 'n drop application and when a file gets dropped, it executed 48 shell scripts. The shell scripts are all the same but have a different variable, i.e. theResult1, theResult2,…
Is it possible to achieve this with repeat ? so that it changes the variables every time ?
Something like
set allVariables to {theResult1, theResult2, ..., theResult48}
repeat with aVariable in allVariables
--Do Actions & theResult1
end repeat
I know this doesn’t work because I haven’t got any clue about repeat in AppleScript
I want to repeat this :
if theResult1 is not "" then
try
do shell script "rm " & theResult1
on error
show window "PasswordBox"
set contents of text field "FullUserNameBox" of window "PasswordBox" to FullUserName
set AdminPass to contents of text field "AdminPasswordBox"
set AskedPass to true
do shell script "rm " & theResult1 password AdminPass with administrator privileges
end try
end if
Thanks in advance.
the easiest way is to have all results in a list
property resultList : {}
repeat
--.
set end of resultList to something --adds something at end of the list
--.
end repeat
--.
repeat with oneResult in resultList
if oneResult is not "" then
try
do shell script "rm " & oneResult
on error
show window "PasswordBox"
set contents of text field "FullUserNameBox" of window "PasswordBox" to FullUserName
set AdminPass to contents of text field "AdminPasswordBox"
set AskedPass to true
do shell script "rm " & oneResult password AdminPass with administrator privileges
end try
end if
end repeat
Thanks for your help, but what do you mean with --… and the something variable ? Repeating that line with different oneResult, twoResult,… variables?
Really thanks
–. is just some code.
instead of
set result1 to "123"
set result2 to "456"
you can write
set end of resultList to "123"
set end of resultList to "456"
Then you have a list of the values and can process them easily in a loop
Again, sorry to be such a noob but all the variables are already set, they’re a POSIX path. Or doesn’t it matter?
i.e.
property resultList : {}
set theResult1 to "/Users/123/123.txt"
set theResult2 to "/Users/456/456.txt"
repeat
--.
set end of resultList to something --adds something at end of the list
--.
end repeat
--I guess I don't need this repeat if the paths are alreay set ?
--.
repeat with theResult1 in resultList
if theResult1 is not "" then
try
do shell script "rm " & theResult1
on error
show window "PasswordBox"
set contents of text field "FullUserNameBox" of window "PasswordBox" to FullUserName
set AdminPass to contents of text field "AdminPasswordBox"
set AskedPass to true
do shell script "rm " & theResult1 password AdminPass with administrator privileges
end try
end if
end repeat
Instead of setting 48 single variables, you can also use a repeat loop and populate a list
Now I get it, ignore the script posts above because I changed variable names that would mess up the script.
Can’t do that, the variables are set once a file gets dropped on the app and the variables are different when you drop another file onto it.
But I think I’ll leave out this part
repeat
--.
set end of resultList to something --adds something at end of the list
--.
end repeat
and set resultList to the the 48 variables.
Thanks a lot Stefan!