Sign in Join
Wiki copytemporary

Add an Apple Help Book to your macOS app: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11*(macOS only)*
22
33No 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.
44
55## Application Bundle Layout
66
77A typical [[Application Bundle|application bundle]] with an Apple Help Book bundle should resemble this layout:
88
99`+-- MyApp.app/                                                  [application bundle]`\
1010`     |`\
1111`     +-- Contents/`\
1212`           |`\
1313`           +-- MacOS/`\
1414`           |     |`\
1515`           |     +-- MyApp                                      [application executable]`\
1616`           +-- Resources/`\
1717`           |     |`\
1818`           |     +-- MyApp.icns`\
1919`           |     |`\
2020`           |     +-- MyApp.help/                                [Help Book bundle]`\
2121`           |           |`\
2222`           |           +-- Contents/`\
2323`           |                 |`\
2424`           |                 +-- Resources/`\
2525`           |                 |     |                               `\
2626`           |                 |     +-- en.lproj/                [for English]`\
2727`           |                 |     |     |`\
2828`           |                 |     |     +-- help.helpindex     [generated by the macOS help indexer hiutil]`\
2929`           |                 |     |     |`\
3030`           |                 |     |     +-- index.html         [table of contents - do not index]`\
3131`           |                 |     |     |`\
3232`           |                 |     |     +-- page1.html`\
3333`           |                 |     |     |`\
3434`           |                 |     |     +-- ...`\
3535`           |                 |     |     |`\
3636`           |                 |     |     +-- pageN.html`\
3737`           |                 |     |     |`\
3838`           |                 |     |     +-- InfoPlist.strings  [see `[[macOS property list files|Localizing an Info.plist]]`]`\
3939`           |                 |     |`\
4040`           |                 |     +-- fr.lproj/                [for French] `\
4141`           |                 |     |     |`\
4242`           |                 |     |     +-- help.helpindex     [generated by the macOS help indexer hiutil]`\
4343`           |                 |     |     |`\
4444`           |                 |     |     +-- index.html         [table of contents - do not index]`\
4545`           |                 |     |     |`\
4646`           |                 |     |     +-- page1.html`\
4747`           |                 |     |     |`\
4848`           |                 |     |     +-- ...`\
4949`           |                 |     |     |`\
5050`           |                 |     |     +-- pageN.html`\
5151`           |                 |     |     |`\
5252`           |                 |     |     +-- InfoPlist.strings `\
5353`           |                 |     |`\
5454`           |                 |     +-- images/          `\
5555`           |                 |     |     |`\
5656`           |                 |     |     +-- chip3_32x32.ico    [icon used in help pages]`\
5757`           |                 |     `\
5858`           |                 +-- Info.plist                     [Help Book plist]`\
5959`           |`\
6060`           +-- Info.plist                                       [application plist]`
6161
6262## Main Menu
6363
6464If 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.
6565
6666You 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:
6767
6868``` pascal
6969...
7070Uses
7171 LCLIntf, MacOSAll;
7272...
7373function GetResourcesPath(): string;
7474var
7575 pathRef: CFURLRef;
7676 pathCFStr: CFStringRef;
7777 pathStr: shortstring;
7878begin
7979 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
8080 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
8181 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
8282
8383 Result := pathStr + '/Contents/Resources/';
8484end;
8585
8686procedure TForm1.MenuItem_MyAppHelpClick(Sender: TObject);
8787var
8888 helpPath: String;
8989begin
9090 helpPath := GetResourcesPath + 'MyApp.help';
9191 OpenDocument(helpPath);
9292end;
9393```
9494
9595> **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.
9696
9797## Application Info.plist
9898
9999One of the most important files is the application [[macOS property list files|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.
100100
101101` Help Book directory name`\
102102` Help Book identifier`
103103
104104The 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).
105105
106106If you are editing the `Info.plist` file in a text editor, then add these keys and their value strings:
107107
108108``` xml
109109 <key>CFBundleHelpBookFolder</key>
110110 <string>myapp.help</string>
111111 <key>CFBundleHelpBookName</key>
112112 <string>My App Help</string>
113113```
114114
115115## Help Book Info.plist
116116
117117Below is a basic Help Book Info.plist for an application called "My App".
118118
119119``` pascal
120120<?xml version="1.0" encoding="UTF-8"?>
121121<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
122122<plist version="1.0">
123123<dict>
124124 <key>CFBundleDevelopmentRegion</key>
125125 <string>en</string>
126126 <key>CFBundleIdentifier</key>
127127 <string>org.sentry.myapp.help</string>
128128 <key>CFBundleInfoDictionaryVersion</key>
129129 <string>6.0</string>
130130 <key>CFBundlePackageType</key>
131131 <string>BNDL</string>
132132 <key>CFBundleShortVersionString</key>
133133 <string>22</string>
134134 <key>CFBundleSignature</key>
135135 <string>hbwr</string>
136136 <key>CFBundleVersion</key>
137137 <string>22</string>
138138 <key>HPDBookAccessPath</key>
139139 <string>index.html</string>
140140 <key>HPDBookIconPath</key>
141141 <string>images/chip3_32x32.ico</string>
142142 <key>HPDBookIndexPath</key>
143143 <string>help.helpindex</string>
144144 <key>HPDBookTitle</key>
145145 <string>My App Help</string>
146146 <key>HPDBookType</key>
147147 <string>3</string>
148148 <key>HPDBookUsesExternalViewer</key>
149149 <false/>
150150</dict>
151151</plist>
152152```
153153
154154## Creating Help Book pages
155155
156156Apple 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".
157157
158158``` html
159159<?xml version="1.0" encoding="UTF-8"?>
160160<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
161161
162162<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
163163<head>
164164 <title>My App Table of Contents</title>
165165 <meta name="robots" content="noindex">
166166</head>
167167
168168<body class="Normal">
169169
170170 <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>
171171
172172 <ul>
173173 <li id="Page1"><a href="Page1.html">Introduction</a></li>
174174 <li id="Page2"><a href="Page2.html">Combo Boxes</a></li>
175175 </ul>
176176
177177</body>
178178</html>
179179```
180180
181181A sample page 1 for "My App" is set out below. Note:
182182
183183``` html
184184<?xml version="1.0" encoding="UTF-8"?>
185185<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
186186
187187<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
188188 <head>
189189 <title>Introduction</title>
190190 <meta name="robots" content="index, anchors" />
191191 <meta name="description" content="My App Introduction" />
192192 <meta name="keywords" content="introduction, operating system" />
193193 </head>
194194 <body>
195195 <a name="Introduction"></a>
196196 <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>
197197 <p>My App is a very useful application for all users of all ages!</p>
198198 <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>
199199 <p>The Windows version of My App has been tested on Windows XP, Windows 7 and Windows 10.</p>
200200
201201 <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>
202202 </body>
203203</html>
204204```
205205
206206Once 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:
207207
208208```text
209209 hiutil -Caf help.helpindex .
210210```
211211
212212This 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.
213213
214214An example of a help anchor is included in the sample page 1 HTML file above; specifically the: `<a name="Introduction"></a>` line.
215215
216216Also note the
217217
218218line which ensures that the anchors are indexed along with the file content.
219219
220220## Updating cached Help Books
221221
222222macOS 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:
223223
224224``` bash
225225#!/bin/sh
226226killall helpd
227227rm -rf ~/Library/Caches/com.apple.help*
228228rm -rf ~/Library/Preferences/com.apple.help*
229229```
230230
231231Note: 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.
232232
233233## Sample MyApp with Help Book
234234
235235The 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".
236236
237-<img src="MyAppMenu.png" width="300" />
237+![](/wiki-images/3/3a/MyAppMenu.png)
238238
239239Compiled MyApp: [Download](https://www.sentry.org/lazarus/MyApp.zip) (2.2MB).
240240
241241Full project source code is available from [SourceForge](https://sourceforge.net/p/lazarus-wiki-projects/code/ci/master/tree/laz_helpbook/).
242242
243243## See also
244244
245245- [[Application Bundle|macOS Application Bundle]]
246246- [[macOS property list files]]
247247
248248---
249249*From the Free Pascal wiki, page "Add an Apple Help Book to your macOS app", as it stood on 2021-12-28 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
250250Category: Code Snippets
251251Category: Help and Docs
252252Category: Platform-sensitive development
253253Category: Tutorials
254254Category: macOS

Back to history