2

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?

jigfox
  • 18,057
  • 3
  • 60
  • 73
Kevin Gao
  • 61
  • 7

3 Answers3

2

@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.

regulus6633
  • 18,848
  • 5
  • 41
  • 49
0

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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • add `#!/usr/bin/python` as first line to your script. run `chmod +x path_to_your_script/myscript.py ` and put the script in some directory which is in the `PATH` environment varibale. Then everyone can run the script by just calling `myscript.py`. You can even remove the `.py` to make it look like all the other command line tools. – jigfox Jun 22 '10 at 10:24
  • Thanks for the advice. I would love if people could just learn how to use the command line, but I feel like the reality is that many people don't *want* to. For now, I will go with your suggestions, but for the sake of learning, is there any way of going about the original request? By the way, I am currently using #!/usr/bin/env python as my shebang. Is there any benefit to using #!/usr/bin/python besides saving a few characters? – Kevin Gao Jun 22 '10 at 11:35
  • 1
    I actually thought #!/usr/bin/env python was a bit more robust. – Kevin Gao Jun 22 '10 at 11:41
  • Nothing's really going to change. Python will probably always stay at /usr/bin/python, so it doesn't really matter. – johankj Jun 22 '10 at 13:15
  • 1
    If you are using python for creating an app intended for users and not devs, this is a really good way to ensure you apps stays stillborn. While I would love for there to be more programmers, most users aren't - and therefore will not be able to work with the command line. – Danny Staple Jan 22 '12 at 13:40
  • @DannyStaple: Really? Do you have evidence of this? Some study or survey? – S.Lott Jan 22 '12 at 18:21
  • 1
    I'll admit my evidence for this is anecdotal. My experience is that users are often both lazy (RTFM, I wish - I've seen many developers who won't), and won't see past the end of their nose. They certainly won't fire up man, work out the params and edit the config with vi. This is not a sleight on the users - many just want to get on with something and not get distracted by the mechanics of doing so. While I find CLI systems productive, my wife, accountant and postman cant be expected to. – Danny Staple Jan 23 '12 at 20:49
  • @DannyStaple: Since the input is interactive -- `raw_input`, all of the "fire up man, work out the params and edit the config with vi" seems utterly irrelevant. If the app asks a question at the command-line, it certainly seems like most people can type an answer. – S.Lott Jan 23 '12 at 20:51
  • @DannyStaple: Also. We don't know one thing about the potential users for this app. It could be that the entire user population is professional programmers. It's difficult to guess from a question with so few facts. – S.Lott Jan 23 '12 at 21:24
  • "Evidence that many users will refuse to use a command-line?" Is this question being asked via a web-browser? – JS. Jan 24 '12 at 22:36
  • @JS. The question is perfectly unclear on that. It seems impossible that someone could be **that** confused. However, asking as comment on this answer will reveal noting. You might want to ask on the question itself. – S.Lott Jan 24 '12 at 23:18
0

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)

Community
  • 1
  • 1
svth
  • 1,303
  • 1
  • 14
  • 23