property hexChrs : "0123456789abcdef" tell application "Numbers" tell front document tell active sheet tell (first table whose class of selection range is range) set outList to {} repeat with aCell in cells of (get selection range) set v to value of aCell if text 1 of v is "#" then set v to text 2 thru -1 of v set hexColor to my hexColorToRGB(v) set inverseColor to {} repeat with i from 1 to 3 copy (65535 - (item i of hexColor)) to end of inverseColor end repeat if v is "808080" then set inverseColor to {65535, 65535, 65535} -- hack to adjust gray set background color of aCell to hexColor set text color of aCell to inverseColor end repeat end tell end tell end tell end tell return outList on hexColorToRGB(h) -- convert if (count h) < 6 then my badColor(h) set rgbColor to {} try repeat with i from 1 to 6 by 2 set end of rgbColor to ((my getHexVal(text i of h)) * 16 + (my getHexVal(text (i + 1) of h))) * 257 end repeat end try if (count rgbColor) < 3 then my badColor(h) return rgbColor end hexColorToRGB on getHexVal(c) if c is not in hexChrs then error set text item delimiters to c return (count text item 1 of hexChrs) end getHexVal on badColor(n) display alert n & " is not a valid hex color" buttons {"OK"} cancel button "OK" end badColor