Bundle a python script into an OS X App, but - 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?

#sudowork: For now, I will go with your
suggestions, but for the sake of
learning, is there any way of going
about the original request?
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...
someVar = sys.argv[1]
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.

What's the best way to get user input
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.

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)

Related

How-to Launch Android Python Script via Shortcut on Homescreen

I've scoured the interwebs and couldn't find anything with python, android, and "shortcut" or "home-screen" to appear on the same page. I have pydroid3 installed, working great. I'd like to have a shortcut (ideally on the home-screen) that I can tap once and have it run without opening the IDE for editing.
Making a shortcut to the file, or opening a source file in the file manager will at best just open it in the pydroid3 IDE, at worst state "this file type is not supported."
At the beginning of the script, I have tried putting "#!/user/data/../pydroid_dir/python", but alas the OS doesn't realize I mean to run it directly in the python interpreter. Any solutions or alternative lines of thinking are appreciated!
EDIT
I'm running an unrooted android 9 PIE. I wanted to provide some more details but not quite a solution for any readers. Check out related question: How to create a homescreen shortcut to launch a shell script?. Closest I got was trying SManager which allows you to create a homescreen shortcut to a shell script. Your shell script could be hardcoded to call python on your script:
/path/to/python /path/to/python/script
Unfortunately, if you're phone is not rooted, you may not have permission to execute whatever version of python you're calling outside of the context of the app it was installed for. Also, you have to ensure that your shell and python scripts reside in an appropriate place for execution. If your phone is rooted, SManager seems to have options to let you run the script as root.
pydroid3 allows me to run scripts residing on my internal storage as well as lets them read and write files at that location. That's why I was hoping that there was a pydroid way to create a shortcut (or an alternative python app that does this) since it has appropriate privileges.
QPython OL lets you create home screen shortcuts to python scripts on Android. First tap on the 'Q' icon at the top of the main screen (it took me a while to realise this was a button), then long-press on the script you want. This should give you a prompt to create a shortcut, as in the screenshots below.
QPython 3L also claims to have similar functionality when you long-press a script in its 'Programs' section. At time of writing (Jun 20) this seems to be broken.
I've not tried Pydroid myself, but haven't come across anything claiming it could create script shortcuts either.
You can use Termux:Widget as a command line to execute a python script. In Termux you can not create GUI with tkinter. You have to launch X-Server with VNC Mobile client. The widget is like a small shell prompt.

How do you pass in default values to a subprocess program that asks for command-line arguments?

I know it is quite possible to run programs from within a python script using the subprocess module, but what I can't seem to figure out is if it's possible to go a bit deeper and pass in default arguments to a subprocess that requires command-line inputs. Please see a specific example below:
I want to create a new vue project using the vue-cli from a python script because aside from initializing the project I want to automatically copy/move files around within the new project directory space. So after doing some setup I want to have subprocess run the following code: subprocess.run(['vue', 'init', 'web pack-simple', '<project-name>'] which is the equivalent of running $ vue init web pack-simple <project-name> directly in the command line.
Here is where I would please like some advice. When directly running $ vue init web pack-simple <project-name> in the command-line I then go through a CLI where it asks me the project-name, and then you hit 'Enter' and then it asks you for a project description, and then you hit 'Enter' and then it asks if you're using sass, etc.
Is there some way through the subprocess module that I can pass in and enter default/specified values automatically into this vue-cli such that whenever the vue-cli asks for project-name and project-description, etc. those are automatically filled out?
Thanks a lot in advance!

Batch Rendering file from a python script without openeing Maya

I have one Maya scene and a Python script where import obj files into it. I need to create a batch render file which calls the maya file and applies the script without opneing maya.
I have this code in a .sh file:
#!/bin/bash
"/Applications/Autodesk/maya2016/Maya.app/Contents/bin/Render" -r file -s 1 -e 4 -cam camera1 -rd "/Users/MyComp/Documents/maya/projects/default/images" "/Users/MyComp/Documents/maya/projects/default/Scenes/test1.mb"
But I have this code into the script which can be an issue or maybe not:
def renderFile(i):
cmds.setAttr("defaultRenderGlobals.imageFilePrefix", i, type="string")
cmds.render(batch=True)
If I execute this .sh file it renders without the python script. How can I add the python script?
I need that file for a renderfarm purposes
I know it's an old thread but thought I'd jump in just incase someone finds this thread in a search.
The comments seem a little confused. This comes from the fact that there are two different Python interpreters being talked about. The first is the system level one, which the original question seems to be talking about. In that case, you can use any of the various shell command launchers (like, subprocess/Popen) that suit your need. Here you are looking to run the render command like you would any other command in in the shell.
In the responses, people there are referring to the other interpreter, the custom Maya Python interpreter (mayapy.exe). In that case you are working with actual Maya libraries and it's the same as working with Python in it's shell, with the added Maya libraries/environment.
The two have different uses, the first is to control things like they were in the shell and the second is controlling things inside of a Maya context. Hope that clarifies things.

How to make it extremely easy to run a python script for a user

I built a python/bash application for OSX that I want to distribute to non techsavvy users. My problem is how to get the user to initially run my python script. The script sets up a cron job so the user never has to do anything again but the user MUST run it the first time. The issue is that python and bash scripts default to non-executable when unzipped. As of now I have instructions in the readme.txt for the user to open the terminal and copy and paste the command which chmods the script and runs it for them. However is there anyway I can accomplish this without having the user open the Terminal? Ideally the user could just double click the python script and have it run. Thanks.
Sometimes being user-friendly warrants using platform-specific features. In this case, the Mac OS X "Installer" system: http://en.wikipedia.org/wiki/Installer_(OS_X)
Rather than a .zip file, you can create a .pkg file which users can download and install just as they would any other program native to OS X. This should give them a familiar experience, and you can make the .pkg file do what you need behind the scenes.

Is it possible to create Python-based Application in Xcode or equivalent?

So I have a lot of python scripts that I have written for my work but no one in my lab knows how to use Python so I wanted to be able to generate a simple Mac App where you can 'Browse' for a file on your computer and type in the name of the file that you want to save . . . everything else will be processed by the application for the python script I have generated.
Does anyone know if this is possible? I watched some tutorials on people generating applications in Xcode with Objective C but I don't want to have to learn a new language to reconstruct my Python scripts.
Thank you
If you only want this to run on OS X, you could use pyobjc (see https://stackoverflow.com/a/2393530) to develop a Cocoa application, but there are also quite a few cross-platform GUI frameworks for Python, which will work on OS X as well as other operating systems.
For me, the easiest way to do this would be using the Automator program that comes with OSX. Create a new application, then choose Run Shell Script from within the Utilities sub-category. The pop-up at the top of the resulting window has a /usr/bin/python option. Put your python in there. You can connect inputs and outputs to that script using the Ask for Text item, etc...
The input from Ask for Text are most easily obtained if you choose "as arguments" from the Pass input: pop-up in the right of the Run Shell Script module. They come in as a list of strings, but not split on spaces like they would be in a normal line.
Try this code and I think it will become clear (for this test, put several strings separated by spaces as the text value that you pass):
import sys
print sys.argv
items = sys.argv[1:][0].split()
for f in items:
print f
Open Automator
Choose "Application"
Drag a "Run Shell Script" onto the workflow panel
Choose "/usr/bin/python" as the shell. Paste in your script, and select Pass Input: "to stdin"
Or, choose bash as the shell, and simply have the automator script run your Python script with Pass Input "as arguments" selected on the top right. You'll then use the contents of $# as your arguments.
Save the application.
Done. You have a .app onto which files can be dragged.

Categories