0

I have a python program which takes a particular file extension as an input. So whenever a user clicks on that particular file with that extension, I have to run my python program with that file path as an argument. How can we accomplish this in Mac OSX?

Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
Bharath
  • 311
  • 1
  • 3
  • 5
  • Take a look at the answer [here](http://stackoverflow.com/a/14793903/648852) –  Mar 24 '15 at 16:01

1 Answers1

0

This simple program will get you started.

#!/bin/python

import os
file_ext_dic = {
    'pdf' : 'evince %s ',
    'c'   : 'gvim %s ',
    'py'  : 'python %s '
    }

if __name__ == "__main__":
    argv     = os.sys.argv
    argc     = len(argv)
    filename = ""
    extension= ""

    if (argc == 2):
        filename = argv[1]
        fileparts=filename.rsplit(".")
        key=fileparts[-1] #file extension
        if file_ext_dic.has_key(key):
                cmd_to_open=(file_ext_dic[key]%filename)
                os.system(cmd_to_open)
        else:
                print 'key "{0}" not found'.format(key)
    else:
        print 'no filenames passed'

It doesn't include all validations and you could add more functionalists to extend this further as per the need. The one useful extension would be to add additional settings to the command associated with the file extension.

  • Thanks for your replay. But I want command to be issued when user double clicks on a particular file with specified extension – Bharath Mar 24 '15 at 15:51
  • are you looking for a mac GUI frontend for command line python scripts ? if so, not sure about making python to do it. this [thread](http://stackoverflow.com/questions/14793391/easy-way-to-launch-python-scripts-with-the-mouse-in-os-x) might help: – Rameshkumar R Mar 24 '15 at 16:05