Myriad Helpers: a sheet + modal alert/window ?

I have surely missed something in Myriad Helpers’ description, but is it possible to have an alert which is “as sheet” AND modal?

The idea is, when an alert in invoked into a method, I have all parameters “at hand”. If I have to receive the result button in another handler (because, unfortunately, we can’t have nested handlers), I have to resume at the point I left the situation before the alert.

And by the way, a idea to have customized icons in such alerts?

Every help welcome, thank you!

I’m not sure what you’re asking here – do you want an alert that’s attached to a window? As I understand it, all sheets are document modal, meaning you can’t click anywhere else in your window until you dismiss the sheet. As far as the icon goes, there is a setIcon: method in NSAlert.

Ric

Indeed they are, but you have to provide a handler “pointer” to get the answer back from the alert.

Reading the doc, from my beginner’s point of view, nothing seems easy, even send an alert message. I just want something like:

on doingSomethingPotentiallyDangerous
.
set answer to cautionAlert("Are you sure?","OK","Cancel","This action is irreversible.")

if answer is "OK" then
     proceed
end if
.
end

How can I do this, with this level of simplicity? Note that the action raising the alert, the alert itself and the subsequent treatment (according to the answer) are ALL included into a single handler (not in a separate handler whose job is to get rid of the alert and retrieve all the relevant data, maybe defined in the caller’s code).

If you are using the methods in Myriad Helpers, then look at the AlertTest.applescript in Shane’s app, he shows both ways to do it there. To get an attached sheet, it takes one method to launch the sheet and another to respond to it. If you want a modal alert, then you can do it all in one method.

Ric

I read (and tested) Shane’s project. That’s why I was asking : is there no possibility to execute an alert as a sheet in the same handler? If you set an alert to be modal, it appears centered on the screen, not as sheet.

I don’t think there is a way to get a sheet to run in a way that you can do it in one method – I think you will need to have code you want to run after the dismissal of the sheet in the alert done method or in a third method that you call from there. There’s nothing really wrong with doing it that way.

Ric

Ok, Ric, thank you! I just wanted to be sure… No, I suppose there is no offense to write two or three handlers to give an alert…

Or maybe to write a general-purpose twin-handler for all the cases (using a global variable):

:mad: – of course not! The global is set but the control is never returned to the caller of the alert.No way to bypass this curious manner to send an alert in the air and retrieve the answer in another part of the code. I suppose I’m not able to see the beauty in this.

No there are minimal 3 action of a sheet

  1. displaying it and defining the closing part
  2. closing it with info and return value
  3. the delegate of closing the sheet

For displaying it you can use

NSApp's beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(mySheet, theWindow, me, "sheetDidEnd:returnCode:contextInfo:", missing value)

to close it you can use

NSApp's endSheet_returnCode_(mySheet, 0) --use another number when another button is pressed; is prefer 0 for submitting

and the handler who handles after closing


on sheetDidEnd_returnCode_contextInfo_(sheet, returnCode, contextInfo)
if returnCode = 1 then
--ok button pressed
else
--cancel button pressed
end
end sheetDidEnd_returnCode_contextInfo_

DJ: In theory, those three handlers should work, but ASObjC doesn’t like contextInfo being a void* in an undeclared method, so you will actually get: Program received signal: “EXC_BAD_ACCESS”.

One workaround, which I used in the first edition of my book and is in an appendix in the second, is to define a protocol for the third method in Objective-C. This works fine, but it seemed to me that the whole process could be simplified, which is what I wrote the Myriad Helpers categories for.

Thank you DJ.

I dreamed of a:

on myMassiveDataDestruction_(sender)
    if userWarningAsSheet ("Are you really sure?", "You may regret this.",{"I'll think about it.", "Make my day."}) is "OK" then
        doSomethingIrrevocable
    end if
end myMassiveDataDestruction_

But I’ll do it the Cocoa way.:confused: