> Bummer. The attached version uses Lucida Grande, which is a system
> font. Its font metrics are a bit different, hence I had to change some
> rects, but it looks very similar to Lucida Grande now (modulo the
> digit glyphs, but we don't use those). It also adds some more helpful
> error messages.

This attached version even works if doc-bm-generic.icns does not exist  
when the script is started :-P


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_mac" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

# Creates a document icon from an app icon and an optional text.

# The font is not quite right, use this script to create a document icon
# for 'PDF' and compare the D with the D in Preview's pdf.icns

# http://www.macresearch.org/cocoa-scientists-part-xx-python-scriptersmeet-cocoa
from Foundation import *
from AppKit import *
import os
import sys

# icon types
LARGE = 0  # 512, 128, 32, 16; about 96kB
SMALL = 1  # 128, 32, 16; about 36kB
LINK = 2  # Create link to generic icon; 4kB (== smallest block size on HFS+)

# path to makeicns binary
MAKEICNS = './makeicns'

# List of icons to create
GENERIC_ICON_NAME = 'doc-bm-generic'
vimIcons = {
    GENERIC_ICON_NAME: [u'', LARGE],
    'doc-bm-vim': [u'VIM', LARGE],
    'doc-bm-txt': [u'TXT', SMALL],
    'doc-bm-tex': [u'TEX', SMALL],
    'doc-bm-h': [u'H', SMALL],
    'doc-bm-c': [u'C', SMALL],
    'doc-bm-m': [u'M', SMALL],
    'doc-bm-mm': [u'MM', SMALL],
    'doc-bm-cpp': [u'C\uff0b\uff0b', SMALL],  # fullwidth plusses
    'doc-bm-java': [u'JAVA', SMALL],
    'doc-bm-f': [u'FTRAN', SMALL],
    'doc-bm-html': [u'HTML', SMALL],
    'doc-bm-xml': [u'XML', SMALL],
    'doc-bm-js': [u'JS', SMALL],
    'doc-bm-perl': [u'PERL', SMALL],
    'doc-bm-py': [u'PYTHON', SMALL],
    'doc-bm-php': [u'PHP', SMALL],
    'doc-bm-rb': [u'RUBY', SMALL],
    'doc-bm-bash': [u'SH', SMALL],
    'doc-bm-patch': [u'DIFF', SMALL],
    'doc-bm-applescript': [u'\uf8ffSCPT', SMALL],  # apple sign
    'doc-bm-as': [u'FLASH', LINK],
    'doc-bm-asp': [u'ASP', LINK],
    'doc-bm-bib': [u'BIB', LINK],
    'doc-bm-cs': [u'C#', LINK],
    'doc-bm-csfg': [u'CFDG', LINK], #D
    'doc-bm-csv': [u'CSV', LINK],
    'doc-bm-tsv': [u'TSV', LINK],
    'doc-bm-cgi': [u'CGI', LINK],
    'doc-bm-cfg': [u'CFG', LINK],
    'doc-bm-css': [u'CSS', SMALL],
    'doc-bm-dtd': [u'DTD', LINK],
    'doc-bm-dylan': [u'DYLAN', LINK],
    'doc-bm-erl': [u'ERLANG', SMALL],
    'doc-bm-fscript': [u'FSCPT', SMALL],
    'doc-bm-hs': [u'HS', SMALL],
    'doc-bm-inc': [u'INC', LINK],
    'doc-bm-ics': [u'ICS', SMALL],
    'doc-bm-ini': [u'INI', LINK],
    'doc-bm-io': [u'IO', LINK],
    'doc-bm-bsh': [u'BSH', LINK], #D
    'doc-bm-properties': [u'PROP', LINK],
    'doc-bm-jsp': [u'JSP', SMALL],
    'doc-bm-lisp': [u'LISP', SMALL],
    'doc-bm-log': [u'LOG', SMALL],
    'doc-bm-wiki': [u'WIKI', SMALL],
    'doc-bm-ps': [u'PS', LINK],
    #'doc-bm-plist': [u'PLIST', SMALL],
    'doc-bm-sch': [u'SCHEME', SMALL],
    'doc-bm-sql': [u'SQL', SMALL],
    'doc-bm-tcl': [u'TCL', SMALL],
    'doc-bm-xsl': [u'XSL', LINK],
    'doc-bm-vcf': [u'VCARD', SMALL],
    'doc-bm-vb': [u'VBASIC', LINK],
    'doc-bm-yaml': [u'YAML', SMALL],
    'doc-bm-gtd': [u'GTD', LINK], #D
}


# Resources
BACKGROUND = '/System/Library/CoreServices/CoreTypes.bundle/' + \
    'Contents/Resources/GenericDocumentIcon.icns'  # might require leopard?
APPICON = 'vim-noshadow-512.png'
#APPICON = 'vim-noshadow-no-v-512.png'


def createIcon(outname, text, iconname=APPICON, bgname=BACKGROUND):
  # Prepare input images
  bg = NSImage.alloc().initWithContentsOfFile_(bgname)
  if not bg:
    print 'Failed to load', bgname
    sys.exit(1)

  icon = NSImage.alloc().initWithContentsOfFile_(iconname)
  if not icon:
    print 'Failed to load', iconname
    sys.exit(1)


  # Prepare text format
  style = NSMutableParagraphStyle.new()
  style.setParagraphStyle_(NSParagraphStyle.defaultParagraphStyle())
  style.setAlignment_(NSCenterTextAlignment)
  # http://developer.apple.com/documentation/Cocoa/Conceptual/AttributedStrings/Articles/standardAttributes.html#//apple_ref/doc/uid/TP40004903
  fontname = 'LucidaGrande-Bold'
  attribs = {
      NSParagraphStyleAttributeName: style,
      NSParagraphStyleAttributeName: style,
      NSFontAttributeName: NSFont.fontWithName_size_(fontname, 72.0),
      NSKernAttributeName: -1.0,  # tighten font a bit
      NSForegroundColorAttributeName: NSColor.colorWithDeviceWhite_alpha_(
        0.34, 1)
  }

  if not attribs[NSFontAttributeName]:
    print 'Failed to load font', fontname
    sys.exit(1)

    
  # Draw!
  bg.lockFocus()
  w, h = 289, 289
  icon.drawInRect_fromRect_operation_fraction_(
      (((512-w)/2 + 1, 405 - h), (w, h)),
      NSZeroRect, NSCompositeSourceOver, 1.0)
  text.drawInRect_withAttributes_( ((0, 7), (512, 119)), attribs)
  bg.unlockFocus()

  # Save
  # http://www.cocoadev.com/index.pl?NSImageToJPEG (this is retarded)
  tmp = NSBitmapImageRep.imageRepWithData_(bg.TIFFRepresentation())
  png = tmp.representationUsingType_properties_(NSPNGFileType, None)
  png.writeToFile_atomically_(outname, True)


TMPFILE = 'make_icons_tmp.png'
def main():
  # Make us not crash
  # http://www.cocoabuilder.com/archive/message/cocoa/2008/8/6/214964
  NSApplicationLoad()

  #createIcon('test.png',
      #NSString.stringWithString_(u'PDF'), iconname='preview.icns')

  if not os.access(MAKEICNS, os.X_OK):
    print 'Cannot find makeicns at', MAKEICNS
    return

  # create LARGE and SMALL icons first...
  for name, t in vimIcons.iteritems():
    text, size = t
    if size == LINK: continue
    print name
    icnsName = '%s.icns' % name

    createIcon(TMPFILE, NSString.stringWithString_(text))
    if size == LARGE:
      os.system('%s -512 %s -128 %s -32 %s -16 %s -out %s' % (MAKEICNS,
        TMPFILE, TMPFILE, TMPFILE, TMPFILE, icnsName))
    elif size == SMALL:
      os.system('%s -128 %s -32 %s -16 %s -out %s' % (MAKEICNS,
        TMPFILE, TMPFILE, TMPFILE, icnsName))

  # ...create links later (to make sure the link targets exist)
  for name, t in vimIcons.iteritems():
    text, size = t
    if size != LINK: continue
    print name
    icnsName = '%s.icns' % name

    # remove old version of icns
    if os.access(icnsName, os.F_OK):
      os.remove(icnsName)
    os.symlink('%s.icns' % GENERIC_ICON_NAME, icnsName)

  
if __name__ == '__main__':
  try:
    main()
  finally:
    if os.access(TMPFILE, os.F_OK):
      os.remove(TMPFILE)


Reply via email to