diff --git a/rumps/__init__.py b/rumps/__init__.py index a0930d9..755187e 100644 --- a/rumps/__init__.py +++ b/rumps/__init__.py @@ -23,4 +23,4 @@ __copyright__ = 'Copyright 2017 Jared Suttles' from .rumps import (separator, debug_mode, alert, notification, application_support, timers, quit_application, timer, - clicked, notifications, MenuItem, Timer, Window, App) + clicked, notifications, MenuItem, Timer, Window, App, show_image) diff --git a/rumps/rumps.py b/rumps/rumps.py index 092cd1e..67277fb 100644 --- a/rumps/rumps.py +++ b/rumps/rumps.py @@ -14,7 +14,8 @@ from Foundation import (NSDate, NSTimer, NSRunLoop, NSDefaultRunLoopMode, NSSearchPathForDirectoriesInDomains, NSMakeRect, NSLog, NSObject) -from AppKit import NSApplication, NSStatusBar, NSMenu, NSMenuItem, NSAlert, NSTextField, NSImage + +from AppKit import (NSApplication, NSStatusBar, NSMenu, NSMenuItem, NSAlert, NSTextField, NSSecureTextField, NSImage, NSImageView) from PyObjCTools import AppHelper import inspect @@ -171,6 +172,10 @@ def notification(title, subtitle, message, data=None, sound=True): notification_center.scheduleNotification_(notification) +def show_image(image): + return _nsimage_window_from_file(image) + + def application_support(name): """Return the application support folder path for the given `name`, creating it if it doesn't exist.""" app_support_path = os.path.join(NSSearchPathForDirectoriesInDomains(14, 1, 1).objectAtIndex_(0), name) @@ -214,6 +219,28 @@ def _nsimage_from_file(filename, dimensions=None, template=None): image.setTemplate_(template) return image +def _nsimage_window_from_file(filename, dimensions=None, template=None): + """Take a path to an image file and return an NSImage object.""" + try: + _log('attempting to open image at {0}'.format(filename)) + with open(filename): + pass + except IOError: # literal file path didn't work -- try to locate image based on main script path + try: + from __main__ import __file__ as main_script_path + main_script_path = os.path.dirname(main_script_path) + filename = os.path.join(main_script_path, filename) + except ImportError: + pass + _log('attempting (again) to open image at {0}'.format(filename)) + with open(filename): # file doesn't exist + pass # otherwise silently errors in NSImage which isn't helpful for debugging + + image = _nsimage_from_file(filename, dimensions, template) + if image: + window = NSImageView.alloc().init(image) + return window + def _require_string(*objs): for obj in objs: