Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rumps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 28 additions & 1 deletion rumps/rumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down