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 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.