KevinCork KevinCork - 21 days ago 6
AppleScript Question

AppleScript list/contains

set r to ""
set device to "IPHONE 6 PLUS SILVER 128GB-AUS"
set HighValueDevicesPass to {"IPHONE 7", "IPHONE 6", "IPAD PRO", "IPHONE 6S", "IPHONE 6 PLUS"}

if devices contains HighValueDevicesPass then
set r to "Pass"
end if
return r


I don't understand why this is not working.
The variable is "IPHONE 6 PLUS SILVER 128GB-AUS" so actually contain "IPHONE 6 PLUS" which is on the list.

It's working fine if I use IF is on the list, but then I would have to set all different model as a variable.

How can I do a partial match?

Answer

You can check if an string is in an list, but you cannot check if an arbitrary list item is in an string. You have to repeat all the items one by one.

set r to ""
set device to "IPHONE 6 PLUS SILVER 128GB-AUS"
set HighValueDevicesPass to {"IPHONE 7", "IPHONE 6", "IPAD PRO"}

repeat with i from 1 to count HighValueDevicesPass
    if device contains item i of HighValueDevicesPass then
        set r to "pass"
        exit repeat
    end if
end repeat

return r

I also removed the values "IPHONE 6S" and "IPHONE 6 PLUS" because they are matched by the "IPHONE 6" string already.

Comments