# "{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'))