Accessing Apple Help from C++ Code (Carbon)
The third/final step in implementing Apple Help is programmatically displaying the main contents or individual topics within your help pages. This, of course, assumes that you have already created a set of HTML help pages for Apple Help and then indexed and integrated these files into your Xcode project.
Accessing Apple Help from C++ using Carbon is fundamentally simple. The Apple Help Reference contains only 4 functions (plus one constant enumeration that is no longer used). There is a little bit of necessary mucking about with bundle references and whatnot that may be unfamiliar to programmers who have not worked with Mac development before, but it is all fairly straightforward.
In order to access Apple Help from the application, you first register your help files using the AHRegisterHelpBook() function. This only needs to be done once, ideally during program initialization, and we do it something like this:
bool RegisterHelp ( void ) { CFBundleRef const bundle = CFBundleGetMainBundle ( ); if ( !bundle ) return false; CFURLRef const location = CFBundleCopyBundleURL ( bundle ); if ( !location ) return false; FSRef file; if ( !CFURLGetFSRef ( location, &file ) ) { CFRelease ( location ); return false; } _Error = AHRegisterHelpBook ( &file ); CFRelease ( location ); if ( _Error ) return false; return true; }
(Additional validity checking and error reporting has been stripped from this sample code for clarity.)
In the above example, most of the code is just to obtain the FSRef (file system reference) of the application bundle from which the help is to be displayed, and it assumes that you want help for the current application. (Obviously, you could instead pass a reference to any other bundle and access a different help system, but that would be unusual.)
Once the help files are registered, you can display the main help page using the AHGotoPage() function (with default parameters), which we accomplish with a function similar to this:
void ShowHelpContents ( void ) { CFBundleRef const bundle = CFBundleGetMainBundle ( ); if ( !bundle ) return; CFStringRef const key = CFSTR( "CFBundleHelpBookName" ); CFTypeRef const book = CFBundleGetValueForInfoDictionaryKey ( bundle, key ); if ( !book ) return; CFStringRef const help = (CFStringRef)book; _Error = AHGotoPage ( help, NULL, NULL ); }
(Additional validity checking and error reporting has been stripped from this sample code for clarity.)
You can also display a specific help topic by using the AHLookupAnchor() function, for which we use something like this:
void ShowHelpTopic ( CFStringRef topic ) { if ( !topic ) return; CFBundleRef const bundle = CFBundleGetMainBundle ( ); if ( !bundle ) return; CFStringRef const key = CFSTR( "CFBundleHelpBookName" ); CFTypeRef const book = CFBundleGetValueForInfoDictionaryKey ( bundle, key ); if ( !book ) return; CFStringRef const help = (CFStringRef)book; _Error = AHLookupAnchor ( help, topic ); }
(Additional validity checking and error reporting has been stripped from this sample code for clarity.)
In both of these last two examples, the function reads the CFBundleHelpBookName string from the main application bundle and uses that string for the name of the help system, passed as the first parameter to the appropriate Apple Help function. (You could, of course, hard code this string, but if you were thinking that this is a good idea, shame on you.)
Accessing Apple Help from Objective-C (Cocoa)
If you are using Objective-C and Cocoa, much of the help system handling is done behind the scenes (which is, in part, why some programmers find Objective-C to be objectionable). If you follow the steps in the previous two posts (Part 1 and Part 2) to insert your Apple Help into a Cocoa project, your main help system will work automatically.
The main help page in a Cocoa application is accessible by the user through the ‘Help->[ApplicationName] Help’ menu option by default. If you wish to bring up the main page from your Objective-C code, you may need to reference an anchor on that page (see below) or use the Carbon method described above.
To access a specific help topic (anchor) from Objective-C and Cocoa, this is done through the shared NSHelpManager object using the openHelpAnchor:inBook: method. For example, to open the ‘rules’ topic, you would use the following code:
NSString *book = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleHelpBookName"]; [[NSHelpManager sharedHelpManager] openHelpAnchor:@"rules" inBook:book];
The documentation for NSHelpManager is quite clear that all of its functionality is simply a number of wrappers around the functions described in the first section of this post, so you could always use those functions directly, if desired.
There you have it. At this point, you should know enough to properly implement Apple Help in your Carbon or Cocoa application for Mac OS X. The process is a little convoluted to suss out from the documentation, so I hope this provides a decent jump start.