Add an Apple Help Book to your macOS app

From Free Pascal wiki
Revision as of 06:20, 28 December 2021 by Trev (talk | contribs) (→‎See also: New section)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en)

No professional macOS application should be without a functioning Apple Help Book. Unfortunately Apple make it harder than it should be for developers to create an Apple Help Book because the official documentation is significantly out of date (2013 anyone?) and the Apple Help Viewer has undergone significant change in recent versions of the operating system. For the full details on how to add an Apple Help Book to your application, along with sample code, read on.

Application Bundle Layout

A typical application bundle with an Apple Help Book bundle should resemble this layout:

+-- MyApp.app/                                                  [application bundle]
     |
     +-- Contents/
           |
           +-- MacOS/
           |     |
           |     +-- MyApp                                      [application executable]
           +-- Resources/
           |     |
           |     +-- MyApp.icns
           |     |
           |     +-- MyApp.help/                                [Help Book bundle]
           |           |
           |           +-- Contents/
           |                 |
           |                 +-- Resources/
           |                 |     |                               
           |                 |     +-- en.lproj/                [for English]
           |                 |     |     |
           |                 |     |     +-- help.helpindex     [generated by the macOS help indexer hiutil]
           |                 |     |     |
           |                 |     |     +-- index.html         [table of contents - do not index]
           |                 |     |     |
           |                 |     |     +-- page1.html
           |                 |     |     |
           |                 |     |     +-- ...
           |                 |     |     |
           |                 |     |     +-- pageN.html
           |                 |     |     |
           |                 |     |     +-- InfoPlist.strings  [see Localizing an Info.plist]
           |                 |     |
           |                 |     +-- fr.lproj/                [for French] 
           |                 |     |     |
           |                 |     |     +-- help.helpindex     [generated by the macOS help indexer hiutil]
           |                 |     |     |
           |                 |     |     +-- index.html         [table of contents - do not index]
           |                 |     |     |
           |                 |     |     +-- page1.html
           |                 |     |     |
           |                 |     |     +-- ...
           |                 |     |     |
           |                 |     |     +-- pageN.html
           |                 |     |     |
           |                 |     |     +-- InfoPlist.strings 
           |                 |     |
           |                 |     +-- images/          
           |                 |     |     |
           |                 |     |     +-- chip3_32x32.ico    [icon used in help pages]
           |                 |     
           |                 +-- Info.plist                     [Help Book plist]
           |
           +-- Info.plist                                       [application plist]

Main Menu

If you have not already done so, you need to add a TMainMenu component to your application form, and create a TMenuItem with a caption name of "Help". macOS will then automatically add a Search menu option to your Help menu when you run your application.

You also need to add a menu item to your Help menu to open your application help file at its table of contents. Once you have added the My App Help menu item, create an OnClick event handler containing the following code:

...
Uses
  LCLIntf, MacOSAll;
...
function GetResourcesPath(): string;
var
  pathRef: CFURLRef;
  pathCFStr: CFStringRef;
  pathStr: shortstring;
begin
  pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
  pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
  CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());

  Result := pathStr + '/Contents/Resources/';
end;

procedure TForm1.MenuItem_MyAppHelpClick(Sender: TObject);
var
  helpPath: String;
begin
  helpPath := GetResourcesPath + 'MyApp.help';
  OpenDocument(helpPath);
end;
Note-icon.png

Tip: If you wish to NOT have macOS automatically add a Search option to your Help menu then, when you create the Help menu item, add a trailing space after Help in the menu item's caption property and the Search option will not show up in your application's Help menu.

Application Info.plist

One of the most important files is the application Info.plist. Two keys need to be added or Spotlight will not index your help file. If your help file is not indexed, then even trying to open it will result in a perplexing "Not available" error page in the Apple Help Viewer. If you are using Xcode, then add these keys.

 Help Book directory name
 Help Book identifier

The Help Book directory name should refer to the Help Book bundle name (eg MyApp.help). This directory should be located in the application bundle Contents/Resources directory. The Help Book identifier is simply the name of your help (eg My App Help).

If you are editing the Info.plist file in a text editor, then add these keys and their value strings:

 <key>CFBundleHelpBookFolder</key>
 <string>myapp.help</string>
 <key>CFBundleHelpBookName</key>
 <string>My App Help</string>

Help Book Info.plist

