macosinstallationpackagemakerproductbuildpkgbuild

Running postflight script when installing a package on Mac


I am trying to build a mac package installer from a script and I want to run postinstall and postflight scripts. My script for building the package looks like this:

pkgbuild --root MyRoot/MyApp.app --identifier com.myapp.MyApp --scripts Scripts --install-location /Applications/MyApp.app MyApp.pkg
productbuild --synthesize --package MyApp.pkg Distribution.xml
productbuild --distribution Distribution.xml --resources Resources --package-path . CompleteInstaller.pkg

I've placed the postinstall script in Scripts and it is being executed successfully. I have a problem with running the postflight script though. It is placed in the Resources directory and doesn't get executed. Is this not enough to specify the path to it? I couldn't find an answer to this question, I'm sorry if I've missed it.

PS: My postflight script does nothing special - for now it tries to create a file in the home directory:

#!/bin/sh
touch ~/file
exit 0

Solution

  • I am not sure why do you have both postinstall and postflight in a package. Both are same scripts which must be run after the "copy files" phase of the installation. "postflight" name was used in older style packages - bundles. After the introduction of flat packages, the same script has to be named as "postinstall". In flat packages, installer treats a file as a postinstall script only if its name is "postinstall", not "postflight" and is present inside "Scripts" directory, not "Resources" directory. Probably, this is the reason your script is not being executed.

    You can get some more information regarding scripts used in packages here.