« Q&A: November 4, 2001

Posted by Jeff Hunter on November 04, 2001 [Feedback (0) & TrackBack (0)]

// Question One

What is the normal way of determining which control in a window is currently selected (has input focus)? I understand how to get the firstResponder for the window, but how do I match that with a control?


The first responder is the control (or more generally, the NSView) with the input focus. If you want to see if the first responder is equal to a certain control (i.e. one of your outlets), just use ==. The only place where this doesn't work is with NSTextField. When an NSTextField is selected, the window's field editor becomes the first responder, not the text field itself.

answer by: Brian Webster on cocoa-dev mailing list.


// Question Two

I added a new menu to my main menu. In IB, I changed the name to "Search" and saved it. When I compile and run, the menu comes up as "SubMenu" If I go back into IB, it says "Search", but no matter what I do, when I run, it comes up as "SubMenu".


Have you tried cleaning the project? Sometimes you can get rid of odd errors by cleaning the project and rebuilding everything. Not always the solution, but it's usually one of the first things I do when confronted with an odd problem.

answer by: John Marriott on cocoa-dev mailing list.


// Question Three

How can I open a URL in the user's default web browser or mail client?


NSWorkspace is the class that offers this functionality. Specifically, look for:

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:myURLString]];


// Question Four

OK, but what if my app handles a custom protocol and I want it to be automatically opened when the user clicks on a URL of the form myapp:whatever.com?


You will need to register to handle the scheme by setting certain keys in your Info.plist. This is a Launch Services issue, not specific to Carbon or Cocoa, and you ought to be able to find more information in the Launch Services documentation.

answer by: Douglas Davidson on cocoa-dev mailing list.


// Question Five

How do I get Project Builder to edit HTML? Right now it is rendering the HTML instead of allowing me to modify the HTML source.


Files in your project are automatically opened in the text editor. Those that aren't in a project are opened in the HTML viewer.

answer by: Dave Ewing on projectbuilder-users mailing list.


// Question Six

My Objective-C++ Cocoa app needs to use string streams from the STL, but when I #include <sstream> Project Builder tells me that the header file was not found! What's going on here?


GCC 2.95.2 (which is the compiler underlying the Dev Tools) does not support sstreams, even though they are part of the ISO C++ standard. If you are adventurous you can try using GCC 3.0 from the Darwin CVS repository, which does include sstream support. You might also look into class strstring, which is similar to sstream, but uses char *.


// Question Seven

Is it possible to eject all or one of the mounted disks from the computer from Cocoa without using Applescript commands?


-[NSWorkspace unmountAndEjectDeviceAtPath:]

Bear in mind that mounted disks may be of many kinds -- fixed disks, removable media, Firewire, disk images, network volumes, etc. -- some of which can be unmounted and some of which cannot.

answer by: Douglas Davidson on cocoa-dev mailing list.


// Question Eight

Are there any Cocoa classes which wrap the standard BSD socket calls?


Steven Frank has written such a library. It is called SmallSockets and can be downloaded here.


// Question Nine

My new app needs to manipulate preference property lists. Is it OK for me to write directly to preference plists? What about modifying another application's preference plist?


Do not write directly to preference plists. The API for modifying preferences is CFPreferences, or at the Foundation level, NSUserDefaults. Having your app modify the preferences for someone else's app is also bad form--unless those preferences have been documented--but not nearly as bad as modifying the plists directly.

answer by: Douglas Davidson on cocoa-dev mailing list.


// Question Ten

One of the benefits of objected oriented programing is code reusability. Where can I get some object created by others that I can use in my app?


One collection of objects that is distributed is the MiscKit. You can learn more about the MiscKit here.

Also, don't forget to check out our article on the OmniFrameworks is the Tutorials section.


// Question Eleven

I'm getting tired of typing the same bits of code again and again. Is there any way to do macro expansion and/or symbol completion in Project Builder?


Objective Development has released a plugin for Project Builder which offers macro completion and enhances Project Builder's built in symbol completion. It's called CompletionDictionary and can be downloaded here.


// Question Twelve

Who's the man?


Doug Davidson is the man!

Doug Davidson
Doug Davidson, Apple employee extraordinaire.

Post a comment