Notifications

Note

Notifications aren’t available in version of OS X older than 10.8/Mountain Lion. Calling notify() on these systems will silently do nothing.

notify allows you to post notifications via OS X’s Notification Center at any time, not just at the end of your script. Furthermore, it uses your workflow’s icon, not Alfred’s.

The only functions that you should need to use directly to post notifications are notify() and possibly validate_sound().

The image-processing functions convert_image() and png_to_icns() might be useful outside of this library.

API

Post notifications via the OS X Notification Center. This feature is only available on Mountain Lion (10.8) and later. It will silently fail on older systems.

The main API is a single function, notify().

It works by copying a simple application to your workflow’s data directory. It replaces the application’s icon with your workflow’s icon and then calls the application to post notifications.

workflow.notify.convert_image(inpath, outpath, size)

Convert an image file using sips.

Parameters:
  • inpath (str) – Path of source file.
  • outpath (str) – Path to destination file.
  • size (int) – Width and height of destination image in pixels.
Raises:

RuntimeError – Raised if sips exits with non-zero status.

workflow.notify.install_notifier()

Extract Notify.app from the workflow to data directory.

Changes the bundle ID of the installed app and gives it the workflow’s icon.

workflow.notify.log()

Return logger for this module.

Returns:Logger for this module.
Return type:logging.Logger
workflow.notify.notifier_icon_path()

Return path to icon file in installed Notify.app.

Returns:Path to applet.icns within the app bundle.
Return type:unicode
workflow.notify.notifier_program()

Return path to notifier applet executable.

Returns:Path to Notify.app applet executable.
Return type:unicode
workflow.notify.notify(title=u'', text=u'', sound=None)

Post notification via Notify.app helper.

Parameters:
  • title (str, optional) – Notification title.
  • text (str, optional) – Notification body text.
  • sound (str, optional) – Name of sound to play.
Raises:

ValueError – Raised if both title and text are empty.

Returns:

True if notification was posted, else False.

Return type:

bool

workflow.notify.png_to_icns(png_path, icns_path)

Convert PNG file to ICNS using iconutil.

Create an iconset from the source PNG file. Generate PNG files in each size required by OS X, then call iconutil to turn them into a single ICNS file.

Parameters:
  • png_path (str) – Path to source PNG file.
  • icns_path (str) – Path to destination ICNS file.
Raises:

RuntimeError – Raised if iconutil or sips fail.

workflow.notify.validate_sound(sound)

Coerce sound to valid sound name.

Returns None for invalid sounds. Sound names can be found in System Preferences > Sound > Sound Effects.

Parameters:sound (str) – Name of system sound.
Returns:Proper name of sound or None.
Return type:str
workflow.notify.wf()

Return Workflow object for this module.

Returns:Workflow object for current workflow.
Return type:workflow.Workflow