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?

1
  • Take a look at the answer here Commented Mar 24, 2015 at 16:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your replay. But I want command to be issued when user double clicks on a particular file with specified extension
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 might help:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.