2011-09-13 12 views
3

単純なPythonスクリプトを呼び出す単純なMacアプリケーションバンドルを作成したいと思います。私は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スクリプトをコピーして、Info.plistをPythonスクリプトを呼び出すように設定したいだけです。

+0

あなたはhttp://packages.python.org/py2app/tutorial.html#create-a-setup-py-file以下しようとしました?あなたが概説しているステップは、それを反映していないか、setuptoolsのチュートリアルであるためです。 – millimoose

+0

@Sii:私はsetup.pyファイルを作成したくありません。他のPythonスクリプトの中からやりたい – Albert

+0

1つのオプションは、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

あなたは、解釈されたスクリプトからMacアプリケーションラッパーを作成するPlatypusを見てみることができます。 http://sveinbjorn.org/platypus – svth

4

PyInstallerをご覧ください。

Pythonスクリプトへのパスを与え、すべてのパッケージインポートを分析し、必要なバイナリファイルを抽出してアーカイブに配置します。

私はこれをかなり複雑なPythonプログラムに使用してくれました。

+0

私はすべてを望んでいません。空のアプリケーションバンドルを作成したいだけです。 – Albert

+0

あなたのアプリは移植可能ではありません。 – jterrace

+0

なぜでしょうか?私自身の答えを見てください。それは移植可能でなければなりません。 – Albert

関連する問題