将 python 脚本捆绑到 OS X 应用程序中,但是
我目前有一个脚本,用于从几个存储库更新文件。如果我使用以下命令执行该脚本,则该脚本可以正常工作:
python myscript.py
但是,当我尝试捆绑到应用程序中时,它不再正常运行,因为我的脚本的一部分需要用户输入。但是,没有可供其使用的界面。我尝试将它与 py2applet、Platypus(带有文本输出)捆绑在一起,但我一直无法找到一种让输入工作的方法。
someVar = raw_input("Enter some file name here: ")
所以,我的问题是:从捆绑的 python 应用程序获取用户输入的最佳方法是什么?
I currently have a script that I'm using to update files for me from a couple repositories. The script works fine if I execute it using:
python myscript.py
However, when I attempt to bundle into an App, it no longer functions correctly because a part of my script requires user input. There is no interface, however, for it to use. I've tried bundling it with py2applet, Platypus (with the Text output), but I haven't been able to find a way to get input working.
someVar = raw_input("Enter some file name here: ")
So, my question is: What's the best way to get user input from a bundled python app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将参数传递到 python 脚本中,这样您就不需要 python 脚本来实际获取它们。而是将它们放入您的可可应用程序中。在 cocoa 应用程序中请求输入,然后使用 NSTask 运行 python 脚本,就像运行任何其他命令行程序一样,并传入参数。以下是如何在 python 脚本中获取传递的参数。第一个参数可以通过以下方式获得...
要捆绑 python 脚本,只需将其添加到您的项目中即可。确保脚本具有 shebang 并且可执行,如上所述。您可能需要通过添加新的“复制文件”构建阶段并向其中添加脚本来手动强制将其添加到您的应用程序包中。
You can pass arguments into a python script so you don't need the python script to actually get them. Get them in your cocoa app instead. Ask for the inputs in the cocoa app, then run the python script using NSTask like you would any other command line program and pass in the arguments. Here's how you get the passed args in the python script. The first arg can be gotten with this...
To bundle the python script just add it to your project. Make sure the script has the shebang and is executable as explained above. You'll probably have to manually force it to be added to your app bundle by adding a new "copy files" build phase and adding the script to that.
您可以通过 AppleScript 使用 GUI 提示输入。只需调用 /usr/bin/osascript,显示一个带有文本字段的窗口并返回它。
请参阅此处查看示例(尽管是在 Perl 中)
You could prompt for input using the GUI via AppleScript. Just invoke /usr/bin/osascript, show a window with a text field and return it.
See here for an example (albeit in Perl)
不要搞乱“捆绑”应用程序。只需使用命令行即可。
告诉您的用户打开终端窗口并从命令行运行您的应用程序。为大家省去了很多痛苦。
用户并不“需要”捆绑应用程序。他们可以打字。尝试一下。
Don't mess with a "bundled" app. Just use the command line.
Tell your user to open a terminal window and run your app from the command line. Saves everyone a lot of pain.
Users don't "need" bundled apps. They can type. Try it.