How to run Python script in a terminal window by double clicking it?

6,754

Solution 1

Here's a better solution to your problem if you absolutely need it to be a .app bundle:

In your Converter.app/Contents/MacOS/Converter script, launch your Python script with the "open" command:

open $(dirname "$0")/../Resources/Converter.py

(I did a relative pathname trick above so you're not hard-coding an absolute path that would break if you moved your .app around.)

Make sure your Resources/Converter.py script has a Python shebang at the top:

#!/usr/bin/python

Make sure both MacOS/Converter and Resources/Converter.py are marked executable (use chmod a+x filename on them).

I tested this setup and the open command successfully figured out that a .py file should be opened in Terminal without me doing anything. I don't think I've previously associated .py files with Terminal.app before on this system, so I think these instructions wouldn't need any further steps even on a clean system.

Solution 2

The easiest way to make a double-clickable script that runs in a new Terminal window is to end the file name with ".command". Then you can double-click on the script file itself, no need for a .app bundle/package.

If you want to execute bash commands before Python, you can put both in the same file but wrap the Python code in a bash exec statement. TCL scripters do it this way a lot. If you just want Python code and no bash code, change the shebang at the top to #!/usr/bin/python (or wherever Python lives on your system).

I personally don't like anything that presents itself as a .app but actually runs in a Terminal window instead of being its own real app. The only time I've seen this done was the Ixia Chariot endpoint for OS X. I haven't looked at it in a few years, but it may still work that way. It's a free download. You might want to grab it and look at how they did it and maybe post an Answer to your own question. I think their solution involved packaging a Terminal settings file in their app. I think it caused a separate instance of Terminal.app to get launched with their weird custom settings, which was annoying in a fresh and exciting way. :)

Share:
6,754
Steampunkery
Author by

Steampunkery

I love programming, I know HTML, Python, Javascript, Java and CSS, I run Kali linux and am a white hat hacker. Some reasons I enjoy programming are, I can now understand most xkcd jokes and I get an intimate knowledge of the computer and its' operating system (which allows me to be a pseudo IT/tech support helper for my family).

Updated on September 18, 2022

Comments

  • Steampunkery
    Steampunkery over 1 year

    I am running OSX, and I am making an app that has a Python program in the Resources folder, the Bash script calls the Python script to run when the app is launched. The script is in the MacOS folder within the app, named the same as the app. I have this Bash script:

    #!/bin/bash
    clear
    python /Applications/Converter.app/Contents/Resources/Converter.py
    

    I thought that the Bash script would execute when the app was launched, thus causing the python script to run through a terminal window. However when I double click the app, nothing happens!

    So basically, I want the bash script to open the Python program in a terminal window so the user can interact with it.

    What am I missing here?

    • Steampunkery
      Steampunkery over 8 years
      How would running in the background help?
    • Steampunkery
      Steampunkery over 8 years
      When I double click the app, nothing happens! I want the bash script to open the python program in a terminal window so the user can interact with it
    • Spiff
      Spiff over 8 years
      Are you missing an "s" in "/Applications/" in your path in that script?
    • Steampunkery
      Steampunkery over 8 years
      let me check....that may be a typo when I typed in the script to the question.
    • Steampunkery
      Steampunkery over 8 years
      no, the script has a "s", I'll edit the question
  • Steampunkery
    Steampunkery over 8 years
    I need it to be an app. When I change it to Converter.command, it says that the app can't be launched because it is incomplete or corrupt.
  • Spiff
    Spiff over 8 years
    I don't think you tested your own instructions to see if they would meet OP's requirements. These instructions would cause the shell script to get executed, but they would not cause it to be executed within a Terminal window like OP wants. DropScript is from 2002 and is PowerPC only. Platypus may be a viable solution, but OP seems to want to figure out how to do it himself.