How to hide Cocoa apps from the OSX dock

Objective-C posted 7 months ago by christian

Open Info.plist and add the following:

   1  NSUIElement=1

Tagged hide, dock, cocoa

How to capture photos/videos with iSight and Objective-C

Objective-C posted 7 months ago by christian

There are plenty of resources online that explain how to access iSight:

This article explains how to use QuartzComposer for capturing images with iSight. The benefit of using QuartzComposer is that you don’t have to write a lot of code to capture an image. All you have to do is setup a QCView with IB and bind it to an outlet, then to get the snapshot you call this method:

   1  NSImage *screenshot = [isightView snapshotImage];

Alternatively, you could also use:

   1  - (id) createSnapshotImageOfType:(NSString*)type

The code in the article didn’t work for me:

   1  NSImage *currentImage = [qcView valueForOutputKey:@"ImageOutput"]; 

The article also forgot to mention that you have to import and link to the following frameworks:

   1  #import <QuartzCore/QuartzCore.h>
   2  #import <Quartz/Quartz.h>

Haven’t tried this one yet.

This example is not complete.

Have a look at one of the demos like StillMotion

Tagged cocoa, isight, objective-c, record

How to set an application to load at login with Cocoa and Objective-C

Objective-C posted 7 months ago by christian

In XCode add the following to your controller’s header file:

   1  @interface AppController : NSObject {
   2  IBOutlet NSButton *buttonOpenAtLogin;
   3  }
   4  .
   5  .
   6  .
   7  - (IBAction)addLoginItem:(id)sender;
   8  - (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath;
   9  - (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath;

Add the following to the implementation:

   1  @implementation AppController
   2  
   3  - (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath {
   4  	// We call LSSharedFileListInsertItemURL to insert the item at the bottom of Login Items list.
   5  	LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(theLoginItemsRefs, kLSSharedFileListItemLast, NULL, NULL, thePath, NULL, NULL);		
   6  	if (item)
   7  		CFRelease(item);
   8  }
   9  
  10  - (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath {
  11  	UInt32 seedValue;
  12  	
  13  	// We're going to grab the contents of the shared file list (LSSharedFileListItemRef objects)
  14  	// and pop it in an array so we can iterate through it to find our item.
  15  	NSArray  *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(theLoginItemsRefs, &seedValue);
  16  	for (id item in loginItemsArray) {		
  17  		LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
  18  		if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr) {
  19  			if ([[(NSURL *)thePath path] hasPrefix:SGApplicationPath])
  20  				LSSharedFileListItemRemove(theLoginItemsRefs, itemRef); // Deleting the item
  21  		}
  22  	}
  23  	
  24  	[loginItemsArray release];
  25  }
  26  
  27  - (IBAction)addLoginItem:(id)sender {
  28  	CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:SGApplicationPath];
  29  	
  30  	// Create a reference to the shared file list.
  31  	LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  32  	
  33  	if (loginItems) {
  34  		if ([[buttonOpenAtLogin selectedCell] state] == YES)
  35  			[self enableLoginItemWithLoginItemsReference:loginItems ForPath:url];
  36  		else
  37  			[self disableLoginItemWithLoginItemsReference:loginItems ForPath:url];
  38  	}
  39  	CFRelease(loginItems);
  40  }
  41  .
  42  .
  43  .

In Interface Builder do the following:

  • Add a User Defaults Controller Bindings object to IB.
  • Add a checkbox button or menu item to IB
  • Bind the button to the IBOutlet in the controller
  • Select the button and bind it to the User Defaults Controller (Cmd+5). Enter values in “Controller Key” and “Model Key Path”

Code found here

See http://developer.apple.com/cocoa/cocoabindings.html for more details…

Tagged startup, login, cocoa, objective-c

Registering global hot keys with Cocoa and Objective-C

Objective-C posted 7 months ago by christian

In your application’s controller add the following:

   1  - (void)awakeFromNib
   2  {
   3  	[self registerHotKeys];
   4  }

And the following code which registers the hot keys:

   1  -(void)registerHotKeys
   2  {	
   3      EventHotKeyRef gMyHotKeyRef;
   4      EventHotKeyID gMyHotKeyID;
   5      EventTypeSpec eventType;
   6      eventType.eventClass=kEventClassKeyboard;
   7      eventType.eventKind=kEventHotKeyPressed;	
   8  	
   9      InstallApplicationEventHandler(&OnHotKeyEvent, 1, &eventType, (void *)self, NULL);
  10  	
  11      gMyHotKeyID.signature='htk1';
  12      gMyHotKeyID.id=1;
  13      RegisterEventHotKey(20, cmdKey+optionKey, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);	
  14  	
  15      gMyHotKeyID.signature='htk2';
  16      gMyHotKeyID.id=2;	
  17      RegisterEventHotKey(21, cmdKey+optionKey, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);	
  18  	
  19      gMyHotKeyID.signature='htk3';
  20      gMyHotKeyID.id=3;	
  21      RegisterEventHotKey(23, cmdKey+optionKey, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);	
  22  }
  23  
  24  OSStatus OnHotKeyEvent(EventHandlerCallRef nextHandler,EventRef theEvent,void *userData)
  25  {
  26      EventHotKeyID hkCom;
  27  	
  28      GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hkCom), NULL, &hkCom);
  29      AppController *controller = (AppController *)userData;	
  30  	
  31      int l = hkCom.id;
  32  	
  33      switch (l) {
  34          case 1: 		
  35  			NSLog(@"Capture area");	
  36  			[ScreenCapture captureArea:controller];
  37  			break;
  38          case 2: 
  39  			NSLog(@"Capture screen");	
  40  			[ScreenCapture captureScreen:controller];
  41  			break;
  42          case 3: 
  43  			NSLog(@"Capture window");	
  44  			[ScreenCapture captureWindow:controller];
  45  			break;  
  46      }
  47  
  48      return noErr;
  49  }

