Python script from FastScripts
  • Hi there,

    I would like to run a Python script from FastScripts that opens the Terminal and prints something. I am having a bit of trouble doing this.

    The contexts of the script are as follows:

    #!/usr/bin/env python
    print "hi there"

    I have tried including the path to the script on the first line as well - and nothing happens. I have also tried changing the extension to '.command' and making the script executable in the Terminal using 'chmod +x path_to_script'. After this, I can double click on the script in the Finder and it works - but still nothing from FastScripts.

    I appreciate any advice. Thanks.
  • Hi there - can you give me a little more information about what your end goal is? Is it to run a command in the Terminal specifically?

    Shell scripts run in FastScripts do not rely upon or expect the Terminal to be open, so it is normal that it isn't opening the Terminal when you run the script.

    I am a little surprised to hear that changing it to a .command doesn't work as expected. I just confirmed that FastScripts treats the ".command" as a regular script and just tries to run it as any other script.
  • Hi - Sorry if I was unclear. Yes, I would like to run the Python script in the Terminal in Python (in other words, I would like the same effect as typing the command 'python /path/to/script.py' into the command line).

    It would by ideal to just drop the script.py file into my FastScripts directory and run it from the menubar. However, if this is not possible, can you let me know the best way to do this? Thank you.
  • Sorry if I'm being dense, but I'm still not sure if it's really important to you that the Terminal window be opened when your python script runs. You said you want to have the equivalent of typing "python /path/to/script.py" from the command line, and as it happens the equivalent of this *does* happen when you run a script.py that happens to be in FastScripts's scripts folder, but of course if there is meaningful output then you can't see it. Is that the key difference, that you need to see the output of the script?
  • Thanks again for your response - I think you are right that the key difference is seeing the output of the script in Terminal. Actually, the script needs to get some input from the user, and so I thought that the script needs to run in a Terminal window in order to do this.

    I do understand what you are saying - the script is run without opening up a Terminal window. If that is the case, and the script is asking for user input, does the script just fail to run quietly?

    For example, I would like to run the following script from FastScripts (such that the user can give input when necessary):

    #!/usr/bin/env python
    print_statement = raw_input("What do you want to print? ")
    print print_statement

    When I do this, as you say, no Terminal window opens (which makes sense to me). I can get my desired effect from the following AppleScript (assuming the script is on the Desktop):

    set file_path to "~/Desktop/script.py"
    tell application "Terminal" to do script "python " & file_path

    Is there a way to achieve this same effect by simply dropping my 'script.py' file into my FastScripts directory?

  • Daniel JalkutDaniel Jalkut
    Accepted Answer
    Thanks for elaborating ... this is an interesting case where I think FastScripts could evolve to handle shell scripts better. Currently it doesn't have any features supporting either input or output to shell scripts in the way you're describing.

    Unfortunately I think your workaround is the best solution I can think of in the short term to get the kind of terminal-based interactivity you're looking for. In the future I think it would be cool if FastScripts was able to provide it's own kind of "terminal" for interacting with users in cases where a shell script has output or demands input. It's something I'll be considering.
  • Oh, and an alternative to having the script reside somewhere on disk would be to package it all up into an executable script that can be run by FastScripts but still achieve the same thing. For example you could have your script write the desired Python script to a temporary location before passing its path to the Terminal "do script" command.
  • Okay. Thanks for your patience in answering this question. I understand better now how FastScripts works. I will look into what you suggested. Cheers.
Start a New Discussion

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!