Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created May 10, 2016 13:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typemytype/3a1ee58b945c68ed08669ced5fabe39c to your computer and use it in GitHub Desktop.
Save typemytype/3a1ee58b945c68ed08669ced5fabe39c to your computer and use it in GitHub Desktop.
add random color to app icon
from AppKit import *
from lib.tools.misc import randomColor
# get the application icon image
icon = NSApp().applicationIconImage()
# get the size
w, h = icon.size()
# make a rect with the size of the image
imageRect = NSMakeRect(0, 0, w, h)
# create a new image with the same size
new = NSImage.alloc().initWithSize_((w, h))
# lock focus on the image to draw in
new.lockFocus()
# draw the original iamge
icon.drawInRect_(imageRect)
# get a bright random color
randomColor(asNSColor=True).set()
# draw the color over the source
NSRectFillUsingOperation(imageRect, NSCompositeSourceAtop)
# draw the image again
icon.drawInRect_fromRect_operation_fraction_(imageRect, imageRect, NSCompositePlusLighter, 1)
# un lock the image, drawing is done
new.unlockFocus()
# set the image as the application icon
NSApp().setApplicationIconImage_(new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment