How to close in code About window

Hello,
after wrapping my app with App Wrapper I can call the app’s About window with this code (provided by Sam in App Wrapper):

[code]declare function NSClassFromString lib “Foundation” ( className as CFstringRef ) as Ptr
declare function sharedApplication lib “AppKit” selector “sharedApplication” ( classRef as Ptr ) as Ptr
declare sub orderFrontStandardAboutPanel lib “AppKit” selector “orderFrontStandardAboutPanel:” (class_id as Ptr)
orderFrontStandardAboutPanel( sharedApplication( NSClassFromString( “NSApplication” ) ) )

Return True[/code]

Now, how do I close it in code? In fact, if the File menu contains a “File Close” menuitem associated to any visible window, selecting it “that” window (obviously) closes, but the About window remains visible.
And if no window is visible, the File Close menuitem is (obviously) dimmed.

A workaround (not yet tested) could be to keep the File Close menuitem disabled/dimmed if its associated window is not window(0); but that would not solve the issue.

And trying to trap it in a windowCount loop, I do not know how to detect it:

for i as integer = 0 to windowCount -1 if window(i) isa what? then //close the About window exit end if next

Suggestions welcome. Thanks.

What code in FileClose ?

Window(0).Close

for i as integer = windowCount -1 downto 0
window(i).close//all windows close except the About window
next

The About window, even when frontmost, never closes, unless by clicking its red button.
And window(i).title returns “untitled”.

Edit: By the way, not even App Wrapper closes its About window when selecting File > Close.
I guess a special declare is needed.

Carlo, have you tried to follow the instructions there: orderfrontstandardaboutpanel ?

I’ve made some more searches, but I do not found how to add a window reference (title) to the About window, etc.

Where is the easy to read / understand / implement Inside Mac books ?

I had a look at it, but since I’m not a declare-man, I just trust Sam and his code.
I do not think that my inability to close it by code depends on Sam’s code. Creating and closing the About window are two different beasts.

Okay; By using a declare to present the Apple About Box; the Xojo framework doesn’t know about this window, which is why Xojo doesn’t even see this window.

So we need to manage the close event also using declares.

  1. In your menu, unselect “AutoEnable” property of your FileClose menu item.
  2. In the “EnableMenuItems” event of your application object, add the following code.

[code]#if targetMacOS then
declare function NSClassFromString lib “AppKit” ( className as CFStringRef ) as integer
declare function NSApplicationSharedApplication lib “AppKit” selector “sharedApplication” ( classRef as integer ) as integer
declare function NSApplicationKeyWindow lib “AppKit” selector “keyWindow” ( NSApplicationInstance as integer ) as integer

if NSApplicationKeyWindow( NSApplicationSharedApplication( NSClassFromString( “NSApplication” ) ) ) <> 0 then FileClose.enable
#endif[/code]
3. Use the following code for the “FileClose” menu handler in your App object.

[code]Function FileClose() As Boolean
#if targetMacOS then
declare function NSClassFromString lib “AppKit” ( className as CFStringRef ) as integer
declare function NSApplicationSharedApplication lib “AppKit” selector “sharedApplication” ( classRef as integer ) as integer
declare function NSApplicationKeyWindow lib “AppKit” selector “keyWindow” ( NSApplicationInstance as integer ) as integer

Dim cApp as integer = NSApplicationSharedApplication( NSClassFromString( "NSApplication" ) )
Dim kw as integer = NSApplicationKeyWindow( cApp )

if kw <> 0 then
  declare sub NSWindowPerformClose lib "AppKit" selector "performClose:" ( NSWindowInstance as integer, sender as integer )
  NSWindowPerformClose( kw, cApp )
end if

#endif

Return True
End Function
[/code]

This solution mimics Apple’s App template solution for closing windows.

Please note; We’re completely bypassing the Xojo framework for this; while in my quick tests, I’ve not noticed any problems, there is the potential for some to arise. If you experience any issues in closing windows using this code, immediately disable it.

Also this doesn’t cover Windows or Linux, for those platforms you’ll need to use an alternative solution.

Wonderful. Thank you, Sam.

BTW: I have tested with your declares several apps. At present no problem.
I noticed that if for a particular window its own (Xojo) fileclose function is needed, then the Apple-fileclose declares are skipped and the Xojo-fileclose code is executed. In my particular case I refactored the code in order to let the declares take charge.
Thanks again.