Sign in Join
Wiki copytemporary

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

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

Back to history