Skip to content Skip to sidebar Skip to footer

How To Create Mac Application Bundle For Python Script Via Python

I want to create a simple Mac application bundle which calls a simple Python script. I want to do that in Python. Is there an easy way? I tried to use py2app but that fails somehow

Solution 1:

This is exactly what I wanted and works just fine:

#!/usr/bin/python

import sys
assert len(sys.argv) > 1

apppath = sys.argv[1]

import os, os.path
assert os.path.splitext(apppath)[1] == ".app"

os.makedirs(apppath + "/Contents/MacOS")

version = "1.0.0"
bundleName = "Test"
bundleIdentifier = "org.test.test"

f = open(apppath + "/Contents/Info.plist", "w")
f.write("""<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plistPUBLIC"-//Apple//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plistversion="1.0"><dict><key>CFBundleDevelopmentRegion</key><string>English</string><key>CFBundleExecutable</key><string>main.py</string><key>CFBundleGetInfoString</key><string>%s</string><key>CFBundleIconFile</key><string>app.icns</string><key>CFBundleIdentifier</key><string>%s</string><key>CFBundleInfoDictionaryVersion</key><string>6.0</string><key>CFBundleName</key><string>%s</string><key>CFBundlePackageType</key><string>APPL</string><key>CFBundleShortVersionString</key><string>%s</string><key>CFBundleSignature</key><string>????</string><key>CFBundleVersion</key><string>%s</string><key>NSAppleScriptEnabled</key><string>YES</string><key>NSMainNibFile</key><string>MainMenu</string><key>NSPrincipalClass</key><string>NSApplication</string></dict></plist>
""" % (bundleName + " " + version, bundleIdentifier, bundleName, bundleName + " " + version, version))
f.close()

f = open(apppath + "/Contents/PkgInfo", "w")
f.write("APPL????")
f.close()

f = open(apppath + "/Contents/MacOS/main.py", "w")
f.write("""#!/usr/bin/python
print "Hi there"
""")
f.close()

import stat
oldmode = os.stat(apppath + "/Contents/MacOS/main.py").st_mode
os.chmod(apppath + "/Contents/MacOS/main.py", oldmode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)

Solution 2:

Check out PyInstaller.

You give it the path to your python script and it analyzes all of your package imports, extracting any binary files that are needed and placing them into an archive.

I've used it for a fairly complex python program, and it worked for me.

Solution 3:

cxFreeze is best solution available as it is simple and time-saving.

first, create your program or application using python and then make setup file for your application.

And then build the app using build command python setup.py build, according to your requirement you need to make some changes, to make mac bundle or mac app refer this

Post a Comment for "How To Create Mac Application Bundle For Python Script Via Python"