Find key in Property List based on value

It is probably a simple syntax problem but I am not able to retrieve a Key name from a property list.

I am using a background RGB value to identify a particular Color Scheme a user has selected. I created a property list of a schema name to RGB values. i.e. (Navy:(0,0,140), etc). I have a script to capture the coloratlocation for a background color value. I do a "if input is in RGBtable then return the schema name.
This script finds the RGB value in table but I do not know how to return the key name.

Comments

  • RyanDRyanD Member
    You can iterate over all the keys in your list and find which key's value matches the value you are looking for. For example:

    set foo to (a:1, b:2, c:3)
    repeat with each item of foo's keys
    if 2 equals foo.(it) then put it
    end repeat

    In this case the key 'b' is printed.
  • I tried your example and even used lists and it worked fine

    However when I used the example as a template for my script I did not get the results I expected. I think the problem now lies with how lists are passed between functions

    put ColorAtLocation(x,y) into RGB

    fum.foo RGB <- is this the correct syntax to pass a list

    to foo fooRGB
    log fooRGB <- 2,45,100 is displayed not (2,45,100)
    set foo to (a: (1,45,33), b: (2,45,100), c: (3,23,130))
    repeat with each item of foo's keys
    if 2 equals foo.(it) then
    put it into SeeIt
    end if
    end repeat
    log SeeIt <- nothing shows up
    end foo

    I can count items in fooRGB and display a single item if I want to but when I do the comparison to the property list values no match is ever found
  • SenseTalkDougSenseTalkDoug ForumAdmin admin
    The way you are passing the value is fine. In this case the value you are passing is a color, so its representation is controlled by the colorFormat global property, which by default will present the color as a text string (like "2,45,100" in your case).

    For your test you could either put all of your comparison values in this same format, like this:
    set foo to (a:"1,45,33", b:"2,45,100", c:"3,23,130")
    
    or, you can ask SenseTalk to compare colors rather than text. Doing it this way will allow the colors being compared to be in any of the dozen or so color formats (including named colors) that SenseTalk understands. Your comparison would then look something like this:
    if the color of fooRGB equals foo.(it) as color then
      put it into SeeIt
    end if
    
    This works because whenever SenseTalk knows that two values are both colors, it will compare them as colors, regardless of what format they are in. Using "the color of" or "as color" are two ways to ensure that those values are treated as colors.
Sign In or Register to comment.