Sign in Join
Wiki copytemporary

Application Bundle: what changed

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

Title: Application Bundle

OldNewLine
1+## Overview
2+
3+An application bundle is a directory with the extension ".app" on macOS systems. It contains the application executable, resource files, library files (if any), help files and information about the application and is necessary for the correct execution of applications. The Mac Finder treats this .app directory as the application file and by default does not show any of its sub-directories.
4+
5+From within Lazarus it is used for the [[Carbon Interface|Carbon]] and [[Cocoa Interface|Cocoa]] interfaces, but one can also build applications with other interfaces (eg Gtk or Qt) by using scripts.
6+
7+You can learn more about application bundles in the Apple Bundle Programming Guide.
8+
9+Application (bundle) settings are located in the property list file: [[macOS property list files|Info.plist]] located in the `bundle.app/Contents/` directory.
10+
11+The **PkgInfo** file is an alternative way to specify the type and creator codes of your application or bundle. This file is not required, but can improve performance for code that accesses this information. Regardless of whether you provide this file, you should always include type and creator information in your information property list file using the CFBundlePackageType and CFBundleSignature keys, respectively. The contents of the PkgInfo file are the 4-byte package type followed by the 4-byte signature of your application. Thus, for the TextEdit application, whose type is 'APPL' and whose signature is 'ttxt', the file would contain the ASCII string “APPLttxt”.
12+
13+To access the application bundle contents in Finder, you need to right click (ctrl-left click) on a bundle and select *Show Package Contents*
14+
15+<figure>
16+<img src="openbundle.png" title="openbundle.png" />
17+<figcaption>openbundle.png</figcaption>
18+</figure>
19+
20+<figure>
21+<img src="infoplist.png" title="infoplist.png" />
22+<figcaption>infoplist.png</figcaption>
23+</figure>
24+
25+## Application bundle layout
26+
27+The basic structure of a Mac application bundle:
28+
29+`MyApp.app/`\
30+`   Contents/`\
31+`      Info.plist`\
32+`      MacOS/`\
33+`      PkgInfo`\
34+`      Resources/`
35+
36+| Sub-Directory of the Contents directory | Usage description |
37+|----|----|
38+| MacOS | (Required) Contains the application’s standalone executable code. Typically, this directory contains only one binary file with your application’s main entry point and statically linked code. However, you may put other standalone executables (such as command-line tools) in this directory as well. |
39+| Resources | Contains all of the application’s resource files. The contents of this directory are further organized to distinguish between localized and nonlocalized resources. For more information about the structure of this directory, see [[Add an Apple Help Book to your macOS app|Adding a Help Book - Bundle layouts]]. |
40+| Frameworks | Contains any private shared libraries and frameworks used by the executable. The frameworks in this directory are revision-locked to the application and cannot be superseded by any other, even newer, versions that may be available to the operating system. In other words, the frameworks included in this directory take precedence over any other similarly named frameworks found in other parts of the operating system. For information on how to add shared libraries to your application bundle, see [[macOS Dynamic Libraries]]. |
41+| PlugIns | Contains loadable bundles that extend the basic features of your application. You use this directory to include code modules that must be loaded into your application’s process space in order to be used. You would not use this directory to store standalone executables. |
42+| SharedSupport | Contains additional non-critical resources that do not impact the ability of the application to run. You might use this directory to include things like document templates, clip art, and tutorials that your application expects to be present but that do not affect the ability of your application to run. |
43+
44+## Creating the Application Bundle
45+
46+### Using Lazarus
47+
48+Open a project in the Lazarus IDE and go to Project -\> Project Options -\> Application tab and push the [[IDE Window: Project Options|Create Application Bundle]] button. The resulting Application Bundle will contain a symbolic link to the real executable.
49+
50+> **Note:** You must remove the symbolic link and copy the real executable "project1" into the `project1.app/Contents/MacOS/` directory to distribute the application and use it on another computer.
51+
52+### Using the Lazarus command-line tool
53+
54+Open `/Applications/Lazarus/components/macfiles/examples/createmacapplication.lpi` in the Lazarus IDE. Compile.
55+
56+Open a Terminal of your choice and type:
57+
58+``` bash
59+cd project1/
60+/Applications/Lazarus/components/macfiles/examples/createmacapplication project1
61+ln -s ../../../project1 project1.app/Contents/MacOS/project1
62+```
63+
64+### Using a shell script
65+
66+You can adapt the following shell script to create a customized application bundle for your application. It allows the creation of either a debug or a release bundle.
67+
68+- In the debug bundle, a link to the executable is placed, allowing for debugging using the Lazarus IDE,
69+- In the release bundle, the executable is copied so the application bundle as a whole can be used stand-alone/copied to other locations.
70+
71+``` bash
72+#!/bin/sh
73+# Force Bourne shell in case tcsh is default.
74+#
75+
76+#
77+# Reads the bundle type
78+#
79+
80+echo "========================================================"
81+echo " Bundle creation script"
82+echo "========================================================"
83+echo ""
84+echo " Please select which kind of bundle you would like to build:"
85+echo ""
86+echo " 1 > Debug bundle"
87+echo " 2 > Release bundle"
88+echo " 0 > Exit"
89+
90+read command
91+
92+case $command in
93+
94+ 1) ;;
95+
96+ 2) ;;
97+
98+ 0) exit 0;;
99+
100+ *) echo "Invalid command"
101+ exit 0;;
102+
103+esac
104+
105+#
106+# Creates the bundle
107+#
108+
109+appname=Magnifier
110+appfolder=$appname.app
111+macosfolder=$appfolder/Contents/MacOS
112+plistfile=$appfolder/Contents/Info.plist
113+appfile=magnifier
114+
115+PkgInfoContents="APPL????"
116+
117+#
118+if ! [ -e $appfile ]
119+then
120+ echo "$appfile does not exist"
121+elif [ -e $appfolder ]
122+then
123+ echo "$appfolder already exists"
124+else
125+ echo "Creating $appfolder..."
126+ mkdir $appfolder
127+ mkdir $appfolder/Contents
128+ mkdir $appfolder/Contents/MacOS
129+ mkdir $appfolder/Contents/Frameworks # optional, for including libraries or frameworks
130+ mkdir $appfolder/Contents/Resources
131+
132+#
133+# For a debug bundle,
134+# Instead of copying executable into .app folder after each compile,
135+# simply create a symbolic link to executable.
136+#
137+if [ $command = 1 ]; then
138+ ln -s ../../../$appname $macosfolder/$appname
139+else
140+ cp $appname $macosfolder/$appname
141+fi
142+
143+# Copy the resource files to the correct place
144+ cp *.bmp $appfolder/Contents/Resources
145+ cp icon3.ico $appfolder/Contents/Resources
146+ cp icon3.png $appfolder/Contents/Resources
147+ cp macicon.icns $appfolder/Contents/Resources
148+ cp docs/*.* $appfolder/Contents/Resources
149+#
150+# Create PkgInfo file.
151+ echo $PkgInfoContents >$appfolder/Contents/PkgInfo
152+#
153+# Create information property list file (Info.plist).
154+ echo '<?xml version="1.0" encoding="UTF-8"?>' >$plistfile
155+ echo '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >>$plistfile
156+ echo '<plist version="1.0">' >>$plistfile
157+ echo '<dict>' >>$plistfile
158+ echo ' <key>CFBundleDevelopmentRegion</key>' >>$plistfile
159+ echo ' <string>English</string>' >>$plistfile
160+ echo ' <key>CFBundleExecutable</key>' >>$plistfile
161+ echo ' <string>'$appname'</string>' >>$plistfile
162+ echo ' <key>CFBundleIconFile</key>' >>$plistfile
163+ echo ' <string>macicon.icns</string>' >>$plistfile
164+ echo ' <key>CFBundleIdentifier</key>' >>$plistfile
165+ echo ' <string>org.magnifier.magnifier</string>' >>$plistfile
166+ echo ' <key>CFBundleInfoDictionaryVersion</key>' >>$plistfile
167+ echo ' <string>6.0</string>' >>$plistfile
168+ echo ' <key>CFBundlePackageType</key>' >>$plistfile
169+ echo ' <string>APPL</string>' >>$plistfile
170+ echo ' <key>CFBundleSignature</key>' >>$plistfile
171+ echo ' <string>MAG#</string>' >>$plistfile
172+ echo ' <key>CFBundleVersion</key>' >>$plistfile
173+ echo ' <string>1.0</string>' >>$plistfile
174+ echo '</dict>' >>$plistfile
175+ echo '</plist>' >>$plistfile
176+fi
177+```
178+
179+## Executing an application via the Application Bundle
180+
181+You can start the application from the Lazarus IDE, by clicking on its Finder icon or in the native macOS Terminal.app by typing:
182+
183+``` bash
184+open project1.app
185+```
186+
187+or
188+
189+``` bash
190+./project1.app/Contents/MacOS/project1
191+```
192+
193+## See also
194+
195+- [[Add an Apple Help Book to your macOS app]]
196+- [[Deploying Your Application|Deploying your macOS application]]
197+- [[Hiding a macOS app from the Dock]]
198+- [[macOS property list files]]
199+
200+## External links
201+
202+- [Apple: NSBundle class](https://developer.apple.com/documentation/foundation/nsbundle)
203+- [Apple: Bundle Resources](https://developer.apple.com/documentation/bundleresources/)
204+- [Apple: CFBundle](https://developer.apple.com/documentation/corefoundation/cfbundle)
205+- [Apple: Bundle Configuration](https://developer.apple.com/documentation/bundleresources/information_property_list/bundle_configuration)
206+- [Apple: Resource Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/Introduction/Introduction.html)
207+- [Apple: Bundle Programming Guide](https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html)
208+
209+---
210+*From the Free Pascal wiki, page "Application Bundle", as it stood on 2022-03-13 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).*

Back to history