imp
imp

Reputation: 2087

Simple pop-up balloon message on mac using python

How to make a simple pop-up balloon message on mac. I don't want to use NSUserNotification. Using python-2.7 and osx 10.8.5. POP-UP should not have any button. POP-UP should come, display message and go automatically. It should be packaged properly with py2app also.

import objc
import Foundation
import AppKit

def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
    NSUserNotification = objc.lookUpClass('NSUserNotification')
    NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
    notification = NSUserNotification.alloc().init()
    notification.setTitle_(title)
    notification.setSubtitle_(subtitle)
    notification.setInformativeText_(info_text)
    notification.setUserInfo_(userInfo)
    if sound:
        notification.setSoundName_("NSUserNotificationDefaultSoundName")
    notification.setDeliveryDate_(Foundation.NSDate.dateWithTimeInterval_sinceDate_(delay, Foundation.NSDate.date()))
    NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)


def notificationBalloon(title,msg):
    notify(title1, msg1,"", sound=False) 

Upvotes: 1

Views: 9791

Answers (1)

Peter Varo
Peter Varo

Reputation: 12150

You can use the display dialog statement in AppleScript and call the script with the subprocess module's call function.

It may seems a bit 'hackish', but since you needed a Mac only solution, I guess this is the easiest and the lightest solution you can get since you don't have to use any kind of external library or framework when you pack your project into a .app file.

enter image description here

import subprocess

applescript = """
display dialog "Some message goes here..." ¬
with title "This is a pop-up window" ¬
with icon caution ¬
buttons {"OK"}
"""

subprocess.call("osascript -e '{}'".format(applescript), shell=True)

Upvotes: 12

Related Questions