Application Bundle: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## Overview | |
| 2 | 2 | ||
| 3 | 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 | 4 | ||
| 5 | 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 | 6 | ||
| 7 | 7 | You can learn more about application bundles in the Apple Bundle Programming Guide. | |
| 8 | 8 | ||
| 9 | 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 | 10 | ||
| 11 | 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 | 12 | ||
| 13 | 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 | 14 | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 15 | + |  | |
| 19 | 16 | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 17 | + |  | |
| 24 | 18 | ||
| 25 | 19 | ## Application bundle layout | |
| 26 | 20 | ||
| 27 | 21 | The basic structure of a Mac application bundle: | |
| 28 | 22 | ||
| 29 | 23 | `MyApp.app/`\ | |
| 30 | 24 | ` Contents/`\ | |
| 31 | 25 | ` Info.plist`\ | |
| 32 | 26 | ` MacOS/`\ | |
| 33 | 27 | ` PkgInfo`\ | |
| 34 | 28 | ` Resources/` | |
| 35 | 29 | ||
| 36 | 30 | | Sub-Directory of the Contents directory | Usage description | | |
| 37 | 31 | |----|----| | |
| 38 | 32 | | 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 | 33 | | 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 | 34 | | 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 | 35 | | 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 | 36 | | 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 | 37 | ||
| 44 | 38 | ## Creating the Application Bundle | |
| 45 | 39 | ||
| 46 | 40 | ### Using Lazarus | |
| 47 | 41 | ||
| 48 | 42 | 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 | 43 | ||
| 50 | 44 | > **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 | 45 | ||
| 52 | 46 | ### Using the Lazarus command-line tool | |
| 53 | 47 | ||
| 54 | 48 | Open `/Applications/Lazarus/components/macfiles/examples/createmacapplication.lpi` in the Lazarus IDE. Compile. | |
| 55 | 49 | ||
| 56 | 50 | Open a Terminal of your choice and type: | |
| 57 | 51 | ||
| 58 | 52 | ``` bash | |
| 59 | 53 | cd project1/ | |
| 60 | 54 | /Applications/Lazarus/components/macfiles/examples/createmacapplication project1 | |
| 61 | 55 | ln -s ../../../project1 project1.app/Contents/MacOS/project1 | |
| 62 | 56 | ``` | |
| 63 | 57 | ||
| 64 | 58 | ### Using a shell script | |
| 65 | 59 | ||
| 66 | 60 | 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 | 61 | ||
| 68 | 62 | - In the debug bundle, a link to the executable is placed, allowing for debugging using the Lazarus IDE, | |
| 69 | 63 | - 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 | 64 | ||
| 71 | 65 | ``` bash | |
| 72 | 66 | #!/bin/sh | |
| 73 | 67 | # Force Bourne shell in case tcsh is default. | |
| 74 | 68 | # | |
| 75 | 69 | ||
| 76 | 70 | # | |
| 77 | 71 | # Reads the bundle type | |
| 78 | 72 | # | |
| 79 | 73 | ||
| 80 | 74 | echo "========================================================" | |
| 81 | 75 | echo " Bundle creation script" | |
| 82 | 76 | echo "========================================================" | |
| 83 | 77 | echo "" | |
| 84 | 78 | echo " Please select which kind of bundle you would like to build:" | |
| 85 | 79 | echo "" | |
| 86 | 80 | echo " 1 > Debug bundle" | |
| 87 | 81 | echo " 2 > Release bundle" | |
| 88 | 82 | echo " 0 > Exit" | |
| 89 | 83 | ||
| 90 | 84 | read command | |
| 91 | 85 | ||
| 92 | 86 | case $command in | |
| 93 | 87 | ||
| 94 | 88 | 1) ;; | |
| 95 | 89 | ||
| 96 | 90 | 2) ;; | |
| 97 | 91 | ||
| 98 | 92 | 0) exit 0;; | |
| 99 | 93 | ||
| 100 | 94 | *) echo "Invalid command" | |
| 101 | 95 | exit 0;; | |
| 102 | 96 | ||
| 103 | 97 | esac | |
| 104 | 98 | ||
| 105 | 99 | # | |
| 106 | 100 | # Creates the bundle | |
| 107 | 101 | # | |
| 108 | 102 | ||
| 109 | 103 | appname=Magnifier | |
| 110 | 104 | appfolder=$appname.app | |
| 111 | 105 | macosfolder=$appfolder/Contents/MacOS | |
| 112 | 106 | plistfile=$appfolder/Contents/Info.plist | |
| 113 | 107 | appfile=magnifier | |
| 114 | 108 | ||
| 115 | 109 | PkgInfoContents="APPL????" | |
| 116 | 110 | ||
| 117 | 111 | # | |
| 118 | 112 | if ! [ -e $appfile ] | |
| 119 | 113 | then | |
| 120 | 114 | echo "$appfile does not exist" | |
| 121 | 115 | elif [ -e $appfolder ] | |
| 122 | 116 | then | |
| 123 | 117 | echo "$appfolder already exists" | |
| 124 | 118 | else | |
| 125 | 119 | echo "Creating $appfolder..." | |
| 126 | 120 | mkdir $appfolder | |
| 127 | 121 | mkdir $appfolder/Contents | |
| 128 | 122 | mkdir $appfolder/Contents/MacOS | |
| 129 | 123 | mkdir $appfolder/Contents/Frameworks # optional, for including libraries or frameworks | |
| 130 | 124 | mkdir $appfolder/Contents/Resources | |
| 131 | 125 | ||
| 132 | 126 | # | |
| 133 | 127 | # For a debug bundle, | |
| 134 | 128 | # Instead of copying executable into .app folder after each compile, | |
| 135 | 129 | # simply create a symbolic link to executable. | |
| 136 | 130 | # | |
| 137 | 131 | if [ $command = 1 ]; then | |
| 138 | 132 | ln -s ../../../$appname $macosfolder/$appname | |
| 139 | 133 | else | |
| 140 | 134 | cp $appname $macosfolder/$appname | |
| 141 | 135 | fi | |
| 142 | 136 | ||
| 143 | 137 | # Copy the resource files to the correct place | |
| 144 | 138 | cp *.bmp $appfolder/Contents/Resources | |
| 145 | 139 | cp icon3.ico $appfolder/Contents/Resources | |
| 146 | 140 | cp icon3.png $appfolder/Contents/Resources | |
| 147 | 141 | cp macicon.icns $appfolder/Contents/Resources | |
| 148 | 142 | cp docs/*.* $appfolder/Contents/Resources | |
| 149 | 143 | # | |
| 150 | 144 | # Create PkgInfo file. | |
| 151 | 145 | echo $PkgInfoContents >$appfolder/Contents/PkgInfo | |
| 152 | 146 | # | |
| 153 | 147 | # Create information property list file (Info.plist). | |
| 154 | 148 | echo '<?xml version="1.0" encoding="UTF-8"?>' >$plistfile | |
| 155 | 149 | echo '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >>$plistfile | |
| 156 | 150 | echo '<plist version="1.0">' >>$plistfile | |
| 157 | 151 | echo '<dict>' >>$plistfile | |
| 158 | 152 | echo ' <key>CFBundleDevelopmentRegion</key>' >>$plistfile | |
| 159 | 153 | echo ' <string>English</string>' >>$plistfile | |
| 160 | 154 | echo ' <key>CFBundleExecutable</key>' >>$plistfile | |
| 161 | 155 | echo ' <string>'$appname'</string>' >>$plistfile | |
| 162 | 156 | echo ' <key>CFBundleIconFile</key>' >>$plistfile | |
| 163 | 157 | echo ' <string>macicon.icns</string>' >>$plistfile | |
| 164 | 158 | echo ' <key>CFBundleIdentifier</key>' >>$plistfile | |
| 165 | 159 | echo ' <string>org.magnifier.magnifier</string>' >>$plistfile | |
| 166 | 160 | echo ' <key>CFBundleInfoDictionaryVersion</key>' >>$plistfile | |
| 167 | 161 | echo ' <string>6.0</string>' >>$plistfile | |
| 168 | 162 | echo ' <key>CFBundlePackageType</key>' >>$plistfile | |
| 169 | 163 | echo ' <string>APPL</string>' >>$plistfile | |
| 170 | 164 | echo ' <key>CFBundleSignature</key>' >>$plistfile | |
| 171 | 165 | echo ' <string>MAG#</string>' >>$plistfile | |
| 172 | 166 | echo ' <key>CFBundleVersion</key>' >>$plistfile | |
| 173 | 167 | echo ' <string>1.0</string>' >>$plistfile | |
| 174 | 168 | echo '</dict>' >>$plistfile | |
| 175 | 169 | echo '</plist>' >>$plistfile | |
| 176 | 170 | fi | |
| 177 | 171 | ``` | |
| 178 | 172 | ||
| 179 | 173 | ## Executing an application via the Application Bundle | |
| 180 | 174 | ||
| 181 | 175 | 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 | 176 | ||
| 183 | 177 | ``` bash | |
| 184 | 178 | open project1.app | |
| 185 | 179 | ``` | |
| 186 | 180 | ||
| 187 | 181 | or | |
| 188 | 182 | ||
| 189 | 183 | ``` bash | |
| 190 | 184 | ./project1.app/Contents/MacOS/project1 | |
| 191 | 185 | ``` | |
| 192 | 186 | ||
| 193 | 187 | ## See also | |
| 194 | 188 | ||
| 195 | 189 | - [[Add an Apple Help Book to your macOS app]] | |
| 196 | 190 | - [[Deploying Your Application|Deploying your macOS application]] | |
| 197 | 191 | - [[Hiding a macOS app from the Dock]] | |
| 198 | 192 | - [[macOS property list files]] | |
| 199 | 193 | ||
| 200 | 194 | ## External links | |
| 201 | 195 | ||
| 202 | 196 | - [Apple: NSBundle class](https://developer.apple.com/documentation/foundation/nsbundle) | |
| 203 | 197 | - [Apple: Bundle Resources](https://developer.apple.com/documentation/bundleresources/) | |
| 204 | 198 | - [Apple: CFBundle](https://developer.apple.com/documentation/corefoundation/cfbundle) | |
| 205 | 199 | - [Apple: Bundle Configuration](https://developer.apple.com/documentation/bundleresources/information_property_list/bundle_configuration) | |
| 206 | 200 | - [Apple: Resource Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/Introduction/Introduction.html) | |
| 207 | 201 | - [Apple: Bundle Programming Guide](https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html) | |
| 208 | 202 | ||
| 209 | 203 | --- | |
| 210 | 204 | *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).* |
