Hi there and happy new year to all.
I’ve got a curious puzzle that I can’t work out. I’m trying to compare some records and at first it seems simple: you can simply ask AppleScript if one record is the same as another and it will answer true or false. But when I put the records into a list then go through each item in that list and compare it with a given record, it doesn’t seem to work any more. See below:
--THESE TWO RECORDS ARE THE SAME
set ThisFill to {black:0, cyan:0, magenta:100, yellow:0}
set ThisOtherFill to {black:0, cyan:0, magenta:100, yellow:0}
--THIS RECORD IS DIFFERENT
set AThirdFill to {black:0, cyan:0, magenta:100, yellow:1}
--LET'S TEST THAT OUT
set MyTest1 to ThisFill is ThisOtherFill --RETURNS TRUE
set MyTest2 to ThisFill is AThirdFill --RETURNS FALSE
--OK, SO LET'S TRY PUTTING THOSE RECORDS IN A LIST
set ThisList to {ThisFill, ThisOtherFill, AThirdFill}
set TrueFalseList to {} --SET UP AN EMPTY LIST TO HOLD TRUE/FALSE VALUES
--CYCLE THROUGH THE RECORDS IN THE LIST
repeat with ThisItem in ThisList
set the end of TrueFalseList to (ThisFill is ThisItem)
end repeat
--WHY DOES TrueFalseList RETURN WITH 3 FALSE VALUES?
Any help appreciated.
David
Hi.
It’s because with the ‘repeat with ThisItem in ThisList’ kind of repeat, ‘ThisItem’ is a reference to an item in ‘ThisList’, not the value of the item. If you need to use this kind of repeat, you can resolve the reference with ‘contents’:
--CYCLE THROUGH THE RECORDS IN THE LIST
repeat with ThisItem in ThisList
set the end of TrueFalseList to (ThisFill is ThisItem's contents)
end repeat
Or of course you could use an index repeat instead:
--CYCLE THROUGH THE RECORDS IN THE LIST
repeat with i from 1 to (count ThisList)
set ThisItem to item i of ThisList
set the end of TrueFalseList to (ThisFill is ThisItem)
end repeat
Here is the fixed script (Notice the change in the repeat loop!)
set ThisFill to {black:0, cyan:0, magenta:100, yellow:0}
set ThisOtherFill to {black:0, cyan:0, magenta:100, yellow:0}
set AThirdFill to {black:0, cyan:0, magenta:100, yellow:1}
set MyTest1 to ThisFill is ThisOtherFill --RETURNS TRUE
set MyTest2 to ThisFill is AThirdFill --RETURNS FALSE
set ThisList to {ThisFill, ThisOtherFill, AThirdFill}
set TrueFalseList to {} --SET UP AN EMPTY LIST TO HOLD TRUE/FALSE VALUES
repeat with ThisItem in ThisList
set the end of TrueFalseList to (ThisFill is contents of ThisItem)
end repeat
Thanks guys,
I didn’t realise there was such a difference between those two types of repeat-with loops.
David
Just a complementary detail.
You have :
set ThisFill to {black:0, cyan:0, magenta:100, yellow:0}
set anotherFill to {black:0, cyan:0, yellow:0, magenta:100}
Have you ever see that AppleScript is fair enough to “understand” that these two records are equal ?
Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) vendredi 1 janvier 2016 17:54:59
Hi Yvan, yes that is because a record is an unordered list of named values as opposed to a list which is an ordered list of unnamed values. So in a record it doesn’t matter which order the values are in.