-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextract_icon.py
More file actions
executable file
·84 lines (68 loc) · 2.64 KB
/
extract_icon.py
File metadata and controls
executable file
·84 lines (68 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/pythonw
#
# extract_icon.py
# Briefcase
#
# Created by Michael Taylor on 30/06/08.
# Copyright (c) 2008 Hey Mac Software. All rights reserved.
#
import AppKit
import sys, os, ctypes
AppKit.NSApplication.sharedApplication()
#from CoreGraphics import *
# Load QuickLook Framework
QuickLook = ctypes.cdll.LoadLibrary('/System/Library/Frameworks/QuickLook.framework/QuickLook')
class CGSize(ctypes.Structure):
_fields_ = [("width", ctypes.c_float), ("height", ctypes.c_float)]
def imageWithPreviewOfFileAtPath(path, size, as_icon):
file_url = AppKit.NSURL.fileURLWithPath_(path)
if not path or not file_url:
return None
dict = AppKit.NSDictionary.dictionaryWithObject_forKey_(
AppKit.NSNumber.numberWithBool_(as_icon), QuickLook.kQLThumbnailOptionIconModeKey)
print dict
dict = AppKit.CFDictionaryCreateMutable(AppKit.kCFAllocatorDefault, 0, None, None)
ref = QuickLook.QLThumbnailImageCreate(AppKit.kCFAllocatorDefault, path,
CGSize(*size),
None);
return None
# if (ref != NULL) {
# # Take advantage of NSBitmapImageRep's -initWithCGImage: initializer, new in Leopard,
# # which is a lot more efficient than copying pixel data into a brand new NSImage.
# # Thanks to Troy Stephens @ Apple for pointing this new method out to me.
# NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithCGImage:ref];
# NSImage *newImage = nil;
# if (bitmapImageRep) {
# newImage = [[NSImage alloc] initWithSize:[bitmapImageRep size]];
# [newImage addRepresentation:bitmapImageRep];
# [bitmapImageRep release];
#
# if (newImage) {
# return [newImage autorelease];
# }
# }
# CFRelease(ref);
# } else {
# # If we couldn't get a Quick Look preview, fall back on the file's Finder icon.
# NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
# if (icon) {
# [icon setSize:size];
# }
# return icon;
# }
#
# return nil;
def main():
#try:
workspace = AppKit.NSWorkspace.sharedWorkspace()
icon = imageWithPreviewOfFileAtPath(os.path.realpath(sys.argv[1]), (64, 64), True)
if icon:
#icon = workspace.iconForFile_(os.path.realpath(sys.argv[1]))
icon.setSize_( (64, 64) )
data = icon.TIFFRepresentationUsingCompression_factor_(NSTIFFCompressionLZW, 0.0)
data.writeToFile_atomically_('/tmp/out.tif', False)
#except:
#sys.stderr.write('Error\n')
#sys.exit(0)
if __name__ == '__main__':
main()