I would like to read the list of colors that are shown on the bottom of the NSColorPanel (see image below). Is there a way to do this?
I would like to read the list of colors that are shown on the bottom of the NSColorPanel (see image below). Is there a way to do this?
For undocumented access (this may not work within a sandbox and will get your app rejected by Apple if you plan to distribute through the App Store):
The
colorsarray will then containNSColorobjects of the colour panel.This works as far back as OS X 10.6. It may work on earlier versions also but you'll need to obtain the filename differently (since
URLsForDirectory:inDomains:was introduced in 10.6). Inside theNSColorPanelSwatches.plistfile is an internal version number which is set to6for 10.6 right through to 10.10. It could change in the future, but you could be more-or-less safe by doing:If you're interested in where the positions of the colours are, you can decode an
NSIndexSetfrom the unarchiver using theNSSwatchColorIndexeskey, and use that index set in conjunction with the number of rows and columns that you can determine by decoding integers with the keysNSSwatchLayoutNumRowsandNSSwatchLayoutNumColumns. The nth index in the index set refers to the location of the nth colour in the array and the indexes increase downwards. For example, the first “colour box” in the panel is index 0, and the box below it is index 1. The box to the right of “index 0” is actually index 10 (or whatever number you decoded fromNSSwatchLayoutNumRows).So if you have a colour in the first box, and another colour in the box to the right, you'll have two
NSColorobjects in thecolorsarray, and theNSIndexSetwill contain two indexes, 0 and 10.