Monday, December 17, 2018

#1 2018-08-07 08:33:20 am

spherico
Member
Registered: 2018-07-13
Posts: 6

Progress Panel as sheet

I'm trying to convert an old ASS app to ASobjC.

Seems that many things aren't that easy (for me) as they used to be be.

The old code is:

Applescript:


set pPanel to window "progressPanel"
tell pPanel
   set textField to text field 1
   set content of textField to ("Converting titles") as string
end
   display panel window "progressPanel" attached to window "main"
   set i to 0
   repeat with titlePath in pathNameList
       set content of textField to ("Converting title " & i & " of " & nodeCount) as string
       ...
   end
       close panel window "progressPanel"

or

Applescript:

   set startUpPanel to (window "startUpPanel")
   tell startUpPanel
       set background color to dark_WindowColor
       center
       set toolName to (text field "startUpToolName")
       set (content of toolName) to "Start Check"
       tell progress indicator 1 to start
       set visible to true
   end tell
   
   repeat
           set (content of toolName) to "A"
           ...
           set (content of toolName) to "B"
           ...

   end

   tell progress indicator 1 of startUpPanel to stop
   hide startUpPanel

I can't find any working examples :-(

Offline

 

#2 2018-08-07 09:19:22 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2808
Website

Re: Progress Panel as sheet

It can be a pain when you're from ASS and move to ASObjC. While ASS was always trying to keep it's syntax AppleScript style and also the object's design strict to AS designs, ASObjC is more like an alien within AppleScript which makes it very difficult at first.

The 1:1 ASObjC code of ASS sheet is the NSApp's beginSheet: method. Showing an window/panel as sheet is not an window task but technically an application task. The method to show the window is beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: (reed more…).

However (NSApp) sheets are deprecated and shouldn't be used any more. If you want to update from ASS to ASObjC I wouldn't use old deprecated code and go for an normal modal window.

Last edited by DJ Bazzie Wazzie (2018-08-07 08:50:13 pm)

Offline

 

#3 2018-08-07 05:59:07 pm

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

Re: Progress Panel as sheet

DJ Bazzie Wazzie wrote:

However sheets are deprecated and shouldn't be used any more.



I think you might be mis-reading the documentation -- sheets are not deprecated. What is deprecated is showing them using NSApplication's beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: -- the recommended way is to use NSWindow's beginSheet:completionHandler: method.

The problem for scripters, though, is that the latter uses blocks, which can't be used in AppleScript.

There are two possible solutions. One is to keep using the old method. It's a soft deprecation more about promoting the new approach, IMO, and I'd be very surprised if it went away anytime soon, or at least without considerable warning.

The other solution is to use my Myriad Helpers here:

https://www.macosxautomation.com/apples … lpers.html

It has an Objective-C category called NSWindow+MyriadHelpers that wraps the new method in code that makes it suitable (and simple) to use from AppleScript:

Applescript:

theSheet's showOver:mainWindow

There's also a similar NSAlert+MyriadHelpers category for alerts.

It's just a matter of dragging the relevant .h and .m files into the Xcode project.


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

Offline

 

#4 2018-08-07 08:47:51 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2808
Website

Re: Progress Panel as sheet

Shane Stanley wrote:

I think you might be mis-reading the documentation



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.

Offline

 

#5 2018-08-07 09:27:50 pm

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

Re: Progress Panel as sheet

DJ Bazzie Wazzie wrote:

Therefore my statement "sheets are deprecated and shouldn't be used any more." is still valid for an out-of-the-box ASObjC solution.



Really, it's not. If you want to show a window as a sheet, you can still use the block-based API, just passing missing value for the block:

Applescript:

mainWindow's beginSheet:customWindow completionHandler:(missing value)

You don't actually need the block; you can put any continuation code in the buttons' action handlers.

For alerts it's similar:

Applescript:

   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.


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

Offline

 

#6 2018-08-08 06:22:42 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2808
Website

Re: Progress Panel as sheet

It can't be queued and completion handler was used in ASS so it's not the same as ASS sheets.

Last edited by DJ Bazzie Wazzie (2018-08-08 06:28:30 am)

Offline

 

#7 2018-08-08 06:42:49 am

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

Re: Progress Panel as sheet

DJ Bazzie Wazzie wrote:

It can't be queued



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.


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

Offline

 

#8 2018-08-08 07:06:54 am

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

Re: Progress Panel as sheet

spherico wrote:

I can't find any working examples :-(



Download Myriad Helpers and add the NSWindow+MyriadHelpers files to your project.

Find the CustomSheetTest.applescript file and add a property like this:

Applescript:

   property textField : missing value

And change the showCustomSheet: handler to this:

Applescript:

   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.


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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)