Below is a basic Help Book Info.plist for an application called "My App".

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>en</string>
	<key>CFBundleIdentifier</key>
	<string>org.sentry.myapp.help</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundlePackageType</key>
	<string>BNDL</string>
	<key>CFBundleShortVersionString</key>
	<string>22</string>
	<key>CFBundleSignature</key>
	<string>hbwr</string>
	<key>CFBundleVersion</key>
	<string>22</string>
	<key>HPDBookAccessPath</key>
	<string>index.html</string>
	<key>HPDBookIconPath</key>
	<string>images/chip3_32x32.ico</string>
	<key>HPDBookIndexPath</key>
	<string>help.helpindex</string>
	<key>HPDBookTitle</key>
	<string>My App Help</string>
	<key>HPDBookType</key>
	<string>3</string>
	<key>HPDBookUsesExternalViewer</key>
	<false/>
</dict>
</plist>

Creating Help Book pages

Apple Help Book pages are simply HTML files. You need to create an index.html page which is the table of contents for your application Help Book. You also need to ensure that this file is not indexed or it will show up in the Apple Help Viewer as a blank entry. There is also usually little point indexing pages which just contain links to other pages. You do this by adding the noindex meta tag to the file. Here is an example index.html page for "My App".

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>My App Table of Contents</title>
  <meta name="robots" content="noindex"> 
</head>
	
<body class="Normal">

  <H1><img style="margin: 0.0px 12.0px 0.0px 0.0px;" height="32.0" src="../images/chip3_32x32.ico" width="32.0" />Table of Contents</H1>

  <ul>
      <li id="Page1"><a href="Page1.html">Introduction</a></li>
      <li id="Page2"><a  href="Page2.html">Combo Boxes</a></li>
  </ul>
	  
</body>
</html>

A sample page 1 for "My App" is set out below. Note:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Introduction</title>
        <meta name="robots" content="index, anchors" />
        <meta name="description" content="My App Introduction" />
        <meta name="keywords" content="introduction, operating system" />
    </head>
    <body>
        <a name="Introduction"></a>
        <H1><img style="margin: 0.0px 12.0px 0.0px 0.0px;" height="32.0" src="../images/chip3_32x32.ico" width="32.0" />Introduction</H1>
	<p>My App is a very useful application for all users of all ages!</p>
        <p>The macOS version of My App has been tested on macOS Sierra 10.12.6 , macOS High Sierra 10.13.6 and macOS Mojave 10.14.5.</p>
        <p>The Windows version of My App has been tested on Windows XP, Windows 7 and Windows 10.</p>

        <p>The latest version of the My App application is available from the web site at <a href="https://www.sentry.org/">www.sentry.org</a>.</p>
    </body>
</html>

Once you have created the HTML pages for your Help Book, you need to index them. You do this from a Terminal using the macOS command line utility called hiutil. The man page (man hiutil in an Applications > Utilities > Terminal) details all the information you may ever need to know. A simple example to create an index file and include anchors:

  hiutil -Caf help.helpindex .

This will index all the HTML files in the current directory and create the index file named helpindex.help in the same directory (do not omit the trailing full stop which signifies the current directory). It will also index any help anchors should you use them for context-sensitive help; note that macOS applications rarely, if ever, implement context-sensitive help.

An example of a help anchor is included in the sample page 1 HTML file above; specifically the: <a name="Introduction"></a> line.

Also note the <meta name="robots" content="index, anchors" /> line which ensures that the anchors are indexed along with the file content.

Updating cached Help Books

macOS caches an Apple Help Book after first use which can be perplexing when you are developing a Help Book and do not see your changes. The way to overcome this is to run the following shell script from a Terminal after a change:

#!/bin/sh
killall helpd
rm -rf ~/Library/Caches/com.apple.help*
rm -rf ~/Library/Preferences/com.apple.help*

Note: those are tildes (~) and not dashes (-) before /Library; this only affects the current user's Apple Help cache and Apple Help preferences, not the system-wide ones.

Sample MyApp with Help Book

The Apple Help Book facility can be frustrating the first time you set out to create an Apple Help Book. In an effort to make it a little easier, you can download a minimal "My App" example (blank form with Main Menu and Help Menu) on which the above examples have been based. Note: the first time you run MyApp.app and choose the Help Menu, no entries will show in the Search if you start typing "my app" apart from the menu items. Give it a few seconds and delete the "my app" in the search box and the two sample entries should show up in a few seconds after you again type "my".

MyAppMenu.png

Compiled MyApp: Download (2.2MB).

Full project source code is available from SourceForge.

See also