n8felton
3/26/2014 - 6:03 PM

Stupid simple script run via Alfred to make a 512x512 icon out of a .app or .icns file and copy it to a Munki repo

Stupid simple script run via Alfred to make a 512x512 icon out of a .app or .icns file and copy it to a Munki repo

# "{query}" string on line 18 is a tab-delimited string of a list of paths
# given by Alfred via a Finder selection

import os
import shutil
import subprocess
import sys
import tempfile

SIZE = '512x512'
MUNKI_REPO_ICONS_DIR = "/Volumes/munki_repo/icons"

if not os.path.exists('/usr/local/munki'):
    sys.exit(1)
sys.path.insert(0, '/usr/local/munki')
from munkilib import FoundationPlist

inputfiles = "{query}".split("\t")
for f in inputfiles:
    if f.endswith('.app'):
        iconfilename = FoundationPlist.readPlist(os.path.join(f, 'Contents/Info.plist'))['CFBundleIconFile']
        if iconfilename.endswith('.icns'):
            iconfilename = iconfilename.split('.icns')[0]
        icnspath = os.path.join(f, 'Contents/Resources', iconfilename + '.icns')
        if not os.path.exists(icnspath):
            print >> sys.stderr, "Icon path %s not found!" % icnspath
            continue
    elif f.endswith('.icns'):
        icnspath = f
    name_portion = os.path.splitext(os.path.basename(f))[0]

    outdir = tempfile.mkdtemp() + '.iconset'
    subprocess.call(['/usr/bin/iconutil', '--convert', 'iconset', '--output', outdir, icnspath])
    chosen_icon = os.path.join(outdir, 'icon_%s.png' % SIZE)
    shutil.copy(chosen_icon, os.path.join(MUNKI_REPO_ICONS_DIR, name_portion + '.png'))