Difference between revisions of "Application Bundle"

From Lazarus wiki
Jump to navigationJump to search
(syntaxhighlight)
m (→‎Using a shell script: PkgInfo contents update to generic)
 
(30 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{Application Bundle}}
 
{{Application Bundle}}
  
Application Bundle is a directory, often with ".app" extension, with special purpose on Mac OS X system. It contains resource files and information about the application. It is necessary for correct execution of applications which are using [[Carbon Interface]]. 
+
== Overview ==
  
=== Creating Application Bundle ===
+
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.
  
==== From Lazarus ====
+
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.
Open project and go to Project -> Project Options -> Application tab and push the [[IDE Window: Project Options#Create Application Bundle|Create Application Bundle]] button. The resulted Application Bundle will have inside symbolic link to the real executable.
+
 
 +
You can learn more about application bundles in the [[#External links|Apple Bundle Programming Guide]].
 +
 
 +
Application (bundle) settings are located in the property list file: [[macOS property list files|Info.plist]] located in the <tt>bundle.app/Contents/</tt> directory.
 +
 
 +
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”.
 +
 
 +
To access the application bundle contents in Finder, you need to right click (ctrl-left click) on a bundle and select ''Show Package Contents''
 +
 
 +
[[Image:openbundle.png]]
 +
 
 +
[[Image:infoplist.png]]
 +
 
 +
== Application bundle layout ==
 +
 
 +
The basic structure of a Mac application bundle:
 +
 
 +
MyApp.app/
 +
    Contents/
 +
      Info.plist
 +
      MacOS/
 +
      PkgInfo
 +
      Resources/
 +
 
 +
{| class="wikitable"
 +
! Sub-Directory of the Contents directory !! Usage description
 +
|-
 +
|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.
 +
|-
 +
|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#Bundle_layouts|Adding a Help Book - Bundle layouts]].
 +
|-
 +
|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]].
 +
|-
 +
|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.
 +
|-
 +
|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.
 +
|}
 +
 
 +
== Creating the Application Bundle ==
 +
 
 +
=== Using Lazarus ===
 +
 
 +
Open a project in the Lazarus IDE and go to Project -> Project Options -> Application tab and push the [[IDE_Window:_Project_Options#Application|Create Application Bundle]] button. The resulting Application Bundle will contain a symbolic link to the real executable.
 +
 
 +
{{Note|You must remove the symbolic link and copy the real executable "project1" into the <tt>project1.app/Contents/MacOS/</tt> directory to distribute the application and use it on another computer.}}
 +
 
 +
=== Using the Lazarus command-line tool ===
 +
 
 +
Open <tt>/Applications/Lazarus/components/macfiles/examples/createmacapplication.lpi</tt> in the Lazarus IDE. Compile.
 +
 
 +
Open a Terminal of your choice and type:
  
==== Via command-line tool shipped with Lazarus ====
 
Open lazarus/components/macfiles/examples/createmacapplication.lpi in the IDE. Compile.
 
Open a Terminal of your choice. Type:
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
cd project1/
 
cd project1/
lazarus/components/macfiles/examples/createmacapplication project1
+
/Applications/Lazarus/components/macfiles/examples/createmacapplication project1
 
ln -s ../../../project1 project1.app/Contents/MacOS/project1
 
ln -s ../../../project1 project1.app/Contents/MacOS/project1
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== Use shell script ====
+
=== Using a shell script ===
 +
 
 +
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.
  
You can adapt the following shell script to create a customized application bundle for your application. It allows the creation of either debug or release bundle, being that in the release bundle a link to the executable is placed, to allow for debugging using the Lazarus IDE, while in release mode the executable is copied.
+
* In the debug bundle, a link to the executable is placed, allowing for debugging using the Lazarus IDE,  
 +
* In the release bundle, the executable is copied so the application bundle as a whole can be used stand-alone/copied to other locations.
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
Line 65: Line 115:
 
appfile=magnifier
 
appfile=magnifier
  
PkgInfoContents="APPLMAG#"
+
PkgInfoContents="APPL????"
  
 
#
 
#
Line 79: Line 129:
 
   mkdir $appfolder/Contents
 
   mkdir $appfolder/Contents
 
   mkdir $appfolder/Contents/MacOS
 
   mkdir $appfolder/Contents/MacOS
 +
  mkdir $appfolder/Contents/Frameworks  # optional, for including libraries or frameworks
 
   mkdir $appfolder/Contents/Resources
 
   mkdir $appfolder/Contents/Resources
  
Line 128: Line 179:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Executing application via Application Bundle ===
+
== Executing an application via the Application Bundle ==
 +
 
 +
You can start the application from the Lazarus IDE, by clicking on its Finder icon or in the native macOS Terminal.app by typing:
 +
 
 +
<syntaxhighlight lang="bash">open project1.app</syntaxhighlight>
 +
 
 +
or
 +
 
 +
<syntaxhighlight lang="bash">./project1.app/Contents/MacOS/project1</syntaxhighlight>
 +
 
 +
== See also ==
 +
 
 +
* [[Add an Apple Help Book to your macOS app]]
 +
* [[Deploying_Your_Application#macOS_Deployment|Deploying your macOS application]]
 +
* [[Hiding a macOS app from the Dock]]
 +
* [[macOS property list files]]
  
You can start the application from IDE (checked option [[IDE_Window: Project Options#Use Application Bundle for running and debugging (darwin only)|Use Application Bundle for running and debugging (darwin only)]]) or via its Finder icon or in the native Mac OS X Terminal via "open project1.app".
+
== External links ==
  
[[Category:Lazarus]]
+
* [https://developer.apple.com/documentation/foundation/nsbundle Apple: NSBundle class]
 +
* [https://developer.apple.com/documentation/bundleresources/ Apple: Bundle Resources]
 +
* [https://developer.apple.com/documentation/corefoundation/cfbundle Apple: CFBundle]
 +
* [https://developer.apple.com/documentation/bundleresources/information_property_list/bundle_configuration Apple: Bundle Configuration]
 +
* [https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/Introduction/Introduction.html Apple: Resource Programming Guide]
 +
* [https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html Apple: Bundle Programming Guide]

Latest revision as of 05:50, 13 March 2022

English (en) 日本語 (ja) русский (ru)

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide


Overview

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.

From within Lazarus it is used for the Carbon and Cocoa interfaces, but one can also build applications with other interfaces (eg Gtk or Qt) by using scripts.

You can learn more about application bundles in the Apple Bundle Programming Guide.

Application (bundle) settings are located in the property list file: Info.plist located in the bundle.app/Contents/ directory.

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”.

To access the application bundle contents in Finder, you need to right click (ctrl-left click) on a bundle and select Show Package Contents

openbundle.png

infoplist.png

Application bundle layout

The basic structure of a Mac application bundle:

MyApp.app/
   Contents/
      Info.plist
      MacOS/
      PkgInfo
      Resources/
Sub-Directory of the Contents directory Usage description
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.
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 Adding a Help Book - Bundle layouts.
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.
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.
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.

Creating the Application Bundle

Using Lazarus

Open a project in the Lazarus IDE and go to Project -> Project Options -> Application tab and push the Create Application Bundle button. The resulting Application Bundle will contain a symbolic link to the real executable.

Light bulb  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.

Using the Lazarus command-line tool

Open /Applications/Lazarus/components/macfiles/examples/createmacapplication.lpi in the Lazarus IDE. Compile.

Open a Terminal of your choice and type:

cd project1/
/Applications/Lazarus/components/macfiles/examples/createmacapplication project1
ln -s ../../../project1 project1.app/Contents/MacOS/project1

Using a shell script

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.

  • In the debug bundle, a link to the executable is placed, allowing for debugging using the Lazarus IDE,
  • In the release bundle, the executable is copied so the application bundle as a whole can be used stand-alone/copied to other locations.
#!/bin/sh
# Force Bourne shell in case tcsh is default.
#

#
# Reads the bundle type
#

echo "========================================================"
echo "    Bundle creation script"
echo "========================================================"
echo ""
echo " Please select which kind of bundle you would like to build:"
echo ""
echo " 1 > Debug bundle"
echo " 2 > Release bundle"
echo " 0 > Exit"

read command

case $command in

  1) ;;

  2) ;;
  
  0) exit 0;;

  *) echo "Invalid command"
     exit 0;;

esac

#
# Creates the bundle
#

appname=Magnifier
appfolder=$appname.app
macosfolder=$appfolder/Contents/MacOS
plistfile=$appfolder/Contents/Info.plist
appfile=magnifier

PkgInfoContents="APPL????"

#
if ! [ -e $appfile ]
then
  echo "$appfile does not exist"
elif [ -e $appfolder ]
then
  echo "$appfolder already exists"
else
  echo "Creating $appfolder..."
  mkdir $appfolder
  mkdir $appfolder/Contents
  mkdir $appfolder/Contents/MacOS
  mkdir $appfolder/Contents/Frameworks  # optional, for including libraries or frameworks
  mkdir $appfolder/Contents/Resources

#
# For a debug bundle,
# Instead of copying executable into .app folder after each compile,
# simply create a symbolic link to executable.
#
if [ $command = 1 ]; then
  ln -s ../../../$appname $macosfolder/$appname
else
  cp $appname $macosfolder/$appname
fi  

# Copy the resource files to the correct place
  cp *.bmp $appfolder/Contents/Resources
  cp icon3.ico $appfolder/Contents/Resources
  cp icon3.png $appfolder/Contents/Resources
  cp macicon.icns $appfolder/Contents/Resources
  cp docs/*.* $appfolder/Contents/Resources
#
# Create PkgInfo file.
  echo $PkgInfoContents >$appfolder/Contents/PkgInfo
#
# Create information property list file (Info.plist).
  echo '<?xml version="1.0" encoding="UTF-8"?>' >$plistfile
  echo '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >>$plistfile
  echo '<plist version="1.0">' >>$plistfile
  echo '<dict>' >>$plistfile
  echo '  <key>CFBundleDevelopmentRegion</key>' >>$plistfile
  echo '  <string>English</string>' >>$plistfile
  echo '  <key>CFBundleExecutable</key>' >>$plistfile
  echo '  <string>'$appname'</string>' >>$plistfile
  echo '  <key>CFBundleIconFile</key>' >>$plistfile
  echo '  <string>macicon.icns</string>' >>$plistfile
  echo '  <key>CFBundleIdentifier</key>' >>$plistfile
  echo '  <string>org.magnifier.magnifier</string>' >>$plistfile
  echo '  <key>CFBundleInfoDictionaryVersion</key>' >>$plistfile
  echo '  <string>6.0</string>' >>$plistfile
  echo '  <key>CFBundlePackageType</key>' >>$plistfile
  echo '  <string>APPL</string>' >>$plistfile
  echo '  <key>CFBundleSignature</key>' >>$plistfile
  echo '  <string>MAG#</string>' >>$plistfile
  echo '  <key>CFBundleVersion</key>' >>$plistfile
  echo '  <string>1.0</string>' >>$plistfile
  echo '</dict>' >>$plistfile
  echo '</plist>' >>$plistfile
fi

Executing an application via the Application Bundle

You can start the application from the Lazarus IDE, by clicking on its Finder icon or in the native macOS Terminal.app by typing:

open project1.app

or

./project1.app/Contents/MacOS/project1

See also

External links