OS X: Show interface of application agent (UIElement) - macos

How do I make the interface for an application that has 'Application is agent (UIElement)' set to yes reappear?
The interface shows up the first time I start the app, but if I close the window, and the click on the app's icon then nothing happens. I guess that it's because OS X is trying to start the app again, and there is some mechanism preventing that. What I would like is this:
The first click on the app's icon should launch the app and show the interface.
If the interface has been closed down (but the app is still running in the background) a subsequent click on the icon should just show the interface.
If the interface is already shown a click on the icon should simply move the window to the foreground.

Here is a way you can do it:
1) add + initialize method to your app delegate
+ (void)initialize
{
// check if there is a running instance of your app
NSArray * apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
if ([apps count] > 1)
{
//post notification to it to update inteface
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:#"updateInterface" object:nil];
//quit current instance of the app, coz you don't need two apps running continiously
exit(0);
}
}
2) Register your app for the notification
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(updateInterface:) name:#"updateInterface" object:nil];
}
3) Add updateInterface method
- (void)updateInterface:(NSNotification *)aNotification
{
// handle your interface here
// ....
// move your app forward
[NSApp activateIgnoringOtherApps:YES];
}

I found the answer here: Closing Mac application (clicking red cross on top) and reopening by clicking dock icon.
- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
hasVisibleWindows:(BOOL)flag
{
[self.window makeKeyAndOrderFront:self];
return YES;
}

Related

Autohide Toolbar only in full screen mode in Cocoa

My goal is simple and yet I cannot find a solution in spite of lots of searching.
Basically, when my app is in full-screen (kiosk) mode, I want the toolbar only to auto-hide, but I want the menu bar hidden.
Apparently this combination is not valid. I've tried:
- (NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions: (NSApplicationPresentationOptions)proposedOptions
{
return (NSApplicationPresentationFullScreen |
NSApplicationPresentationHideDock |
NSApplicationPresentationHideMenuBar |
NSApplicationPresentationAutoHideToolbar);
}
I get the following exception:
"... fullscreen presentation options must include NSApplicationPresentationAutoHideMenuBar if NSApplicationPresentationAutoHideToolbar is included"
Thing is, I don't want the menu bar displayed at all!
So, I'm presuming this is not possible using the standard presentation options. Any ideas how I might approach implementing this behaviour manually?
I'm thinking along the lines of: detect the mouse position and only show/hide the toolbar when the mouse is at/near the top of the screen.
I'm very new to Cocoa so not sure where I would start to achieve this. Any help much appreciated!
Many thanks,
John
I've got It to work, but only by using private APIs.
First I had to find out how to prevent the menubar from appearing. I discovered the functions _HIMenuBarPositionLock and _HIMenuBarPositionUnlock, from Carbon (link the app with Carbon.framework).
Then I had to create a custom subclass of NSToolbar, at awakeFromNib I register notification observers to lock and unlock the menubar when the window enters and exits fullscreen, respectively:
- (void)awakeFromNib
{
[super awakeFromNib];
[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowWillEnterFullScreenNotification object:[self _window] queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
// lock menubar position when entering fullscreen so It doesn't appear when the mouse is at the top of the screen
_HIMenuBarPositionLock();
}];
[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowWillExitFullScreenNotification object:[self _window] queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
// unlock menubar position when exiting fullscreen
_HIMenuBarPositionUnlock();
}];
[self _setupToolbarHotspotTrackingView];
}
_setupToolbarHotspotTrackingView is a method on SOToolbar which adds a view to the window, this view will be used to track the mouse location and show/hide the toolbar accordingly.
- (void)_setupToolbarHotspotTrackingView
{
NSView *contentView = [self _window].contentView;
self.toolbarHotspotTrackingView = [[SOToolbarTrackingView alloc] initWithFrame:contentView.bounds];
[contentView addSubview:self.toolbarHotspotTrackingView];
self.toolbarHotspotTrackingView.autoresizingMask = NSViewWidthSizable|NSViewHeightSizable;
self.toolbarHotspotTrackingView.toolbar = self;
}
I also had to override _attachesToMenuBar on SOToolbar so the animation works properly.
- (BOOL)_attachesToMenuBar
{
return NO;
}
SOToolbarTrackingView sets up a tracking area for mouse moved events and checks to see if the mouse is at the top of the window. It then calls some methods on the private class NSToolbarFullScreenWindowManager to show and hide the toolbar.
There's too much stuff to explain It all in detail here, I've uploaded my experimental project so you can take a look. Download the sample project here.

How to detect when user opens the OS X Notification Center?

