public

  • Download Gist
export-current-omnigraffle-canvas.py
Python
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 85
#!/usr/bin/env python
 
# Exports a current omnigraffle canvas into a file.
#
# A file `.<DocumentName>.omnigraffle_export` placed next to the
# `<DocumentName>.omnigraffle`, can contain a directory relative to the document
# directory where the exports should be saved. If no such a file is found it
# will always prompt for a location.
 
import os
import sys
import omnigraffle
 
from AppKit import NSApplication, NSAutoreleasePool, NSApp, NSSavePanel, NSFileHandlingPanelOKButton
import objc
 
log = None
 
def log_growl(msg):
import gntp.notifier
gntp.notifier.mini(msg)
 
def log_std(msg):
from AppKit import NSAlert, NSInformationalAlertStyle
alert = NSAlert.alloc().init()
alert.autorelease()
alert.setAlertStyle_(NSInformationalAlertStyle)
alert.setMessageText_(msg)
alert.runModal()
 
try:
# using Growl to notify about the results
# requires gntp
# install using pip install gntp
import gntp.notifier
log = log_growl
except Exception:
# otherwise notify using
log = log_std
 
og = omnigraffle.OmniGraffle()
schema = og.active_document()
 
schema_path = schema.path
schema_fname = os.path.basename(schema_path)
schema_dir = os.path.dirname(schema_path)
 
target_path = None
format = 'pdf'
canvas_name = schema.active_canvas_name()
 
# Try to look for a file .<DocumentName>.omnigraffle_export
export_info_fname = os.path.join(schema_dir, '.' + schema_fname[0:schema_fname.rindex('.')] + '.omnigraffle_export')
if os.path.exists(export_info_fname):
# if it exists it should contain one line that gives a relative path to a
# directory where to export
with open(export_info_fname) as f:
target_dir = f.read().strip()
target_dir = os.path.join(schema_dir, target_dir)
target_dir = os.path.normpath(target_dir)
target_path = os.path.join(target_dir, canvas_name + '.' + format)
else:
# otherwise ask for a path
savePanel = NSSavePanel.savePanel()
savePanel.setTitle_("Save %s as" % canvas_name)
savePanel.setDirectoryURL_(schema_dir)
savePanel.setCanCreateDirectories_(True)
savePanel.setExtensionHidden_(False)
savePanel.setNameFieldStringValue_(canvas_name+'.pdf')
NSApplication.sharedApplication().activateIgnoringOtherApps_(True);
 
if savePanel.runModal() == NSFileHandlingPanelOKButton:
target_path = savePanel.URL().path()
format = target_path[target_path.rindex('.')+1:]
 
if not target_path:
sys.exit(0)
 
try:
schema.export(canvas_name, target_path, format=format)
log("Exported %s to: %s as: %s" % (canvas_name, target_path, format))
except Exception as e:
log("Unable to export: %s to %s. %s" % (canvas_name, target_path, e.message))
 
NSApplication.sharedApplication().terminate_(None)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.