Search code examples
objective-cmacospyobjcnsalert

Behaviour of NSAlert with runModal


I am new to osx programming. I am using pyobjc to create the alerts. My understanding of Modal windows or dialogs is that modal windows require the user’s action before they can proceed. However, if I use runModal of NSAlert, I am still able to goto other apps while the alert is still shown. Is my understanding of modal dialogs incorrect.

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)
        alert.setAlertStyle_(NSCriticalAlertStyle)
        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

Solution

  • You would not be able to swap to any other apps if the modal dialog was a system modal dialog. In the case of your app, it prevents you from proceeding any further in the user interface of your own application, not in other applications.

    In the case of your code, you're creating an application-modal dialog, which is as described in the NSAlert Documentation.