1

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) 
pnuts
  • 58,317
  • 11
  • 87
  • 139
imp
  • 1,967
  • 2
  • 28
  • 40
  • 2
    What have you tried so far? Also, I love the "It should be packaged properly with py2app also" part. Surely you would want to do *some* of the work yourself? – Taylan Aydinli Nov 19 '13 at 07:06
  • @vape I am trying a lot from last 8 days. NSUserNotification has issues with delegate setting in python for overriding the frontmost application. I also looked into https://wiki.python.org/moin/GuiProgramming but didn't found any helpful thing. – imp Nov 19 '13 at 07:13
  • Do you not want to use NSUserNotification because you can't figure out how to set a delegate? – Glyph May 12 '17 at 06:03

1 Answers1

11

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)
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
  • POP-UP should come, display message and go automatically. So no need for button. Moreover it is showing error 0:1: syntax error: A ‚Äú/‚Äù can‚Äôt go here. (-2740) – imp Nov 19 '13 at 09:06
  • I'm in Mac OS X 10.9 and using Python 3 and works for me -- try to remove the `¬` symbols and make a long one line string and try to run that. – Peter Varo Nov 19 '13 at 09:08
  • how about trying to `import os` and then use: `os.system("osascript -e '{}'".format(applescript))` instead of `subprocess`? – Peter Varo Nov 19 '13 at 09:16
  • applescript = """display dialog "Some message goes here..." with title "This is a pop-up window" with icon caution buttons {"OK"}""" os.system("osascript -e '{}'".format(applescript)) same 0:112: execution error: No user interaction allowed. (-1713) error. – imp Nov 19 '13 at 09:21
  • Sir I will call pop-up function many times in code when want to display notification. It should come and go, so there is no need for button, as it will stop the code workflow until clicked. – imp Nov 19 '13 at 09:24
  • 1
    okay, let's try something else: open your terminal.app, and copy-paste this line, and then press enter: `osascript -e 'display dialog "Some message goes here..." with title "This is a pop-up window" with icon caution buttons {"OK"}'` – Peter Varo Nov 19 '13 at 09:24
  • well, if you don't need any buttons, and you only want to notify the user, then use the notification center instead a "full-blown dialog window": http://stackoverflow.com/a/17651702/2188562 – Peter Varo Nov 19 '13 at 09:27
  • File "", line 1 osascript -e 'display dialog "Some message goes here..." with title "This is a pop-up window" with icon caution buttons {"OK"}' error. I am facing issue in setting delegate in NSUsernotification center for overriding the foremost app – imp Nov 19 '13 at 09:38
  • please suggest a python library for pop-up. I checked this https://wiki.python.org/moin/GuiProgramming. But didn't get the desired thing – imp Nov 19 '13 at 09:40
  • then your issue is not related to python but to your system settings/privileges! anyway, you could use the `tkinter` module, but your goal is to inform the user, not to interact with the user with this so called *pop-up*, so I strongly recommend you the notification center. good luck! – Peter Varo Nov 19 '13 at 09:46
  • Is informing the user through pop-up can be done through any other python module/library. Please suggest. I tried a lot with notification center but facing issues with it. – imp Nov 19 '13 at 09:58
  • @PeterVaro 's answer works(if you do `sudo gem install terminal-notifier` in terminal), but I cannot get any of the "full-blown dialogues" to work. :( – dylnmc Aug 09 '14 at 01:22
  • @vy32 if you really want to update this very old answer, you should use the finalised `subprocess.run` over the `subprocess.call`. In which case it is not only the code that's need to be updated but the paragraphs above it. – Peter Varo Mar 27 '20 at 09:11
  • @PeterVaro: I didn't want to rewrite it to circa 2020 python, I wanted to fix the problematic style. Creating a quoted string that needs to be split apart and spinning up a shell is not just wasteful, it's error prone, because if your inner string contains a single-quote, then the parsing of the shell will break for no apparent reason. It's unfortunate that you put it back. You're leaving a trap for junior programmers. – vy32 Mar 27 '20 at 13:58
  • @vy32 1. It will not break if the inner string contains a single-quote if you follow the shell's escaping rules, e.g. on bash the following works fine: `call("printf 'hello '\"'\"'world'\"'\"'\n'", shell=True)` (in this specific case, I'd rather use triple quotes instead of escaped ones, but that's beyond the point now). 2. As I said: I'm happy if an edit really fixes something, in this case, uses `subprocess.run`. Otherwise it does not add too much value to the answer -- especially because the proposed changes have nothing to do with the correctness of the answer to the given question. – Peter Varo Mar 30 '20 at 15:17
  • So you really expect the caller to know about shell quoting rules? Is it really that important to spend the CPU and memory to create a single string, and then create a shell, just so the shell can parse it and break it down into atoms? I really do not understand why you want to use the shell. It's inefficient and it can cause quoting problems. What is your reason for wanting to use the shell? – vy32 Mar 31 '20 at 00:51
  • I don't and I can't recall I ever said otherwise. My cheeky comment tried to prove that "if your inner string contains a single-quote, then the parsing of the shell will break" is simply not true. But that wasn't the real point I wanted make. As I said, if you like to update this 7 years old answer then **do it right**: use the correct standard lib function and whilst you're doing so you might as well not invoke the shell. (If you're really worried about allocations & CPU cycles, you're using the wrong language though!) – Peter Varo Apr 01 '20 at 12:31