Skip to content

Instantly share code, notes, and snippets.

@dalleng
Last active August 29, 2015 14:13
Show Gist options
  • Save dalleng/1879263215191e3df6e7 to your computer and use it in GitHub Desktop.
Save dalleng/1879263215191e3df6e7 to your computer and use it in GitHub Desktop.
Saves the icon of a running app with a given pid
import sys
from Cocoa import NSDictionary, NSNumber
from AppKit import NSRunningApplication, NSImageCompressionFactor, NSBitmapImageRep, NSPNGFileType
app = NSRunningApplication.runningApplicationWithProcessIdentifier_(int(sys.argv[1]))
icon = app.icon()
image_data = icon.TIFFRepresentation()
image_rep = NSBitmapImageRep.imageRepWithData_(image_data)
image_props = NSDictionary.dictionaryWithObject_forKey_(NSNumber.numberWithFloat_(1.0), NSImageCompressionFactor)
image_data = image_rep.representationUsingType_properties_(NSPNGFileType, image_props)
file_name = "{}.png".format(app.localizedName())
image_data.writeToFile_atomically_(file_name, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment