Tag: cocoa

Getting desktop path for current user on OS X

I use NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES ); NSString* theDesktopPath = [paths objectAtIndex:0]; It works well. But when I launch the application with sudo it gives the root’s desktop path. Is there any way to return current user’s desktop always (even if the app is started with sudo)?

Growing NSTextView to fit contents is clipping last line of text

I’m trying to create an NSTextView that grows vertically as the user types and scrolls once the height has reached a maximum. This is similar to the text view in Messages works. My first attempt uses the delegate to listen for text changes and adjust the height constraint associated with the NSTextView’s scroll view: – […]

Get window values under mouse

I am playing with Cocoa/Objective-C and I would like to ask you if it is possible to get window information such as pid, window name from a window that is inactive. What I precisely mean is, in the case there are two non full screen (nor maximized) windows A and B of different tasks, lets […]

NSURLCache crashes with autoreleased objects, but leaks otherwise

CSURLCache is designed to cache resources for offline browsing, as NSURLCache only stores data in-memory. If cachedResponse is autoreleased before returning the application crashes, if not, the objects are simply leaked. Any light that could be shed onto this would be much appreciated. Please note stringByEncodingURLEntities is a category method on NSString. @interface CSURLCache : […]

firstResponder in NSViewController

I’ve got two classes. ManagingViewController, a subclass of NSViewController, and ViewController, a subclass auf ManagingViewController. In Viewcontroller I’ve got a NSTextField which I want to become the firstResponder, but I didn’t manage that. So it is nearly the same like the Chapter 29 in Hillegass’ book Cocoa Programming for Mac OS X (Download of the […]

Creating a custom NSViewController without a nib

I recently started learning Objective-C and I’ve run into a slight problem. I’m trying to use a custom view controller without a nib, so the view is created in the code. The view controller itself is created in the AppDelegate. When I run the program, it first displays a default empty window. After I close […]

beginSheet: block alternative with ARC?

Mike Ash created an example of using blocks to handle callbacks from sheets, which seems very nice. This was in turn updated to work with garbage collection by user Enchilada in another SO question at beginSheet: block alternative?, see below. @implementation NSApplication (SheetAdditions) – (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow didEndBlock:(void (^)(NSInteger returnCode))block { [self beginSheet:sheet modalForWindow:docWindow […]

NSView initialization: -init: vs. -awakeFromNib:

I have a simple class MyView inherited from NSView and instance variable NSImage * image; in it. Class functionality is to draw image on the view. However, in -drawRect: image instance always equal nil, if it was initialized in -init: function and not nil if it was initialized in -awakeFromNib:. Log shows both -init: and […]

Using NSProgressIndicator inside an NSMenuItem

I’m trying to use a NSProgressIndicator (indeterminate) inside of a statusbar-menu. I’m using an NSView-object as view for the menuitem, and then subviews the progress indicator to display it. But whenever i try to call the startAnimation: for the progress, nothing happens. When i try do the very same thing on a normal NSWindow it […]

Fetched Property in Core Data

In my core data model, a Person has one or more Cars, specified by the unordered to-many relationship ‘Cars’. Frequently, I need to retrieve a Person’s cars ordered by datePurchased, or by dateLastUsed. Until now, I have been adding my own method to Person for carsByDatePurchased. This uses a sort descriptor to sort the NSSet […]