Run python script from Finder rather than edit?

6

1

Max OS X. When I double-click on a python script it opens in the default editor. How might I make it so that when I double-click, or choose the script from Applications, it executes from the python interpreter? From the "open with" option it does not appear that I can choose /usr/bin/python.

Charlie Wilson

Posted 2013-05-09T04:07:02.787

Reputation: 501

Answers

8

  1. Start up Automator
  2. Choose application
  3. In the Actions bar, select Library, and search for Shell
  4. Drag "Run Shell Script" to the right pane.
  5. Change pass input to "as arguments"
  6. Change the script to

    for f in "$@" do python "$f" done

  7. Save it (possibly in the application folder, but can be anywhere) as Python.app

Now if you want .py files to always launch with python:

  1. Select a .py file
  2. File -> Get Info
  3. In the Open with selection, choose Other, and select the Python.app you created.
  4. Click Change All

David Miani

Posted 2013-05-09T04:07:02.787

Reputation: 256

step 6 of the accepted answer needs to be formatted correctly to work for f in "$@" do python "$f" done – borgified – 2016-04-23T06:53:15.930

1Joined just so I could upvote this. – ArtOfWarfare – 2013-07-24T12:50:11.450

1:-/ unfortunately it's simply not working... I have no idea why. Any step having python run a script simply has automator say the Run Shell Script failed, with no description of what went wrong. – ArtOfWarfare – 2013-07-24T13:17:11.540

1

Seems like it would be easier to just fire up the terminal and type "python /path-to-script/scriptname"? If not, there's apparently a way to get Apple script to do this, according to this stack overflow post: https://stackoverflow.com/questions/14793391/easy-way-to-launch-python-scripts-with-the-mouse-in-os-x

In either case, you'll need to know the full path to the script.

erewok

Posted 2013-05-09T04:07:02.787

Reputation: 531

For the problem I am trying to solve it isn't easier but yes, I could execute the script from the terminal. Thanks for the info. – Charlie Wilson – 2013-05-09T04:49:45.253

1

I suggest you install the latest version of Python (not the one installed on Mac OS X as of Mountain Lion). It includes PythonLauncher that does just what you want.

  1. Install the latest version of Python (download)
  2. Run "Update Shell Profile.command" (in Applications/Python 2.7) to make sure python is in your shell's PATH
  3. Select a .py file
  4. File -> Get Info
  5. In the Open with selection, choose Other, and select the PythonLauncher app (in Applications/Python 2.7)
  6. Click Change All

Raph P

Posted 2013-05-09T04:07:02.787

Reputation: 11