Better said; poor wording from my side or I may have taken the ASS->ASObjC conversion too literal. Anonymous C functions (known as blocks in Objective-C) are a no-go in ASObjC and will never be supported by technicality. The only way without help from 3rd party code is using the deprecated NSApp method.
Another difference is that the ASS sheet is application based, which means it can be queued. Only the NSApp’s deprecated method can be queued and is identical to ASS implementation, not the NSWindow method. They’re technically not the same even if they carry the same name (and behave differently).
There tons of UI libraries available with alternatives that are close or identical to the ASS solutions but a 1:1 conversion without 3rd party code is impossible. Therefore my statement “sheets are deprecated and shouldn’t be used any more.” is still valid for an out-of-the-box ASObjC solution.
p.s. I’ll edit my previous topic to make it more clearer.
You don’t actually need the block; you can put any continuation code in the buttons’ action handlers.
For alerts it’s similar:
on showAlertSheet:sender
set theAlert to current application's NSAlert's alloc()'s init()
tell theAlert
its setMessageText:"An alert"
its setInformativeText:"Blah blah blah"
set okButton to its addButtonWithTitle:"OK"
okButton's setAction:"doOK:"
okButton's setTarget:me
set cancelButton to its addButtonWithTitle:"Cancel"
cancelButton's setAction:"doCancel:"
cancelButton's setTarget:me
its beginSheetModalForWindow:theWindow completionHandler:(missing value)
end tell
end showAlertSheet:
on doOK:sender
theWindow's endSheet:(sender's |window|())
-- do whatever
end doOK:
on doCancel:sender
theWindow's endSheet:(sender's |window|())
-- do whatever
end doCancel:
That said, Myriad Helpers makes the whole process so much simpler, I’m not sure why anyone would bother with the hard way.
For sure – the approach has to be quite different. And it’s made more complicated by the fact that these days there’s no simple way to force an update to the UI for the duration of a method that handles an event.
Download Myriad Helpers and add the NSWindow+MyriadHelpers files to your project.
Find the CustomSheetTest.applescript file and add a property like this:
property textField : missing value
And change the showCustomSheet: handler to this:
on showCustomSheet:sender -- triggered by button in window
customWindow's showOver:mainWindow
repeat with i from 1 to 10
(textField's performSelectorOnMainThread:"setStringValue:" withObject:("Progress " & i) waitUntilDone:true)
delay 1
end repeat
mainWindow's endSheet:customWindow
end showCustomSheet:
Now in IB go to MainMenu.xib, control-click on the Custom Sheet Test object, and connect the textField outlet to the text field on the Custom Window, where it says “Custom sheet”.
Run, click Show More, then click Show custom sheet.
Hi,
I would like to know how to close a sheet Window after finishing a process, for example I am uploading a photo and a sheet Window is displayed saying “Wait for the photo to load” so when you finish loading the photo this sheet Window can close without the need to hit the “OK” button or any other.
I use this:
set alert to current application's NSAlert's alloc's init()
tell alert
its setMessageText:""
its setInformativeText: ""
its setAlertStyle:2
its setShowsSuppressionButton:false
its beginSheetModalForWindow:theWindow modalDelegate:me didEndSelector:(missing value) contextInfo:(missing value)
end tell
Thanks @Shane Stanley, working like a charm!
one more question:
Is there any way to insert a progress bar or an indeterminate progress indicator, if so how would it be done?
Is there any way to launch the App with a certain window and after a few seconds this window will close automatically and open a second window? if so how could i do?
I know I can use
orderOut_ (me)
to hide and
makeKeyAndOrderFront_ (me)
to show another window.
but how can i close the first automatically after a few seconds?
It’s not code – you create a new window in Xcode. Add whatever buttons you want, and connect them to handlers that close the sheet. Then have your main window call beginSheet:completionHandler:, passing an outlet to the sheet window and missing value.