在 if 块中重复?
Repeat in an if block?
我正在尝试制作一个程序,要求此人 select 一个文件并试图确认它。当我试图在 if 块中添加重复时,我 运行 陷入了一个问题。是否有任何解决方法,因为这非常令人沮丧。提前致谢! :)
display dialog "Select the program"
tell application "Finder"
set filePath to POSIX path of (choose file)
end tell
display dialog "Are you sure this is the program that you have selected?"
buttons {"Yes", "No"}
if the button returned is "No" then
end repeat
else
虽然这可能会导致我的死亡,但我会回答。研究这个:
set correctChoice to false
repeat until correctChoice is true -- "is true" is actually unnecessary
--I took this next line out because it's unnecessary - you can put this text in the prompt of the choose file, below
-- display dialog "Select the program"
-- this doesn't need to be (and shouldn't be) in a Finder tell block, so I took that out, too:
set filePath to POSIX path of (choose file with prompt "Select the program")
set myQuery to display dialog "Are you sure this is the program that you have selected?" buttons {"Yes", "No"}
if the button returned of myQuery is "No" then
--there is no repeat loop! Where do you want it? I assume you want the repeat outside of this process
--end repeat
else
set correctChoice to true
end if
end repeat
--maybe do other stuff
做我假设您正在尝试的事情意味着将整个事情放在一个重复循环中,当一个布尔变量设置为 true 时该循环停止。 if/then 要么保留布尔值的原始 false 值,要么将其设置为 true,从而允许我们离开重复循环。
"workaround" 是一个术语,表示需要在限制或语言错误内工作的东西。您不需要解决方法 - 您需要正确设置代码。从简单开始 (!!) 并了解各种块的工作原理,然后再尝试强制代码执行您认为它应该执行的操作。
我正在尝试制作一个程序,要求此人 select 一个文件并试图确认它。当我试图在 if 块中添加重复时,我 运行 陷入了一个问题。是否有任何解决方法,因为这非常令人沮丧。提前致谢! :)
display dialog "Select the program"
tell application "Finder"
set filePath to POSIX path of (choose file)
end tell
display dialog "Are you sure this is the program that you have selected?"
buttons {"Yes", "No"}
if the button returned is "No" then
end repeat
else
虽然这可能会导致我的死亡,但我会回答。研究这个:
set correctChoice to false
repeat until correctChoice is true -- "is true" is actually unnecessary
--I took this next line out because it's unnecessary - you can put this text in the prompt of the choose file, below
-- display dialog "Select the program"
-- this doesn't need to be (and shouldn't be) in a Finder tell block, so I took that out, too:
set filePath to POSIX path of (choose file with prompt "Select the program")
set myQuery to display dialog "Are you sure this is the program that you have selected?" buttons {"Yes", "No"}
if the button returned of myQuery is "No" then
--there is no repeat loop! Where do you want it? I assume you want the repeat outside of this process
--end repeat
else
set correctChoice to true
end if
end repeat
--maybe do other stuff
做我假设您正在尝试的事情意味着将整个事情放在一个重复循环中,当一个布尔变量设置为 true 时该循环停止。 if/then 要么保留布尔值的原始 false 值,要么将其设置为 true,从而允许我们离开重复循环。 "workaround" 是一个术语,表示需要在限制或语言错误内工作的东西。您不需要解决方法 - 您需要正确设置代码。从简单开始 (!!) 并了解各种块的工作原理,然后再尝试强制代码执行您认为它应该执行的操作。