2011-09-13 60 views
3

我想創建一個調用簡單Python腳本的簡單Mac應用程序包。我想用Python做到這一點。如何通過Python爲Python腳本創建Mac應用程序包

有沒有簡單的方法?

我試圖用py2app但失敗不知何故,如:

from setuptools import setup 
setup(app=["foo.py"], setup_requires=["py2app"]) 

給出:

--------------------------------------------------------------------------- 
SystemExit        Traceback (most recent call last) 
/Users/az/<ipython console> in <module>() 

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.pyc in setup(**attrs) 
    138   ok = dist.parse_command_line() 
    139  except DistutilsArgError, msg: 
--> 140   raise SystemExit, gen_usage(dist.script_name) + "\nerror: %s" % msg 
    141 
    142  if DEBUG: 

SystemExit: usage: ipython [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] 
    or: ipython --help [cmd1 cmd2 ...] 
    or: ipython --help-commands 
    or: ipython cmd --help 

error: no commands supplied 
Type %exit or %quit to exit IPython (%Exit or %Quit do so unconditionally). 

我也試過:

import py2app.build_app 
py2app.build_app.py2app("foo.py") 

也不起作用( TypeError: dist must be a Distribution instance)(我真的不知道如何使用py2app.build_app.py2app,也沒有真正發現太多的例子/做對它的抱怨)。

也許setuptools/py2app左右反正是我的用例矯枉過正。我只想創建一個簡單的空應用程序包,將Python腳本複製到其中,並以調用Python腳本的方式配置其Info.plist。

+0

你有沒有嘗試以下http://packages.python.org/py2app/tutorial.html#create-a-setup-py-file ?因爲你所概述的步驟似乎並不反映這個或setuptools教程。 – millimoose

+0

@Sii:我不想創建setup.py文件。我想從另一個Python腳本中完成它。 – Albert

+0

一個選項是使用OSX Automator:http://stackoverflow.com/a/31638867/191246 – ccpizza

回答

10

這正是我想要的,只是正常:

#!/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 plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="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) 
+1

你可以看看Platypus,它從解釋腳本創建Mac應用程序包裝。 http://sveinbjorn.org/platypus – svth

4

結賬PyInstaller

您可以將它指定到您的python腳本的路徑,並分析您的所有軟件包導入,提取所需的任何二進制文件並將它們放入歸檔中。

我用它來做一個相當複雜的python程序,它對我很有用。

+0

我不想要那麼多。我只想創建一個空的應用程序包。 – Albert

+0

然後你的應用程序將不可移植。 – jterrace

+0

爲什麼不呢?看到我自己的答案。這應該是便攜式的。 – Albert

相關問題