I’m sure this is pretty simple, but in the state of mind I’m in at the moment (I lost a LOT of data last night) the answer is evading me.
The following script works fine
set theList to {{"~/Desktop/", "Folder1"}, ¬
{"~/Desktop/", "Folder2"}, ¬
{"~/Desktop/", "Folder3"}}
repeat with i from 1 to count of items of theList
set x to item i of theList
MakeNew(x)
end repeat
on MakeNew({PathName, CreateName})
do shell script "mkdir " & PathName & CreateName
end MakeNew
However this does not
set theList to {{"~/Desktop/", "Folder1"}, ¬
{"~/Desktop/", "Folder2"}, ¬
{"~/Desktop/", "Folder3"}}
repeat with i from 1 to count of items of theList
MakeNew((item i of theList))
end repeat
on MakeNew({PathName, CreateName})
do shell script "mkdir " & PathName & CreateName
end MakeNew
For the life of me I can’t figure out why this wouldn’t work. What’s the difference between passing the item of a list versus setting a variable to that item then passing the variable.
In the second version, item i of theList is a list, but MakeNew specifies the contents of the list instead of just a list:
set theList to {{"~/Desktop/", "Folder1"}, ¬
{"~/Desktop/", "Folder2"}, ¬
{"~/Desktop/", "Folder3"}}
repeat with i from 1 to count of items of theList
MakeNew(item i of theList)
end repeat
on MakeNew(myList)
set {PathName, CreateName} to myList
display dialog "OK"
end MakeNew
In the second version, this also works (strange, I know):
set theList to {{"~/Desktop/", "Folder1"}, ¬
{"~/Desktop/", "Folder2"}, ¬
{"~/Desktop/", "Folder3"}}
repeat with i from 1 to count of items of theList
MakeNew(items of (item i of theList) as list)
end repeat
on MakeNew({PathName, CreateName})
display dialog "OK"
end MakeNew
Well that both clarified and confused me even more. When you mention MakeNew looking for the contents of the list this line actually makes perfect sense to me:
MakeNew(items of (item i of theList) as list)
What confuses me though is since its looking for the contents I don’t quite grasp how this works:
set x to item i of theList
MakeNew(x)
since isn’t it doing the same thing… passing the list rather then the lists contents?
I believe that first iteration works because (item i) is 2 items, not a list, and the handler is expecting a 2 item list, so it takes the two items, and runs. The second iteration is sending a list (a single item), but the handler wants 2 items, so it does not fire. I think that is why Adam’s versions works; it is sending the items, not a list.
Of course, I have my preferences on how I would code this:
set theList to {{"~/Desktop/", "Folder1"}, ¬
{"~/Desktop/", "Folder2"}, ¬
{"~/Desktop/", "Folder3"}}
repeat with i in theList
MakeNew(i)--Just send the list
end repeat
on MakeNew(pc)--since a list was sent, we know this is a list, without telling the handler explicitly
do shell script "mkdir " & (pc's item 1) & (pc's item 2)--Separate the items here
end MakeNew
Yeah I had that too, but was just trying to experiment with different ways to skin a cat. Thanks for the info though that makes a decent amount of sense =)
The reason it doesn’t work is that you’re passing a reference to a list item and not a list of two items. The same thing happens when you use the more common form of the repeat loop where this occurs.
set l to {{1, 2}, {3, 4}, {5, 6}}
repeat with this_item in l
DoSomething(this_item)
end repeat
--
on DoSomething({x, y})
display dialog (x + y)
end DoSomething
Note that here, you are using a variable in the call to the subroutine and it still doesn’t work because it’s a refernce to the list item.