How to hook the OS X dictionary - macos

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
}

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.

OS X: Show interface of application agent (UIElement)

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;
}

how to pass parameters between cocoa applications

I have a Cocoa application (.app) and I would like to launch it from another Cocoa application, no problem here, but is there any way to launch the second application passing it some parameters ? maybe using the argv[] array in the main function?
I did this using NSWorkspace to launch the app, and NSDistributedNotificationCenter to pass the data. This obviously isn't fully developed, but it worked. One caveat from the docs -- the dictionary I sent with the argument (just a string in this example) can't be used in a sandboxed app (the dictionary must be nil).
This is in the app that opens the other app:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
ws = [NSWorkspace sharedWorkspace];
NSNotificationCenter *center = [ws notificationCenter];
[center addObserver:self selector:#selector(poster:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
[ws launchApplication:#"OtherApp.app"];
}
-(void)poster:(NSNotification *) aNote
{
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
NSDictionary *dict = [NSDictionary dictionaryWithObject:#"theDataToSend" forKey:#"startup"];
[center postNotificationName:#"launchWithData" object:nil userInfo:dict];
NSLog(#"Posted notification");
}
And this is in the app that is opened:
-(void)awakeFromNib
{
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(doStartup:) name:#"launchWithData" object:nil];
}
-(void)doStartup:(NSNotification *) aNote
{
NSLog(#"%#",aNote.userInfo);
}
How are you launching the second Cocoa app?
When I've done this, I usually communicate with the other app using AppleScript via NSAppleScript. You can launch apps that way too. Of course, the other app has to support AppleScript.
You could also use Distributed Objects if you have control over both apps, but it is more complex.
If you ever have to work with a command-line program, then NSTask is useful.

How to hide window of UIAgent process with cocoa

I have an UIAgent application with one window. I want to hide/show it from another application.How do I do it with cocoa? Seems like hide/unhide methods of NSRunningApplication doesn't affect UIAgent processes.
Thanks in advance
I solved it with NSDistributionNotifications. In the UIAgent application I add an observer to a #"QuitProcessNotification" (any other name):
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self selector:#selector(quit:)
name:#"QuitProcessNotification"
object:#"com.MyCompany.MyApp"
suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
The callback looks like that:
- (void) quit:(NSNotification *) notification
{
[NSApp terminate:nil];
}
In the main application:
Sending notification:
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:#"QuitProcessNotification"
object:#"com.MyCompany.MyApp"
userInfo: nil /* no dictionary */
deliverImmediately: YES];
Be sure, that the object parameter is indeed your sender application's bundle identifier.

Resources