Created
June 14, 2011 21:20
-
-
Save fikovnik/1025915 to your computer and use it in GitHub Desktop.
Styling Poster in OmniGraffle using color scheme from Adobe Kuler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import os | |
import logging | |
from itertools import permutations | |
from appscript import * | |
from kuler import * | |
def style_shape(shape, styles, colors): | |
""" | |
The styling function. It is done to be extensible. Each data key can define | |
a representing styling function named _style_<key_name>. | |
""" | |
def _style_bgcolor(shape, value): | |
shape.fill_color.set([c for c in colors[value].asRGB16()]) | |
for style, value in styles.items(): | |
# check if the style is supported | |
if '_style_%s' % style in locals(): | |
# get the styling function | |
style_fce = locals()['_style_%s' % style] | |
# style | |
style_fce(shape, value) | |
else: | |
logging.error('Unknown style: %s on object %s' % (style, shape)) | |
def main(): | |
if len(sys.argv) not in (5,6): | |
print('Usage: %s <filename> <canvasname> <apikey> <exportdir> [<theme_id>]' % sys.argv[0]) | |
sys.exit(1) | |
# decode the args | |
filename = os.path.abspath(sys.argv[1]) | |
canvasname = sys.argv[2] | |
apikey = sys.argv[3] | |
exportdir = os.path.abspath(sys.argv[4]) | |
# do we search for a specific theme? | |
theme_id = None | |
if len(sys.argv) == 6: | |
theme_id = sys.argv[5] | |
og = app('OmniGraffle Professional 5.app') | |
assert og | |
# important for export | |
og.current_export_settings.area_type.set(k.all_graphics) | |
doc = og.open(filename) | |
assert doc | |
# find canvas by name | |
canvas = [c for c in doc.canvases() if c.name() == canvasname][0] | |
og.windows.first().canvas.set(canvas) | |
# get all shapes | |
shapes = [(s, s.properties()[k.user_data]) for s in canvas.shapes() if s.properties()[k.user_data] != k.missing_value] | |
logging.info('Styling %d shapes' % len(shapes)) | |
# get theme(s) | |
themes = None | |
if theme_id: | |
themes = Kuler(apikey).search(themeID=theme_id) | |
else: | |
themes = Kuler(apikey).list() | |
# style | |
for theme in themes: | |
# setup theme | |
if len(theme) != 5: | |
logging.info('Skipping theme: %s - not enough colors' % theme.title) | |
continue | |
logging.info('Using theme: %s' % theme.title) | |
# enumerate through all the permutations of colors - 5! | |
for j,p in enumerate(permutations(theme.items())): | |
colors = {} | |
for i in range(5): | |
colors['color%d' % i] = p[i] | |
# style | |
for shape, styles in shapes: | |
style_shape(shape, styles, colors) | |
# export to pdf | |
exportfile = os.path.join(exportdir, '%s-%d.pdf' % (theme.title,j)) | |
doc.save(as_='Apple PDF pasteboard type', in_=exportfile) | |
logging.info('Exported: %s' % exportfile) | |
if __name__ == '__main__': | |
logging.basicConfig(level=logging.INFO) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment