Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kryten2

macrumors 65816
Original poster
Mar 17, 2012
1,115
99
Belgium
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.
 

Attachments

  • Picture 4.png
    Picture 4.png
    64.8 KB · Views: 121
  • Picture 5.png
    Picture 5.png
    20.2 KB · Views: 94
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
 
Last edited:
Thanks for the reply chown33. Clear as ever. Had to lookup the meaning of superfluously prolix. Got it now.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.