How do I detect when a user opens the OS X Mountain Lion Notification Center?
Is there an NSNotification (ugh, very similar term for a different thing) which I can observe?
I don't know of any officially documented solution or notification (let me know!), but this appeared to work (at least on OS X 10.10) when I tested it, so long as my application was in the foreground/had the frontmost window I believe.
Add your object as an observer:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationCenterOpened:) name:#"com.apple.HIToolbox.beginMenuTrackingNotification" object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationCenterClosed:) name:#"com.apple.HIToolbox.endMenuTrackingNotification" object:nil];
Add methods similar to the following to your object, making sure to check for the correct ToolboxMessageEventData number (4927), for example:
- (void)notificationCenterOpened:(NSNotification*)notification {
if ([notification.userInfo[#"ToolboxMessageEventData"] isEqual: #4927]) {
NSLog(#"Notification center opened");
}
}
- (void)notificationCenterClosed:(NSNotification*)notification {
if ([notification.userInfo[#"ToolboxMessageEventData"] isEqual: #4927]) {
NSLog(#"Notification center closed");
}
}
Let me know if that does or doesn't work for you.
Nevermind - upon restart/log-off + log back in, the ToolboxMessageEventData appears to change.

How to hook the OS X dictionary

on osx lion, you can control-command-d or triple-tap on a word that your mouse is pointed to in any app to launch a popover dictionary. i want to make an app to track the words a user is looking up in the dictionary.
how do i observe the event where the user does control-command-d or triple-tap to launch the popover dictionary?
I understand that the specific API for this is HIDictionaryWindowShow.
You can use popoverDidShow:
- (void)awakeFromNib {
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:#selector(popoverDidShow:)
name:NSPopoverDidShowNotification object:nil];
}
// dictionary is shown or another NSPopover
- (void)popoverDidShow:(NSNotification*)notify {
//your code
}

Toggle Keyboard Viewer on Mac OS X programmatically

I created a cocoa app which has a window with a text field to get user input, a small keyboard-icon button to bring up the keyboard viewer. When the user clicks OK or Cancel button to finish, i want to hide the keyboard viewer. What I've done is as follows:
//action for keyboard-icon button
-(IBAction)input:(id)sender
{
[self toggleKeyboard:YES];
}
//action for Cancel button
-(IBAction)cancel:(id)sender
{
[self toggleKeyboard:NO];
[NSApp abortModal];
[[self window] orderOut: self];
}
//action for OK button
-(IBAction)ok:(id)sender
{
[self toggleKeyboard:NO];
[NSApp stopModal];
[[self window] orderOut: self];
}
-(void)toggleKeyboard:(BOOL)show
{
NSDictionary *property = [NSDictionary dictionaryWithObject:(NSString*)kTISTypeKeyboardViewer
forKey:(NSString*)kTISPropertyInputSourceType];
NSArray *sources = (NSArray*)TISCreateInputSourceList((CFDictionaryRef)property, false);
TISInputSourceRef keyboardViewer = (TISInputSourceRef)[sources objectAtIndex:0];
if (show == YES)
{
TISSelectInputSource(keyboardViewer);
}
else
{
TISDeselectInputSource(keyboardViewer);
}
CFRelease((CFTypeRef)sources);
}
I can launch keyboard viewer successfully, but it cannot be hidden by TISDeselectInputSource at all times.
It is possible to programmatically hide the Keyboard Viewer. I made an AppleScript for my media center a little while ago that will toggle it. I'm not going to include the bit that shows it, partly because you've already figured that out, but mostly because the way I did it was to call some executable that I can't remember where I got it from.
Anyway, here's the AppleScript:
tell application "System Events"
if exists (process "Keyboard Viewer") then
click (process "Keyboard Viewer"'s window 1's buttons whose subrole is "AXCloseButton")
end if
end tell
This uses Accessibility, so you need to have "Enable access for assistive devices" turned on in order for it to work.
Note this same thing can be accomplished in a Objective-C / C++ app using the Accessibility API, which you could use to avoid having to call into AppleScript in your app.

Multiwindows problem, cocoa

I have a simple application, not document-based. I want to have a login window that allows people to login or add a user, and when they logged in successfully I want it to load the main page. If from the main page you click log out, it should destroy the main page and take you back to login page.
sounds like a simple plan, but for some reason I have a problem.
The way I have it right now, I check if the customer logged in or not in the main file AppDelegate and load different window controller. When customer logs in, I send a notification back to AppDelegate from Login Conntroller and load another window controller for main window.
Something like this:
if([[settings get:#"isLoggedIn"] isEqualToString:#"Yes"])
{
MainController *tmpMainController = [[MainController alloc] initWithWindowNibName:#"MainWindow"];
self.mainController = tmpMainController;
NSWindow *mainWindow = [tmpMainController window];
[mainWindow makeKeyAndOrderFront:self];
[tmpMainController release];
} else {
LoginController *tmpViewController = [[LoginController alloc] initWithWindowNibName:#"LoginWindow"];
self.loginController = tmpViewController;
loginWindow = [tmpViewController window];
[loginWindow makeKeyAndOrderFront:self];
[tmpViewController release];
}
Everything works fine, it displays the correct window. But the weird part happens when I log out from the main page, log in again and log out again. If I do it several times, instead of showing me 1 login window, it draws 2. If I continue the login process, on the second try I get 2 main windows. If I log out again, I see 4 cascade login windows, then I see 5 or 7 main windows. After all windows gets loaded all extra windows start getting destroyed one-by-one. It looks like when new window gets created it draws all old windows, then the new one and then destroys all old ones. I don't know why it happens. Would like some help.
Here is the code from my main controller when customer clicks log out:
-(IBAction)logOutClick:(id) sender
{
[settings set:#"isLoggedIn" value:#"No"];
[[self window] orderOut:self];
[[NSNotificationCenter defaultCenter] postNotificationName:#"NSUserLoggedOutNotification" object: self userInfo: nil];
}
the same thing for login controller:
if ([users verifyUser]) {
[settings set:#"isLoggedIn" value:#"Yes"];
[loginView removeFromSuperview];
[[self window] orderOut:self];
[[NSNotificationCenter defaultCenter] postNotificationName:#"NSUserLoggedInNotification" object: self userInfo: nil];
}
I have "Released when closed" checked off for both windows.
I added new nsnotification center observer every time I log out.
That was the problem.

Resources