2

There is a file type called "netCDF". My goal is to be able to double click on a netCDF file on Finder and see the text output from a CLI command to which the filename is given.

Specifics: There is a command-line command that displays the table of contents of a netCDF file:

$ ncdump -h yourfile.nc

This command shows the table of contents of the netCDF file to the current terminal.

How can I see the same output when I double click on the file on Finder?

Edit: The following is a description of a failed approach.

I have made some progress toward a solution.

Following this thread

How to make a Mac OS X .app with a shell script?

I've been able to create a macOS "app" which is actually a shell script:

#!/bin/sh
/usr/local/bin/ncdump -h "$@"

So far, I've been able to verify that I can invoke this "app" from Finder and that the above shell script is actually run. But, I found that the shell script doesn't get the filename as a command line argument. (You can see what's going on by modifying the line to /usr/local/bin/ncdump -h "$@" > $HOME/tmp/logfile.txt 2>&1)

So, I have two specific questions

  1. How can a shell script (as app) get the filename when invoked from Finder?

  2. To which GUI app should it send its stdout and how?

8
  • What happens if you run it like: /PATH/TO/MyApp.app/Contents/MacOS/MyApp (without open -a) ? Commented Sep 2, 2022 at 7:16
  • "What happens if you run it like . . . (without open -a)" . . . It prints out the output to the current terminal. Does that answer your question?
    – Ryo
    Commented Sep 3, 2022 at 10:07
  • yes. So is there any reason to run the .app with open -a instead of running it directly? Commented Sep 3, 2022 at 10:45
  • " So is there any reason to run the .app with open -a instead of running it directly?" My goals to invoke the shell script by double-clicking on the file on Finder and see the output from the command.
    – Ryo
    Commented Sep 4, 2022 at 6:29
  • The problem with this question is that it doesn't address where the OP would like for his output to go. Should it go a file, to a tty, the console... where? I wonder if the OP realizes his objective could be realized by simply invoking the "app" from the command line (Terminal.app)?
    – Seamus
    Commented Sep 4, 2022 at 21:22

2 Answers 2

0

If your App is actually just a shell script, you don't need the open command to run it. Just execute the shell script using ./PATH_TO_SCRIPT -- in your case, this would be ./MyApp.app/MyApp, if you followed the naming suggested in the linked thread. From there, you can redirect or pipe the output like with any other shell command.

However, if you have to use the open command: There seems to be an argument --stdout, which could solve your problem. From man open:

--stdout PATH
    Launches the application with stdout connected to PATH.

Edit

The edit of your question changes a lot: You want to start the Application/Script via Finder, but then see the "Terminal Output".

You could, for example, use AppleScript to show a notification or dialog containing the output of your app (depending on how much text it is). This can be done using the osascript -e '[APPLESCRIPT_COMMANDS]' command. For example, osascript -e 'display dialog "Sample Text"' will show a small popup window containing "Sample Text" and Ok/Cancel buttons. More info on dialogs can be found for example here.

For this to work, you must redirect the output of the commands in your script to a variable, this can be done for example with myVariable=$(...original command...)

5
  • 1
    Did you try whether -stdout actually solves the problem?
    – nohillside
    Commented Sep 2, 2022 at 14:50
  • 1
    @JLubberger Thanks for the suggestion. I've just tried open --stdout $HOME/tmp/tmp.log -a MyApp and found that the output file is created but it's empty. If I directly invoke the shellscript from the command line, it prints its output onto the current terminal. So, open command must be sending the standard output to somewhere.
    – Ryo
    Commented Sep 3, 2022 at 10:13
  • @JLubberger Thank you again for your help and I apologize for the confusion. I've now totally rewritten my question. After the last edit to my question, I realized that you have also edited yours. So, basically, I should use AppleScript, not a shell script, right?
    – Ryo
    Commented Sep 5, 2022 at 8:56
  • 1
    You can, but don't have to, since the osascript command allows the integration of AppleScript inside regular shell scripts. So if you already have the shell script, it might be easier to use AppleScript just for displaying the output.
    – JLubberger
    Commented Sep 5, 2022 at 9:09
  • 1
    Okay, I hadn't read the complete re-write of your question. If you "only" want a quick way to see the contents of a netCDF file, you could look into QuickLook plugins. For example this: github.com/tobeycarman/QLNetcdf -- looks a bit old, but might actually work and would probably solve your issue best.
    – JLubberger
    Commented Sep 5, 2022 at 9:13
0

Abandoning the idea of turning a shell script into an app, I adopted @JLubberger's idea of using an AppleScript. The best I have achieved so far is

on open filename
    tell application "Terminal"
        do script "ncdump -h " & quoted form of POSIX path of filename & " 2>&1"
        activate
    end tell
end open

and export this as an app.

Then, open a netCDF file with this app, which will launch Terminal, which will show the output from the command "ncdump -h filename".

One unsatisfactory aspect to this solution is that Terminal shows extra ouptputs of its own. I'd rather have a window which will just show the ouptput from the command. This would be a different approach because I would have to find a way to run the command from within the script and send its output to a GUI window. In contrast, the above script of mine asks Terminal both to run the command and to display its output.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .