Applescript相当于“继续”?

时间:2009-06-21 20:04:00

标签: loops applescript continue

我在AppleScript中有一个简单的'重复',并希望有条件地转到“重复”中的下一个项目。基本上我在寻找其他语言类似“继续”(或休息?)的东西。

我不熟悉AppleScript,但我现在发现它很有用了几次。

7 个答案:

答案 0 :(得分:44)

在搜索到这个问题之后,我在网上发现了这个book extract。它完全回答了如何跳过当前迭代并直接跳转到repeat循环的下一次迭代的问题。

Applescript有exit repeat,它将完全结束循环,跳过所有剩余的迭代。这在无限循环中很有用,但在这种情况下不是我们想要的。

显然,AppleScript中不存在类似continue的功能,但这是一个模拟它的技巧:

set aList to {"1", "2", "3", "4", "5"}

repeat with anItem in aList -- # actual loop
    repeat 1 times -- # fake loop
        set value to item 1 of anItem

        if value = "3" then exit repeat -- # simulated `continue`

        display dialog value
    end repeat
end repeat

这将显示1,2,4和5的对话框。

在这里,你创建了两个循环:外部循环是你的实际循环,内部循环是一个只重复一次的循环。 exit repeat将退出内循环,继续外循环:正是我们想要的!

显然,如果你使用它,你将失去正常exit repeat的能力。

答案 1 :(得分:7)

set aList to {"1", "2", "3", "4", "5"}

repeat with anItem in aList -- # actual loop
    try
        set value to item 1 of anItem

        if value = "3" then error 0 -- # simulated `continue`

        log value
    end try
end repeat

这仍然会给你“退出重复”的可能性

答案 2 :(得分:6)

set aList to {"1", "2", "3", "4", "5"}

repeat with anItem in aList -- # actual loop
    try -- # needed to simulate continue
        set value to item 1 of anItem
        if value = "3" then continueRepeat -- # simulated `continue` throws an error to exit the try block

        log value
    on error e
        if e does not contain "continueRepeat" then error e -- # Keeps error throwing intact
    end try
end repeat

基于上面基于try块的方法,但读取稍微好一些。当然,由于未定义continueRepeat,因此将抛出错误,导致跳过try块的其余部分。

为了保持错误抛出完整,包括抛出任何意外错误的on error子句。

答案 3 :(得分:2)

- 或者您可以使用不同的策略:使用循环循环,并在处理程序中执行条件逻辑,如下所示:

set aList to {"1", "2", "3", "4", "5"}

repeat with anItem in aList
   doConditionalWork(anItem as string)
end repeat

on doConditionalWork(value)

   if value = "3" then return

   display dialog value

end doConditionalWork

答案 4 :(得分:2)

你们都过于复杂了。试试这个:

set aList to {"1", "2", "3", "4", "5"}

repeat with anItem in aList
    set value to item 1 of anItem
    if value is not "3" then log value
end repeat

答案 5 :(得分:1)

您也可以使用“repeat while”来循环,只能有条件地重复。

答案 6 :(得分:0)

基于 Tom Lokhorst 的 answer,这里有一个变体,可以根据您设置的 break 变量执行 continueisExit。 为此,在外部(非假)循环的末尾,您添加

if isExit then
    exit repeat
end if

这样,如果 isExit 为真,您也只需退出外循环,成为一个有效的 break

原始问题/答案:

对于最初的问题,这看起来像这样,下面将是一个概括的问题。

set aList to {"1", "2", "3", "4", "5"}

repeat with anItem in aList -- actual loop
    set isExit to false
    repeat 1 times -- fake loop
        -- do your stuff
        set value to item 1 of anItem

        if value = "3" then 
            -- simulated `continue`
            set isExit to false
            exit repeat
        end if

        if value = "69" then 
        -- simulated `break`
            set isExit to true
            exit repeat
        end if
        
        display dialog value
    end repeat

    if isExit then
        exit repeat
    end if
end repeat

广义

所以把它分解成一个更简单的例子:

假设您希望在这两个 continue 中包含 breakif。 您可以将所有代码放在它们之间,我只包含与不同退出策略相关的代码。

如果你想写这样的东西:

repeat <condition_repeat>
    if <condition_continue> then
        continue
    end if
    if <condition_break> then
        break
    end if
end repeat

可以写成

repeat <condition_repeat>
    set isExit to false                -- added 
    repeat 1 times                     -- added 
        if <condition_continue> then
            set isExit to false        -- changed
            exit repeat                -- changed
        end if
        if <condition_break> then
            set isExit to true         -- changed
            exit repeat                -- changed
        end if
    end repeat                         -- added
    if isExit then                     -- added
        exit repeat                    -- added
    end if                             -- added
end repeat
相关问题