Macro to apply .p2m based on material

I have been finding lots of different macros to apply appearance and/or materials. However the material of the part must be known before, and the appearance is only an RGB number. I'm trying to figure out how to apply a .p2m appearance file to a part, based on the already applied material. I was hoping to do a lookup and an If/Then action. Can anyone help?

SolidworksApi macros