0

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
Pradeep Vairamani
  • 4,004
  • 3
  • 36
  • 59

1 Answers1

2

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.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • Thanks! Is there a way to create system level modal dialogs in OSX, preferably using PyObjC? – Pradeep Vairamani Aug 05 '14 at 18:17
  • I'm not aware of any as it's not something I've tried to do. System modal dialogs are incredibly user-hostile. Why do you want to inflict something like this on other users? – Anya Shenanigans Aug 05 '14 at 18:35
  • Unfortunately that is the requirement. I am pretty sure that there is a way to do it because it is used in the Mac app for Box sync. – Pradeep Vairamani Aug 06 '14 at 05:50
  • I see no evidence in the box app of it preventing the switching to other apps. What I do see is the 'always on top' behaviour of *regular* windows. That can be accomplished by setting the level using the setLevel call of the window to `NSStatusWindowLevel`. That is *not* something that you can accomplish with the `NSAlert` API, though. – Anya Shenanigans Aug 06 '14 at 06:32