Make your python CUI application an app for mac

Overview -Use py2app to convert a program created in python into an app. -Target console-based programs, not GUI applications that use wxPython or pyQt. -When you start the created app, terminal will start up and the program will run on it.

Target

This time, I wanted to distribute the python CUI application as shown below to the general public, and tried to create an application for mac. controlServer.png However, even if I execute the created app, there is no sign that it will start at all (it has been bound in the Dock all the time) ... In fact, the program is ** running in the background **. However, since the execution status is refreshing as it is, I want terminal to appear and execute on it. (By the way, py2exe for windows works well as shown in the figure below ...) control_win.png The goal this time is to create an application that works ** "on the console" ** of a CUI application created with python, just as it was created with py2exe.

flow

  1. Create a mac application (referred to as base.app) from a python program with py2app.
  2. Write a script that directly hits the UNIX executable file in base.app.
  3. Convert the script into an app (let's call the created app wrap.app).
  4. Store base.app in the resource folder in wrap.app. python_to_binary_app.png

1. Create a mac application with py2app

2. Create a script that directly hits the executable file in base.app

When I double-click base.app, nothing happens on the screen (it is bound in the Dock or does not respond). しかし,base.app/Contents/MacOS/baseをダブルクリックするとterminalが起動し,その上でプログラムが動作しているかと思います! This time ** I will create a script that directly hits this executable file and make it an app ** (let's call it wrap.app). The script uses AppleScript. As will be described later, it is easy to get the path of the resource folder in the app file with AppleScript, so AppleScript is recommended as the script to create. Write the following script.

wrapper.scpt


set base_path to (POSIX path of (path to resource "base.app/Contents/MacOS/base")) as string

tell application "Terminal"
	activate
	do script (base_path & " &")
end tell

On the first line path to resource" base.app / ... " ** Get the absolute path of "base.app / ..." in the resource folder of the app where this script is executed * *. Since the acquired path is an alias reference, convert it to POSIX format and store the converted string type in base_path.

Start Terminal with tell application" Terminal " and activate it. Terminal will now be displayed on the screen. Then execute the executable file in base.app. At this time, adding & to the end will result in background operation, and ** the program will be executed even if this script ends ** (conversely, if you do not add &, the program must end on Terminal. wrap.app does not end either.)

3. Create wrap.app based on the script

Create wrap.app based on wrapper.scpt created above. This operation is very easy. Just do the following on the Terminal: $ osacompile -o wrap.app wrapper.scpt This will create wrap.app.

4. Store base.app in the resource folder inside wrap.app

Finally, store the base.app of the app you want to run on the console created in 1. in the resource folder in wrap.app. $ cp base.app wrap.app/Contents/Resources/base.app

reference

http://qiita.com/mattintosh4/items/83e1540c31c803c3fd5e http://qiita.com/mattintosh4/items/3220a75ae6229553b87b

Recommended Posts

Make your python CUI application an app for mac
Make Qt for Python app a desktop app
Building an environment for executing Python scripts (for mac)
Set Up for Mac (Python)
Python environment construction For Mac
Python 3 series installation for mac
Qt for Python app self-update
How to make a Python package (written for an intern)
[Python] Make your own LINE bot
Try docker: Create your own container image for your Python web app
An app for smart people and smart people
Python3 TensorFlow for Mac environment construction
An introduction to Python for non-engineers
[Definitive Edition] Building an environment for learning "machine learning" using Python on Mac
Explanation of creating an application for displaying images and drawing with Python
Build an environment for Blender built-in Python
Solution for pip install error [Python] [Mac]
Create a python environment on your Mac
Creating amateur python environment settings (for MAC)
Make your own PC for deep learning
[Mac] Building a virtual environment for Python
[Python] Web application design for machine learning
An introduction to Python for machine learning
Create an English word app with python
Make a desktop app with Python with Electron
An introduction to Python for C programmers
How to build an environment for using multiple versions of Python on Mac
The road to updating Splunkbase with your own Splunk app for Python v2 / v3
Create an app that guesses students with python
[Python] Make a game with Pyxel-Use an editor-
[Python] matplotlib: Format the diagram for your dissertation
Building an Anaconda environment for Python with pyenv
Tweet (API 1.1) on Google App Engine for Python
Make your own module quickly with setuptools (python)
Building an environment for matplotlib + cartopy on Mac
Build a Python development environment on your Mac
[Introduction to Udemy Python3 + Application] 43. for else statement
Don't use readlines () in your Python for statement!
[Blender x Python] How to make an animation
[Python] Predict the appropriate rent for an apartment
Create a Mac app using py2app and Python3! !!
Let's make a module for Python using SWIG
How to make Python faster for beginners [numpy]
python: Use your own class for numpy ndarray
Make your Python environment "easy" with VS Code
Let's make an app that can search similar images with Python and Flask Part1
Let's make an app that can search similar images with Python and Flask Part2
Get an Access Token for your service account with the Firebase Admin Python SDK
[Python] Building an environment for competitive programming with Atom (input () can be used!) [Mac]
I tried to build an environment for machine learning with Python (Mac OS X)