【问题标题】:Applescript equivalent of "continue"?Applescript 相当于“继续”?
【发布时间】:2010-11-04 17:01:27
【问题描述】:

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

我对 AppleScript 不太熟悉,但我发现它有用几次。

【问题讨论】:

    标签: loops applescript continue


    【解决方案1】:

    基于 Tom Lokhorst 的answer,这里有一个变体,可以根据您设置的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
    

    广义

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

    假设您希望在这两个ifs 中拥有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
    

    【讨论】:

      【解决方案2】:

      您还可以对仅按条件重复的循环使用“repeat while”。

      【讨论】:

        【解决方案3】:

        你们都把它复杂化了。试试这个:

        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
        

        【讨论】:

          【解决方案4】:

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

          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
          

          【讨论】:

            【解决方案5】:
            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 子句。

            【讨论】:

              【解决方案6】:
              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】:

                在搜索了这个确切的问题后,我在网上找到了这个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的能力。

                【讨论】:

                • 上面的代码不会编译,因为applescript中的cmets不是#
                • 在 applescript 2.0 中,cmets 也允许使用 # 符号:developer.apple.com/library/mac/documentation/applescript/…
                • 代码无论如何都会编译,因为“--”在所有“#”之前,并且“--”总是在 AppleScript 中标记注释。
                猜你喜欢
                • 2011-04-29
                • 1970-01-01
                • 1970-01-01
                • 2011-06-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-05-27
                • 2013-02-11
                相关资源
                最近更新 更多