Created
July 15, 2011 02:19
-
-
Save corbinbs/1083908 to your computer and use it in GitHub Desktop.
Example Python Alert Dialogs using PyObjC (alternative to EasyDialogs?)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import objc | |
from AppKit import * | |
from Foundation import * | |
class Alert(object): | |
def __init__(self, messageText): | |
super(Alert, self).__init__() | |
self.messageText = messageText | |
self.informativeText = "" | |
self.buttons = [] | |
def displayAlert(self): | |
alert = NSAlert.alloc().init() | |
alert.setMessageText_(self.messageText) | |
alert.setInformativeText_(self.informativeText) | |
alert.setAlertStyle_(NSInformationalAlertStyle) | |
for button in self.buttons: | |
alert.addButtonWithTitle_(button) | |
NSApp.activateIgnoringOtherApps_(True) | |
self.buttonPressed = alert.runModal() | |
def alert(message="Default Message", info_text="", buttons=["OK"]): | |
ap = Alert(message) | |
ap.informativeText = info_text | |
ap.buttons = buttons | |
ap.displayAlert() | |
return ap.buttonPressed | |
def cocoa_dialogs(fn): | |
class MainRunner(object): | |
def runWithShutdown_(self, timer): | |
fn() | |
NSApp.stop_(None) | |
def run(self): | |
NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1, self, 'runWithShutdown:', "", False) | |
def wrapped(): | |
NSApplication.sharedApplication() | |
MainRunner().run() | |
NSApp.run() | |
return wrapped | |
@cocoa_dialogs | |
def main(): | |
alert("Hello!", "We'll rule the world...in Python!", ["OK"]) | |
print "I love it" | |
#big time computation | |
x = 10000 + 1 | |
alert("Some Error?", "Maybe something bad happened?", ["Oh Well", "Thanks"]) | |
print "End of the code" | |
if __name__ == "__main__": | |
main() |
Really great. Thanks!
Since alert.runModal() is called, won't that make the an alert dialog an modal ie shouldn't it require the user’s action before it can proceed? I am able to switch tabs even when the alert is shown.
Hi Experts
I am trying to run the alert dialog in Mac OSX 10.12. DMG App is bundled with python 3.4.4.
Not able to get the pop up alert dialog through logs it seems it goes till runModal() which doesnot return anything.
In Safari->Prefrences->Security the Enable Javascript is enabled and Block pop-up windows is disabled.
Any pointers on why it is getting blocked from safari, though the standalone code of alertdialog is working fine.
def trusted_dialog(self, msg, title):
from AppKit import NSAlertFirstButtonReturn
try:
title_buttons = [_("OK"), _("Cancel")]
reply = self.displayAlert(msg, "", title_buttons)
if reply == NSAlertFirstButtonReturn:
return True
else:
get_logger().warning("User has NOT trusted the origin")
except Exception as e:
get_logger().debug("MessageBoxEx exception in MacOSX: {}".format(e))
def displayAlert(self, text, title, title_buttons):
from AppKit import NSAlert, NSInformationalAlertStyle, NSApp
alert = NSAlert.alloc().init()
alert.setMessageText_(text)
alert.setInformativeText_(title)
alert.setAlertStyle_(NSInformationalAlertStyle)
for button in title_buttons:
alert.addButtonWithTitle_(button)
NSApp.activateIgnoringOtherApps_(True)
return alert.runModal()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very cool ✨ 👍 ✨