📜 ⬆️ ⬇️

Platypus and Packages. Create and install programs on macOS



It is often necessary to build code written in Python into an application for Windows, Linux or OS X. Yes, there are indeed many packages for this, such as cross-platform cx_Freeze and PyInstaller , as well as separate py2exe and py2app . But for macOS it is possible to avoid messing around with creating an installation file.

I must say that unlike Windows, a program on MacOS is just a folder with the .app extension , but the installer has the extension .pkg .

Create a program

To create a program from a script, use the program Platypus . This is a free, open-source program, distributed under the BSD license. Here is the only main window of this program:

')
  • In the App Name field, specify the name of the application, this is how the program will be displayed in the list of programs and Launchpad.
  • In the Script Type drop-down list the language of the script is indicated, in our case it is Python, you can specify the path to the compiler, if, for example, there are several of them on the computer.
  • In the Script Path field, you can create your own new script (“+ New” button), or you can select a ready-made script (“Select Script” button).
  • In the Interface drop-down list, you can select the type of application, in case you have a console application that does not require user input, I recommend selecting Text Window.
  • To the left of the above fields, there is a field for selecting an application icon, the program includes several standard icons, but you can choose your own.
  • The next block of fields is responsible for information about the program, which is provided in the standard About menu. This is a unique identifier, the author of the program and its version. In addition, you can specify properties to run, for example, run as an administrator or work in the background.
  • And the second most important block, these are additional files. As in MacOS, an application is not only an executable file, but also all the files and folders necessary for the application to work. By pressing the "+" button, you can select both individual files and entire folders.
  • After filling in all the fields, the “Create App” button becomes active, press it and get a nice file with the extension .app.

We have compiled an application that will run on any computer running macOS, but this is not enough for easy distribution and installation by the end user. Therefore, we will create an installation package for our application.

Create an installer

To create installation packages there are several programs, we will use Packages .



When you open the Packages program, we are asked to choose the type of installation package, choose Distibution, since the Raw Package is necessary when you need to install the files in a non-standard location.



In the next window, select the name and location of the project. It makes sense to specify the project working folder as the location. After creating the project, the following window opens, with a large number of tabs.



Let's run through them:

  • Settings is the first tab, here you can set the name of the application, the working directory, as well as the necessary conditions for installation.
  • Presentations - on this tab you can see the appearance of the installation package itself, add translations to various languages, the installation path and other nuances.
  • Requirements & Resources - the necessary conditions for installation and dependencies. For example, if the program requires Xcode Command Line Tools , then you can specify this here, and if the check fails, the installation will be canceled.
  • Comments - tab exclusively for developer notes, is a huge field for entering text.


If in the side menu we select the package we need (in the picture it is My ), then we will see the following picture:



In the Settings tab, you select an identifier, version, and you can specify what to ask the user to do after installation (turn off the computer, reboot, and end the user session). In addition, you can select Location and additional options, such as entering the admin password, changing permissions for a folder, and so on.

In the Payload tab, the main process takes place, adding the application file to the project. You need to select the desired folder to install the application, for example, Applications , click on the "+" button and select a ready-made file with the extension .app .



In the Scripts tab, you can select scripts that will run before or after installation.

After all the items are completed, you need to build a project. This can be done either from the menu Build -> Build or with the help of a special short command B.

After this, you will receive an installation file that you can share with your friends and partners.

Links to necessary files and training video

Platypus - program to build .app;

Packages - software for assembling pkg;

This is a great video tutorial for working with the Packages program.

Small addition

By default, Platypus just runs the script, but almost always we need to run the program on a computer where python is not installed. For such a case there is a special life hack:

In Platypus, select the shell script as the script type, click on + and write the following code:

#!/bin/sh /Applications/MyApp.app/Contents/Resources/compiler/bin/python /Applications/MyApp.app/Contents/Resources/main.py 


Where
  • MyApp.app is the name of your application.
  • / compiler / bin / python - path to the compiler
  • main.py - required script


Now, if you build the program and move it to the Applications folder, it will start with your compiler.

Source: https://habr.com/ru/post/304778/


All Articles