How to programmatically dismiss NSAlert from runModal()? - cocoa

I have an NSAlert that I present in my Cocoa app via runModal(). I would like to unit test this behavior. Is there any way I can dismiss the alert programmatically?

See NSApplication: stopModal(), stopModal(withCode:), abortModal(), ...

Related

Swift Open Dialog closes

Is there a event in swift for when a "Open" Dialog disappears? I need to update my viewController when this happens. I am having a hard time correlating the "Open" in appDelegate to anything on the ViewController.
How are you displaying the Open dialog? Are you using the NSOpenPanel?
If so, you can just update your view after the call to run the NSOpenPanel (with runModal())

Quitting an app using the Red X button?

I've made my first Mac app and it only has one window and I want it to quit when your press the red button. This button closes the window as opposed to quitting the app at the moment and I cant seem to find a way to edit the behaviour of this button any tips? Note this is in Xcode 4 and is an Applescript based app.
I don't know AppleScriptObjC but here's how you do it in objective-c. I'm sure you can easily convert these.
Basically you don't have to modify anything. Just put this method in your app delegate. It is called automatically by the app when the last window is closed because it's an NSApplication Delegate method. There's many other NSApplication Delegate methods so you should look at those methods to see what else you can do. Good luck.
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
return YES;
}
By the way, alternative methods would have been to use NSWindowDelegate methods. You just need to set your AppDelegate (or any other class) as the window's delegate with...
[[self window] setDelegate:self];
Specifically you could have used either of these NSWindowDelegate methods to modify the behavior of the red close button. For example you could issue the terminate: method of NSApp in there to do as you asked but you can do anything in them.
- (BOOL)windowShouldClose:(id)sender
- (void)windowWillClose:(NSNotification *)notification
But applicationShouldTerminateAfterLastWindowClosed: is the one you want. I just want to show there's lots of ways to accomplish tasks.
Congrats on your first app!
The answer provided by #regulus6633 did NOT work for me. The Xcode build failed.
This is because, like the OP, my Xcode app is a "Cocoa-AppleScript" app, and the AppDelegate is written in AppleScript.
So, the solution is:
script AppDelegate
--- YOUR OTHER CODE ---
--- QUIT APP WHEN WINDOW IS CLOSED ---
on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminateAfterLastWindowClosed_
end script
I put this handler at the end of the script block, but it probably doesn't matter.
I found this solution at http://www.macscripter.net/viewtopic.php?pid=117933#p117933

Get notified when mouse is over a particular NSMenuItem

Is there a way to be notified when mouse enters and exit from a particular NSMenuItem without using a custom NSView?
I tried subclassing NSMenuItem and overriding -(BOOL)isHighlighted but it doesn't seem to work.
Have you got any hint?
Why don't you try setting a menu delegate and receiving the menu:willHighlightItem: delegate call?

Can't dismiss NSAlert in Qt app

I'm trying to display an NSAlert in a Qt application. The alert is shown using runModal, in response to some logic that's triggered by a QTimerEvent, so the stack looks like this:
[NSAlert runModal]
<.......>
timerEvent(QTimerEvent*)
<.......>
QCoreApplication::exec()
However, the alert cannot be dismissed. The buttons respond to clicks or key shortcuts, but it is never dismissed. I'm guessing it's some kind of run loop problem: the NSAlert is blocking somehow?
I can display NSAlert elsewhere in the app without problems. If I substitute a QMessageBox it works as expected. Does anyone have any ideas?

Showing a modal NSWindow, without activating the other application windows

I have an NSStatusItem that is properly displaying in the MenuBar. One of the items (when clicked) displays a modal NSWindow from my application, which is meant to perform a one-off task, then disappear. (Eg. the user enters a small bit of text, clicks "Save", and the modal NSWindow goes away.)
The issue occurs when the application is running in the background. The modal window properly appears above whatever application is running in the foreground, but when the user clicks the "Save" button, the rest of the application's windows also are made active. This is undesirable, as the user then has to click back to whatever app they were using. (Destroying the convenience of the NSStatusItem.) I'm displaying the modal window using:
[myWindow setFrame:finalRect display:YES animate:NO];
[myWindow setLevel:NSPopUpMenuWindowLevel];
[NSApp runModalForWindow:myWindow];
Is there any way to prevent clicks/events in my popup window from causing the rest of the application to become active? Or a way to let NSApp know that this particular panel shouldn't automatically activate the rest of the app? Thanks!
Instead of creating an NSWindow, create an NSPanel with the style NSNonactivatingPanelMask. You can then do the usual makeKeyAndOrderFront: and orderOut: to show/hide panel as needed.
NSApp's beginModalSessionForWindow, runModalSession, endModalSession are methods you need.
Have a look here for example how to use it:
Creating a fully customized NSAlert
A solution by Ken Thomases on the cocoa-dev list a couple years ago looks applicable here too:
[[NSApplication sharedApplication] hide:self];
[[NSApplication sharedApplication] performSelector:#selector(unhideWithoutActivation)
withObject:nil
afterDelay:0.05];
Which in theory tells the application to hide itself and unhide at the bottom of the window stack.
You could also intercept the mouse click event and use [NSApp preventWindowOrdering]
You can try something like:
...
if ([NSApp isHidden])
[myWindow makeKeyAndOrderFront:self];
else
[NSApp runModalForWindow:myWindow];
...
and when finish:
...
if ([NSApp isHidden])
[myWindow orderOut:self];
else
[NSApp stopModal];
...

Resources