Hi there.

I have made a program in Python with PyGame and PyOpenGL to test out vertex and pixel (fragment) shaders. I would like to be able to right click on the shader file (just a text file with a custom extension) and choose Open With... and then open with my Python program. How could I go about doing this? I am willing to convert to an executable with Py2Exe if the need arises.

If anyone wants to see the code for their own interest, I will put it up.

Thanks in advance :)

Recommended Answers

All 8 Replies

No, it's a python question. What would I need to put in my code that means I will have the file path to what the user opened with my program/script? I know how to do everything in those two links. If it was a windows question, I would have put it in the windows forum ;)

Yeah thanks :) I'll give it a go.
I think the OP of that thread worded it better than me :/

I managed to get that working with a test script:

import sys
import os
for arg in sys.argv:
    print arg
os.system("pause")

I had to use py2exe to compile it to exe, as I couldn't get it to work as a python script. I implemented this feature in my main program, but that won't compile with py2exe.

Looking at the code above, is there a way that I can double click on this so it will run and read the contents of "sys.argv" into the file?

Don't worry about that last post. I managed to get it to work from CMD. Now I'm writing a batch script/exe so that I can run it on double click.

Could you please post a snippet about how you solved the actual problem?

I ended up doing it in C++ seeing as I couldn't compile my code with Py2exe. Here's the thread:
http://www.daniweb.com/forums/thread330291.html

Here's the C++ code: [B][I][U]C++ SYNTAX!!![/U][/I][/B]

#include <iostream>
//for strings
#include <string>
//for c_strings
#include <cstdlib>
//standard namespaces
using namespace std;

//we need these args. for main
int main(int argc, char *argv[])
{   
    //file path of script. NOTE THE SPACE!!!
    string toCall="<Edited out the path>\test.py ";
    //path to opened file (opened with right click -> open with...)
    string file=argv[1];
    //add the strings, this will create "<path>/test.py example.txt"
    string finalCall = toCall + file;
    //call from CMD (windows only)
    system(finalCall.c_str());
}

And here's the bit of python code you need:

//needed to get system args
import sys
//incase we opened the script without having a value
//for argv[1]
try:
        //open the file in "argv[1]", which in above C++ code
        //is "example.txt"
	file = open(sys.argv[1], "r")
//on epic fail
except:
        //exit the program
        sys.exit

Basically, you are just using the command test.py example.txt , which can be done from the command line. I only made an exe so I could then make it so I can double click on the file and open it.

Unfortunately, when I added that in (which now works), I managed to break something else in the script so I've got to try and fix it all now.

Please upvote if this helped :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.