Repeat loop weirdness

Discussion in 'Mac Programming' started by kryten2, Aug 9, 2012.

  1. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #1
    I noticed some odd behaviour when copying some code from ScriptEditor to a Run Applescript action in Automator.

    This example code runs fine in Scripteditor :

    Code:
    set numberList to {"1", "2", "3"}
    repeat with i from 1 to number of items in numberList
    	set this_item to item i of numberList
    	display dialog this_item giving up after 3
    end repeat
    
    When copied to the Run Applescript action in Automator it changes into this when compiled :

    Code:
    set numberList to {"1", "2", "3"}
    repeat with i from 1 to number of items [COLOR="Red"]of[/COLOR] numberList
    	set this_item to item i of numberList
    	display dialog this_item giving up after 3
    end repeat
    The "of" between number and items is no longer bold and the "in" changed to of. When run it errors with the following message :

    Syntax Error

    No result was returned from some part of this expression.

    The solution is to change the code to this :

    Code:
    set numberList to {"1", "2", "3"}
    repeat with i from 1 to count of items of numberList
    	set this_item to item i of numberList
    	display dialog this_item giving up after 3
    end repeat
    
    Why does this happen? Can someone shed some light on this? Thanks.
     

    Attached Files:

  2. chown33, Aug 9, 2012
    Last edited: Aug 9, 2012

    macrumors 603

    Joined:
    Aug 9, 2009
    #2
    Why not just switch to the more concise:
    Code:
    repeat with each_item in numberList
    	display dialog each_item giving up after 3
    end repeat
    
    The form is:
    Code:
    repeat with listVar in someList
    
    Search for Repeat with listVar in this doc:
    http://oreilly.com/catalog/aplscptian/chapter/ch07.html


    EDIT
    If you insist on using counted loops, consider using the shorter:
    Code:
    count of listName
    
    Reference doc:
    https://developer.apple.com/library...applescriptlangguide/reference/aslr_cmds.html

    It's superfluously prolix to write:
    Code:
    count of items of listName
    
     
  3. thread starter macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #3
    Thanks for the reply chown33. Clear as ever. Had to lookup the meaning of superfluously prolix. Got it now.
     

Share This Page