4

task:
I would like to create my own file extension so that when I click on a particular "dot" file, my program will open it. However, this program I wrote, is just a middle-man in that it will do some processing, but ultimately pass that file onto another program for processing.

example:
So for example, all plain text files with the extension .foo will appear as though they are being opened with gedit. But what is really happening, is that they are being opened with one of my programs, and in turn, my program passes that file onto gedit.

my c++ program to do this, looks like this:

#include <string>
#include <cstdlib>

int main(int argc,  char** argv) {

    //open file with gedit
    system(("(gedit " + std::string(argv[1]) + " > /dev/null &)").c_str());

    //do other processing
    //...
}

I believe that when you click on a file and the operating system tells it to be opened with a certain program, that file name is passed to a c++ program as the 2nd argument of that program(as index 1, since index zero is the program's name). The & is so that gedit is run as a background process, and the extra () around the statement ensures that gedit will not be closed when the parent shell is closed.

problem:
With all that being said, this program works correctly when I have the binary sitting in a folder that is listed in my $PATH variable and I run the program from the command line passing in the file as if it were clicked. However, it does not work when I actually set the file to be opened with the program.

how I've set up running a program on file click:
I ran the script found in the answer of this question,
and the program DOES show up when I Right Click->Properties->Open With enter image description here
but nothing appears to happen when I click the file. I even put the program in an infinite loop and checked to see if it was running as a process, but it was not. Why is the program not being called at all? Or does it get called and just closes immediately for some reason? Does that script not do everything I needed it to? Are there additional steps I need to take? Should I try to register the program and associate it with the file extension in a different way?

I am on Ubuntu.

Community
  • 1
  • 1
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • @It does differ from the system path. It's in some local folder that I added to the path myself. Does it have to be in /usr/bin? Oh you know what, I just used the program name instead of a full path. I will try to use a full path and update my answer whether that works or not. – Trevor Hickey Jan 31 '14 at 12:57
  • Where did you put `test_program`? "Somewhere on your path" might not be good enough if **your** path differs from the **system** path. You might want to add code that creates a file in `/tmp` to conclusively check if your program is being invoked. – user4815162342 Jan 31 '14 at 12:59
  • Your `PATH` is typically modified in the shell, and only affects the shell and the programs started from it. The desktop environment, on the other hand, starts up before the shell does, so modifications of `PATH` from the shell do not affect it. The system most likely can't find your program. The standard "system" place for installing small non-system programs is `/usr/local/bin`. – user4815162342 Jan 31 '14 at 13:00

1 Answers1

0

You might just open a terminal and investigate the environment:

int main(int argc,  char** argv) {
    std::system("gnome-terminal");
}