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
  • 2
    @Zanna I wouldn't necessarily say so. OP wants to add their program to "Open With" right click menu from Nautilus file manager, so it's not specifically about programming. – Sergiy Kolodyazhnyy Jan 19 '17 at 15:18
  • @Zanna http://stackoverflow.com/questions/41729127/how-do-i-open-with-a-terminal-program-qt-creator-linux – Kosarar Jan 19 '17 at 15:36

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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • I can compile and run C++ from Python, cool (you did not write "hello_world.c", but maybe it is OK). That is very close to my task. However, my task is simpler. I want to use "Open With" on your second slide (actually, to make my program default "opener"). There is an obstacle somewhere. – Kosarar Jan 18 '17 at 21:01
  • I mean I try to open PDF file. My program should at least write that attempt down somewhere. Is it appear to be a need for 3-level architecture (I "Open [my PDF file] with" a Python script which in turn compiles and executes C file)? No simpler one? – Kosarar Jan 18 '17 at 21:14
  • @user3107513 well, if your goal is to simply associate a program with specific file type (for example, I made pdf open with Xpdf instead of default Evince ) there is a slightly simpler way: http://askubuntu.com/q/538299/295286 But if you want to open code AND run that code, then what I posted in my answer is probably a simplest solution - not ideal by any means, but simple enough. – Sergiy Kolodyazhnyy Jan 18 '17 at 21:28
  • And I didn't write "hello_world.c" because the filename will be passed as argument to the script via command-line argument , so when you right click on the file and select the script all that is being done for you already – Sergiy Kolodyazhnyy Jan 18 '17 at 21:28
  • I see. But may I use C++ program invoking C++ program, not Python program invoking C++ program? – Kosarar Jan 19 '17 at 13:30
  • @user3107513 Sure, you can do that as well, you'll probably want to use exec() family of functions, for example, see this: http://stackoverflow.com/q/5687928/3701431 Personally , I see it a bit more convoluted compared to Python approach, but definitely doable – Sergiy Kolodyazhnyy Jan 19 '17 at 15:16
  • Your answer was the final drop leading me to success. I will right my row. I thank you. – Kosarar Jan 20 '17 at 01:24
  • @user3107513 You're welcome :) Happy coding and enjoy Ubuntu ! – Sergiy Kolodyazhnyy Jan 20 '17 at 01:25
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
  • 70,465
Kosarar
  • 171