1

I created a program to be invoked as a default for certain file types. I “Open [any file] with…” it - the file “test.txt” is created, but no I/O with my program is possible. In order to be able to communicate with my program I created an “Application in Terminal” launcher (shortcut, label, desktop icon) for my executable. Now I “Open [any file] with…” it - no file “test.txt” is created. Why?

#include <stdio.h>

char pcPW[1000];
FILE *fp;

int main()
{
    printf("Start.\n");

    fp = fopen("/home/kkk/build-ert-Desktop-Debug/test.txt", "w+");
    fprintf(fp, "Start.\n");
    fclose(fp);


    scanf("%s",pcPW);

    printf("pcPW:%s\n",pcPW);

    fp = fopen("/home/kkk/build-ert-Desktop-Debug/test.txt", "a+");
    fprintf(fp, "pcPW: \n%s\n", pcPW);
    fclose(fp);

    return 0;
}

Screenshots: http://www.filedropper.com/downloads_87

Kosarar
  • 171
  • 3
  • 9

2 Answers2

2

You can add a custom script to do what you want, but there's couple tricky things that you want to keep in mind:

  • Your program is in C/C++. This means , that the program will have to be compiled into a binary first. Depending on whether or not it uses any special libraries, you might have to do so from command-line. gcc compiler by default creates a.out file, which can run from terminal ( i.e. executable permission is already set ). The simple compilations can be scripted, but for more complex you'll need to do that from terminal.

  • Running a program that prints to terminal requires having a terminal in the first place. Thus, you need to spawn terminal window first, and then run the program.

So here's the script to do the job:

#!/usr/bin/env python
from os import path
from sys import argv
from subprocess import call

for item in argv[1:]:
    full_path = path.abspath('./' + item)
    try:
        call(['gcc',full_path])
    call(['gnome-terminal','-e', 
          "bash -c './a.out;read'"])
    except Exception as e:

Save that as ~/.local/share/nautilus/scripts/compile_and_run.py , make sure it has executable permissions, and test.

enter image description here

And if everything is successful, that's what you'll see:

enter image description here

1

I created a program invoking a program invoking a program. The key line in the first is:

 execl("/usr/bin/x-terminal-emulator", "/usr/bin/x-terminal-emulator",
        "-e", "/home/kkk/build-untitled-Desktop-Debug/untitled",
        "/home/kkk/Downloads/1.pdf", (char*) NULL);

Thank you, Serg, for a great tip and 96% of the solution.

Zanna
  • 72,312
Kosarar
  • 171
  • 3
  • 9