"continue"的Applescript等效语句是什么?

43
我有一个简单的“repeat with”循环在AppleScript中,并且想要条件性地继续下一个项目。基本上,我正在寻找类似于其他语言中的“continue”(或break?)的东西。
我对AppleScript不是很熟悉,但现在已经发现了它的一些用途。
7个回答

61
在搜索了这个确切的问题后,我在网上找到了这个书籍摘录。它完全回答了如何跳过当前迭代并直接跳转到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
以上的代码无法编译,因为Applescript中的注释是--而不是#。 - alexyorke
2
在AppleScript 2.0中,#符号也可以用于注释:https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/conceptual/ASLR_lexical_conventions.html#//apple_ref/doc/uid/TP40000983-CH214-SW8 - Joakim
2
代码仍然可以编译,因为“--”在所有的“#”之前,并且“--”一直被用作AppleScript中的注释标记。 - Simon White
如果aList是{"1", "2", "", "4", "5"},在这种情况下,我该如何有效地跳过空字符串?当前的代码在这种情况下会出现错误,显示无法获取""的第一个项目。 - Max Well

9
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

这仍然可以给你“退出重复”功能。

7
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

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

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

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

1
你还可以使用“repeat while”循环,仅在条件满足时才重复执行。

0

基于Tom Lokhorst的答案,这里提供了一种根据你设置的isExit变量来执行breakcontinue的变体方法。 因此,在外部(非虚假)循环的末尾,您需要添加以下代码:

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

泛化

为了更容易理解,我们来看一个简单的例子:

假设你想在这两个if语句中使用continuebreak,你可以将所有代码放在它们之间,我只包含了与不同退出策略相关的代码。

如果你想写出类似下面的代码:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接