Friday, December 15, 2017

#1 2017-04-13 10:11:20 pm

xmaslance
Member
Registered: 2017-04-05
Posts: 15

Implementing sheets with beginSheet:completionHandler

Shane has mentioned in a couple of past threads that there is a bug that prevents using beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo with an actual didEndSelector argument. (All my attempts result in a crash due to EXC_BAD_ACCESS.)

Since beginSheet:modalForWindow... has been deprecated, I thought I'd try beginSheet:completionHandler. Even here, I get the same crash when specifying a completionHandler. Is this due to a bug as well, or am I doing something wrong? (It works fine if completionHandler is "missing value".)

Applescript:


on clickShowSheet_(sender)
   tell theWindow to beginSheet_completionHandler_(theSheet, "endSheetCompletionHandler:")
end clickShowSheet_

on clickCloseSheet_(sender)
   set returnCode to current application's NSModalResponseOK
   tell theWindow to endSheet_returnCode_(theSheet, returnCode)
end clickCloseSheet_

on endSheetCompletionHandler_(returnCode)
   tell theSheet to orderOut_(me)
end endSheetCompletionHandler_

Thanks!
Lance

Offline

 

#2 2017-04-13 10:23:14 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Implementing sheets with beginSheet:completionHandler

xmaslance wrote:

I thought I'd try beginSheet:completionHandler. Even here, I get the same crash when specifying a completionHandler.


That doesn't mean a handler in the AppleScript sense -- it requires a block, which is essentially an inline function. You can't use blocks in AppleScriptObjC.

You can get roughly the same functionality if you use my Myriad Helpers, the class categories of which use beginSheet:completionHandler:.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2017-04-14 10:35:12 am

xmaslance
Member
Registered: 2017-04-05
Posts: 15

Re: Implementing sheets with beginSheet:completionHandler

Makes sense, thanks.

Is it safe to say then that sheets work just fine in vanilla AppleScript as long as I don't need a completion handler? I can live with that, it just means I have to remember to call endSheet() and orderOut() together, instead of being able to consolidate the orderOut() calls into a completion handler.

Which, looking at Myraid Helpers, I notice that orderOut() is called in a completion handler only when using showOver() with a timeout. I assume then that when using the showOver() without a timeout, I have to call orderOut() myself alongside endSheet()?

Offline

 

#4 2017-04-14 06:39:54 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Implementing sheets with beginSheet:completionHandler

xmaslance wrote:

Is it safe to say then that sheets work just fine in vanilla AppleScript as long as I don't need a completion handler?


They work fine with the deprecated methods.


Which, looking at Myraid Helpers, I notice that orderOut() is called in a completion handler only when using showOver() with a timeout.


The rules differ when using a completionHandler: block.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2017-04-14 09:04:24 pm

xmaslance
Member
Registered: 2017-04-05
Posts: 15

Re: Implementing sheets with beginSheet:completionHandler

Shane Stanley wrote:

They work fine with the deprecated methods.


I've had no issues (so far) with using the new beginSheet too as long as completionHandler is set to "missing value".

Shane Stanley wrote:

The rules differ when using a completionHandler: block.


So is calling orderOut not always necessary after endSheet? The documentation for NSAlert's beginSheetModalForWindow explicitly mentions, "Note that orderOut: no longer needs to be called in the completion handler. If you don’t dismiss the alert, it will be done for you after the completion handler finishes." But, I haven't run across any other hints about when orderOut is necessary or not.

Offline

 

#6 2017-04-15 12:27:44 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Implementing sheets with beginSheet:completionHandler

The documentation means what it says.

To be honest, I think worrying about this sort of deprecation is needless. There are degrees of deprecation, and the delegate methods have not been hard deprecated.

Last edited by Shane Stanley (2017-04-15 12:54:12 am)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2017-04-15 04:05:46 pm

xmaslance
Member
Registered: 2017-04-05
Posts: 15

Re: Implementing sheets with beginSheet:completionHandler

No worries from me. I'm mostly using the new beginSheet because it has fewer arguments smile If the deprecated beginSheet provided any advantage, I'd be happy to use it instead. My conclusion, in short, is if you need sheet completion handlers, your Myriad Helpers provides the easiest way to get them. Thanks!

Last edited by xmaslance (2017-04-15 04:06:29 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)