The code used in this snippet was inspired by this blog post http://dbachrach.com/blog/2005/11/28/program-global-hotkeys-in-cocoa-easily/

Alternatives

If you need a more complete solution you can use one of these open-source alternatives:

Showing the application window

Usually you want to show the application window when the hot key is pressed. This can be done as explained in this snippet

Tagged cocoa, hotkeys, objective-c

How to make a clickable link inside a NSTextField and Cocoa

Objective-C posted 7 months ago by christian

From How do I embed a hyperlink inside an NSTextField or NSTextView?

NSAttributedString+Hyperlink.h

   1  #import <Cocoa/Cocoa.h>
   2  
   3  @interface NSAttributedString (Hyperlink)
   4  +(id)hyperlinkFromString:(NSString*)inString withURL:(NSURL*)aURL;
   5  @end
   6  

NSAttributedString+Hyperlink.m:

   1  #import "NSAttributedString+Hyperlink.h"
   2  
   3  
   4  @implementation NSAttributedString (Hyperlink)
   5  +(id)hyperlinkFromString:(NSString*)inString withURL:(NSURL*)aURL
   6  {
   7      NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: inString];
   8      NSRange range = NSMakeRange(0, [attrString length]);
   9  	
  10      [attrString beginEditing];
  11      [attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];
  12  	
  13      // make the text appear in blue
  14      [attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];
  15  	
  16      // next make the text appear with an underline
  17      [attrString addAttribute:
  18  	 NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];
  19  	
  20      [attrString endEditing];
  21  	
  22      return [attrString autorelease];
  23  }
  24  @end
  25  

This code sets the contents of the NSTextField to a URL:

   1  #import "NSAttributedString+Hyperlink.h"
   2  
   3  [textURL setAllowsEditingTextAttributes: YES];
   4  [textURL setSelectable: YES];
   5  	
   6  NSURL* url = [NSURL URLWithString:@"http://www.apple.com"];
   7  	
   8  NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init];
   9  [string appendAttributedString: [NSAttributedString hyperlinkFromString:@"Apple Computer" withURL:url]];
  10  	
  11  [textURL setAttributedStringValue: string];

Tagged clickable, url, nstextfield, cocoa