I'm want to create a menu for a status bar item like the one seen in Tapbot's PastebotSync application:
Does anyone have any ideas how to achieve the custom area at the top of the menu which is flush with the top?
I've tried/thought of a few potential ways of doing it:
Standard NSMenuItem with a view - isn't flush with the top of the menu
Some hack-ish code to place an NSWindow over the area at the top of the menu - not great as it doesn't fade out nicely with the menu when it closes
Abandoning an NSMenu entirely and using an NSView instead - haven't tried this yet but I don't really want to have to make some fake buttons or something that act as NSMenuItems
Anyone have any better ideas or suggestions?
Thanks!
In case anyone comes looking, I posted a solution to this at Gap above NSMenuItem custom view
Here's the code:
#interface FullMenuItemView : NSView
#end
#implementation FullMenuItemView
- (void) drawRect:(NSRect)dirtyRect
{
NSRect fullBounds = [self bounds];
fullBounds.size.height += 4;
[[NSBezierPath bezierPathWithRect:fullBounds] setClip];
// Then do your drawing, for example...
[[NSColor blueColor] set];
NSRectFill( fullBounds );
}
#end
Use it like this:
CGFloat menuItemHeight = 32;
NSRect viewRect = NSMakeRect(0, 0, /* width autoresizes */ 1, menuItemHeight);
NSView *menuItemView = [[[FullMenuItemView alloc] initWithFrame:viewRect] autorelease];
menuItemView.autoresizingMask = NSViewWidthSizable;
yourMenuItem.view = menuItemView;
I had the same need in early versions of HoudahSpot 2. I did get it working with one limitation: my code leaves the menu with square corners at the bottom.
I have since abandonned this setup, as the BlitzSearch feature in HoudahSpot grew to need a complexer UI, I ran into other limitations with using NSViews in a NSMenu.
Anyway, here is the original code taking care of those extra 3 pixels:
- (void)awakeFromNib
{
HIViewRef contentView;
MenuRef menuRef = [statusMenu carbonMenuRef];
HIMenuGetContentView (menuRef, kThemeMenuTypePullDown, &contentView);
EventTypeSpec hsEventSpec[1] = {
{ kEventClassMenu, kEventMenuCreateFrameView }
};
HIViewInstallEventHandler(contentView,
NewEventHandlerUPP((EventHandlerProcPtr)hsMenuCreationEventHandler),
GetEventTypeCount(hsEventSpec),
hsEventSpec,
NULL,
NULL);
}
#pragma mark -
#pragma mark Carbon handlers
static OSStatus hsMenuContentEventHandler( EventHandlerCallRef caller, EventRef event, void* refcon )
{
OSStatus err;
check( GetEventClass( event ) == kEventClassControl );
check( GetEventKind( event ) == kEventControlGetFrameMetrics );
err = CallNextEventHandler( caller, event );
if ( err == noErr )
{
HIViewFrameMetrics metrics;
verify_noerr( GetEventParameter( event, kEventParamControlFrameMetrics, typeControlFrameMetrics, NULL,
sizeof( metrics ), NULL, &metrics ) );
metrics.top = 0;
verify_noerr( SetEventParameter( event, kEventParamControlFrameMetrics, typeControlFrameMetrics,
sizeof( metrics ), &metrics ) );
}
return err;
}
static OSStatus hsMenuCreationEventHandler( EventHandlerCallRef caller, EventRef event, void* refcon )
{
OSStatus err = eventNotHandledErr;
if ( GetEventKind( event ) == kEventMenuCreateFrameView)
{
err = CallNextEventHandler( caller, event );
if ( err == noErr )
{
static const EventTypeSpec kContentEvents[] =
{
{ kEventClassControl, kEventControlGetFrameMetrics }
};
HIViewRef frame;
HIViewRef content;
verify_noerr( GetEventParameter( event, kEventParamMenuFrameView, typeControlRef, NULL,
sizeof( frame ), NULL, &frame ) );
verify_noerr( HIViewFindByID( frame, kHIViewWindowContentID, &content ) );
HIViewInstallEventHandler( content, hsMenuContentEventHandler, GetEventTypeCount( kContentEvents ),
kContentEvents, 0, NULL );
}
}
return err;
}
Sorry, I forgot that bit:
- (MenuRef) carbonMenuRef
{
MenuRef carbonMenuRef = NULL;
if (carbonMenuRef == NULL) {
extern MenuRef _NSGetCarbonMenu(NSMenu *);
carbonMenuRef = _NSGetCarbonMenu(self);
if (carbonMenuRef == NULL) {
NSMenu *theMainMenu = [NSApp mainMenu];
NSMenuItem *theDummyMenuItem = [theMainMenu addItemWithTitle: #"sub" action: NULL keyEquivalent: #""];
if (theDummyMenuItem != nil) {
[theDummyMenuItem setSubmenu:self];
[theDummyMenuItem setSubmenu:nil];
[theMainMenu removeItem:theDummyMenuItem];
carbonMenuRef = _NSGetCarbonMenu(self);
}
}
}
if (carbonMenuRef == NULL) {
extern MenuRef _NSGetCarbonMenu2(NSMenu *);
carbonMenuRef = _NSGetCarbonMenu2(self);
}
return carbonMenuRef;
}
Related
I get bad tendinitis from clicking the mouse all day.
In the past I used Karabiner to remap the fn key to simulate a left mouse button. However it doesn't work with Sierra.
I tried to accomplish this in Cocoa, and it correctly performs mouse-down/up when I press and release fn.
However it doesn't handle double-click / triple-click.
Also when dragging, (e.g. dragging a window, or selecting some text) nothing happens visually until I key-up, whereupon it completes.
How can I adapt my code to implement this?
First I create an event tap:
- (BOOL)tapEvents
{
_modifiers = [NSEvent modifierFlags];
if ( ! _eventTap ) {
NSLog( #"Initializing an event tap." );
// kCGHeadInsertEventTap -- new event tap should be inserted before
// any pre-existing event taps at the same location,
_eventTap = CGEventTapCreate( kCGHIDEventTap, // kCGSessionEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
CGEventMaskBit( kCGEventKeyDown )
| CGEventMaskBit( kCGEventFlagsChanged )
| CGEventMaskBit( NSSystemDefined )
,
(CGEventTapCallBack)_tapCallback,
(__bridge void *)(self));
if ( ! _eventTap ) {
NSLog(#"unable to create event tap. must run as root or "
"add privlidges for assistive devices to this app.");
return NO;
}
}
CGEventTapEnable( _eventTap, YES );
return [self isTapActive];
}
Now I implement the callback:
CGEventRef _tapCallback(
CGEventTapProxy proxy,
CGEventType type,
CGEventRef event,
Intercept* listener
)
{
//Do not make the NSEvent here.
//NSEvent will throw an exception if we try to make an event from the tap timout type
#autoreleasepool {
if( type == kCGEventTapDisabledByTimeout ) {
NSLog(#"event tap has timed out, re-enabling tap");
[listener tapEvents];
return nil;
}
if( type != kCGEventTapDisabledByUserInput ) {
return [listener processEvent:event];
}
}
return event;
}
Finally I implement a processEvent that will pass through any event apart from fn key up/down, which will get converted to left mouse up/down:
- (CGEventRef)processEvent:(CGEventRef)cgEvent
{
//NSLog( #"- - - - - - -" );
NSEvent* event = [NSEvent eventWithCGEvent:cgEvent];
//NSLog(#"%d,%d", event.data1, event.data2);
//NSEventType type = [event type];
NSUInteger m = event.modifierFlags &
( NSCommandKeyMask | NSAlternateKeyMask | NSShiftKeyMask | NSControlKeyMask | NSAlphaShiftKeyMask | NSFunctionKeyMask );
NSUInteger flags_changed = _modifiers ^ m;
_modifiers = m;
switch( event.type ) {
case NSFlagsChanged:
{
assert(flags_changed);
//NSLog(#"NSFlagsChanged: %d, event.modifierFlags: %lx", event.keyCode, event.modifierFlags);
if( flags_changed & NSFunctionKeyMask ) {
bool isDown = _modifiers & NSFunctionKeyMask;
CGEventType evType = isDown ? kCGEventLeftMouseDown : kCGEventLeftMouseUp;
CGPoint pt = [NSEvent mouseLocation];
CGPoint mousePoint = CGPointMake(pt.x, [NSScreen mainScreen].frame.size.height - pt.y);
CGEventRef theEvent = CGEventCreateMouseEvent(NULL, evType, mousePoint, kCGMouseButtonLeft);
CGEventSetType(theEvent, evType);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);
//return theEvent;
}
break;
}
}
_lastEvent = [event CGEvent];
CFRetain(_lastEvent); // must retain the event. will be released by the system
return _lastEvent;
}
EDIT: Performing a double click using CGEventCreateMouseEvent()
EDIT: OSX assign left mouse click to a keyboard key
Karabiner now works on macOS 10.12 and later.
I've this task, from my application i need to kill another my application, the problem is that the other application has a Termination Confirm Dialog (there is no critical data to save, only confirmation of user intent to quit).
On 10.6+ you will use:
bool TerminatedAtLeastOne = false;
// For OS X >= 10.6 NSWorkspace has the nifty runningApplications-method.
if ([NSRunningApplication respondsToSelector:#selector(runningApplicationsWithBundleIdentifier:)]) {
for (NSRunningApplication *app in [NSRunningApplication runningApplicationsWithBundleIdentifier:#"com.company.applicationName"]) {
[app forceTerminate];
TerminatedAtLeastOne = true;
}
return TerminatedAtLeastOne;
}
but on <10.6 this commonly used Apple Event:
// If that didn‘t work either... then try using the apple event method, also works for OS X < 10.6.
AppleEvent event = {typeNull, nil};
const char *bundleIDString = "com.company.applicationName";
OSStatus result = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication, typeApplicationBundleID, bundleIDString, strlen(bundleIDString), kAutoGenerateReturnID, kAnyTransactionID, &event, NULL, "");
if (result == noErr) {
result = AESendMessage(&event, NULL, kAENoReply|kAEAlwaysInteract, kAEDefaultTimeout);
AEDisposeDesc(&event);
}
return result == noErr;
can't Force Quit!!!
So what can you use?
You can use this simple code that I've digged out on cocoabuilder:
// If that didn‘t work then try shoot it in the head, also works for OS X < 10.6.
NSArray *runningApplications = [[NSWorkspace sharedWorkspace] launchedApplications];
NSString *theName;
NSNumber *pid;
for ( NSDictionary *applInfo in runningApplications ) {
if ( (theName = [applInfo objectForKey:#"NSApplicationName"]) ) {
if ( (pid = [applInfo objectForKey:#"NSApplicationProcessIdentifier"]) ) {
//NSLog( #"Process %# has pid:%#", theName, pid ); //test
if( [theName isEqualToString:#"applicationName"] ) {
kill( [pid intValue], SIGKILL );
TerminatedAtLeastOne = true;
}
}
}
}
return TerminatedAtLeastOne;
How can I get the position of my application's dock icon using the Accessibility API?
Found it! Using this forum post as reference, I was able to shape the given sample code to what I needed:
- (NSArray *)subelementsFromElement:(AXUIElementRef)element forAttribute:(NSString *)attribute
{
NSArray *subElements = nil;
CFIndex count = 0;
AXError result;
result = AXUIElementGetAttributeValueCount(element, (CFStringRef)attribute, &count);
if (result != kAXErrorSuccess) return nil;
result = AXUIElementCopyAttributeValues(element, (CFStringRef)attribute, 0, count, (CFArrayRef *)&subElements);
if (result != kAXErrorSuccess) return nil;
return [subElements autorelease];
}
- (AXUIElementRef)appDockIconByName:(NSString *)appName
{
AXUIElementRef appElement = NULL;
appElement = AXUIElementCreateApplication([[[NSRunningApplication runningApplicationsWithBundleIdentifier:#"com.apple.dock"] lastObject] processIdentifier]);
if (appElement != NULL)
{
AXUIElementRef firstChild = (__bridge AXUIElementRef)[[self subelementsFromElement:appElement forAttribute:#"AXChildren"] objectAtIndex:0];
NSArray *children = [self subelementsFromElement:firstChild forAttribute:#"AXChildren"];
NSEnumerator *e = [children objectEnumerator];
AXUIElementRef axElement;
while (axElement = (__bridge AXUIElementRef)[e nextObject])
{
CFTypeRef value;
id titleValue;
AXError result = AXUIElementCopyAttributeValue(axElement, kAXTitleAttribute, &value);
if (result == kAXErrorSuccess)
{
if (AXValueGetType(value) != kAXValueIllegalType)
titleValue = [NSValue valueWithPointer:value];
else
titleValue = (__bridge id)value; // assume toll-free bridging
if ([titleValue isEqual:appName]) {
return axElement;
}
}
}
}
return nil;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
AXUIElementRef dockIcon = [self appDockIconByName:#"MYAPPNAME"];
if (dockIcon) {
CFTypeRef value;
CGPoint iconPosition;
AXError result = AXUIElementCopyAttributeValue(dockIcon, kAXPositionAttribute, &value);
if (result == kAXErrorSuccess)
{
if (AXValueGetValue(value, kAXValueCGPointType, &iconPosition)) {
NSLog(#"position: (%f, %f)", iconPosition.x, iconPosition.y);
}
}
}
}
As for Mac OS El Capitan, looks like you aren't supposed to get the position of the icon using Accessibility API. The matter is that the icon isn't located in accessibility objects hierarchy of the app—it can be found in the hierarchy of the system Dock application. A sandboxed app isn't supposed to access the accessibility objects of other apps.
The code in approved answer doesn't yield any warnings of the sandboxd daemon in the console, looks like it doesn't violate any rules. It creates the top-level accessibility object with the function AXUIElementCreateApplication. The documentation states, that it:
Creates and returns the top-level accessibility object for the
application with the specified process ID.
Unfortunately, this top-level object is not the ancestor of the Dock icon.
I've tried to run the code, and it calculates the position of the first app's main menu item (which has the same title as the app itself). The comparison takes place in this line:
if ([titleValue isEqual:appName]) {
So the output was always the same for my app:
position: (45.000000, 0.000000)
An attempt to access the other app's accessibility object yielded a warning in console. I guess another way to calculate the position of the icon has to be found.
I want to trap, modify and divert all the keydown/keyup events in the system within my cocoa app. I know about CGEventTapCreate but, didn't found any working code from net.
Thanks
Found Solution:
self.machPortRef = CGEventTapCreate(kCGSessionEventTap,
kCGTailAppendEventTap,
kCGEventTapOptionDefault,
CGEventMaskBit(kCGEventKeyDown),
(CGEventTapCallBack)eventTapFunction,
self);
if (self.machPortRef == NULL)
{
printf("CGEventTapCreate failed!\n");
} else {
self.eventSrc = CFMachPortCreateRunLoopSource(NULL, self.machPortRef, 0);
if ( self.eventSrc == NULL )
{
printf( "No event run loop src?\n" );
}else {
CFRunLoopRef runLoop = CFRunLoopGetCurrent(); //GetCFRunLoopFromEventLoop(GetMainEventLoop ());
// Get the CFRunLoop primitive for the Carbon Main Event Loop, and add the new event souce
CFRunLoopAddSource(runLoop, self.eventSrc, kCFRunLoopDefaultMode);
}
}
Properties :
CFMachPortRef machPortRef;
CFRunLoopSourceRef eventSrc;
Event Handler:
CGEventRef eventTapFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
//printf("eventTap triggered\n");
return event;
}
This question already has answers here:
Register as Login Item with Cocoa?
(7 answers)
Closed 9 years ago.
Just wondering how I can make my app open automatically at login, but make this be able to be toggled on and off using a check box in the preferences window.
Here's some code that I use, it's based on the Growl source.
+ (BOOL) willStartAtLogin:(NSURL *)itemURL
{
Boolean foundIt=false;
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
UInt32 seed = 0U;
NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
for (id itemObject in currentLoginItems) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef URL = NULL;
OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, /*outRef*/ NULL);
if (err == noErr) {
foundIt = CFEqual(URL, itemURL);
CFRelease(URL);
if (foundIt)
break;
}
}
CFRelease(loginItems);
}
return (BOOL)foundIt;
}
+ (void) setStartAtLogin:(NSURL *)itemURL enabled:(BOOL)enabled
{
OSStatus status;
LSSharedFileListItemRef existingItem = NULL;
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
UInt32 seed = 0U;
NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
for (id itemObject in currentLoginItems) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef URL = NULL;
OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, /*outRef*/ NULL);
if (err == noErr) {
Boolean foundIt = CFEqual(URL, itemURL);
CFRelease(URL);
if (foundIt) {
existingItem = item;
break;
}
}
}
if (enabled && (existingItem == NULL)) {
LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst,
NULL, NULL, (CFURLRef)itemURL, NULL, NULL);
} else if (!enabled && (existingItem != NULL))
LSSharedFileListItemRemove(loginItems, existingItem);
CFRelease(loginItems);
}
}
If you want an easy to implement checkbox, make a #property BOOL startAtLogin; in one of your classes and implement it as follows. Just bind the checkbox value to the property and it should all work seamlessly.
- (NSURL *)appURL
{
return [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
}
- (BOOL)startAtLogin
{
return [LoginItem willStartAtLogin:[self appURL]];
}
- (void)setStartAtLogin:(BOOL)enabled
{
[self willChangeValueForKey:#"startAtLogin"];
[LoginItem setStartAtLogin:[self appURL] enabled:enabled];
[self didChangeValueForKey:#"startAtLogin"];
}
There is a decent description of what to do at CocoaDev.
Basically, you'll want to use the API in LaunchServices/LSSharedFileList.h if you can target Mac OS X 10.5 or later. Before 10.5 there was no clean API, so you have to manually manipulate the login items (Sample code at the Developer Connectiong).
Here's the sample code(dead) for Leopard I mentioned in the comments. Found via this blog post. The code you need to enable or disable startup at login is in Controller.m.
Call the method pasted below with a file URL pointing at your application to add it to the current user's login items.
To disable again, you'll need to get that same loginListRef, convert it to an array, and iterate through it until you find the item with the url you want to disable. Finally, call LSSharedFileListItemRemove with the appropriate arguments.
Good luck :)
- (void)enableLoginItemWithURL:(NSURL *)itemURL
{
LSSharedFileListRef loginListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginListRef) {
// Insert the item at the bottom of Login Items list.
LSSharedFileListItemRef loginItemRef = LSSharedFileListInsertItemURL(loginListRef,
kLSSharedFileListItemLast,
NULL,
NULL,
(CFURLRef)itemURL,
NULL,
NULL);
if (loginItemRef) {
CFRelease(loginItemRef);
}
CFRelease(loginListRef);
}
}
See also SO question: Register as login item with cocoa