0

Specifically, I have a bash script called nbopen that takes as argument a file of type .ipynb. I use it like this: nbopen mynotebook.ipynb to launch Jupyter Notebooks (a type of Python script) from the Terminal.

What I want, is for a double click on a file such as mynotebook.ipynb in the Finder to cause nbopen mynotebook.ipynb to execute in a Terminal window. This way I no longer have to launch this type of file from a Terminal environment (cumbersome and repetitive), I can just double click the file and it will launch directly.

I figure this can be done somehow with Automator, or probably by writing a small application, but I'm not knowledgeable enough about OSX programming to know which direction is wisest to pursue. Any help is appreciated.

2
  • This should be achievable. One way would be to create an AppleScript application bundle. This bundle would house your nbopen shell program, together with an AppleScript that tells the app what to do with any file that is sent to it for opening (i.e. the AppleScript would forward it on to the shell program and execute it for you). You then set the AppleScript application to become the default opener for .ipynb files, which should be very straightforward in Finder.
    – CJK
    Dec 7, 2019 at 16:01
  • Thanks! Do I simply write an Applescript program that does this and save it as a bundle? You're welcome do answer this question with a suggested solution, then I have something to go from. I'll update your answer and accept it when it works.
    – Ulf Aslak
    Dec 8, 2019 at 16:08

1 Answer 1

0

I have exactly the same use case. Inspired by @CJK, I wrote a script application. It's not perfect, quite buggy, but works:

property notebookDir : null
property notebookPort : 8888
property jupyterExecutable : "/path/to/a/working/jupyter/executable"

on run
    set notebookDir to choose folder with prompt "Choose the --notebook-dir of the jupyter server. Note that .ipynb_checkpoints directory may be created underneath."
    set notebookDir to POSIX path of notebookDir
    do shell script "/usr/local/bin/tmux new -ds jupyter-opener " & quoted form of jupyterExecutable & " notebook --notebook-dir " & quoted form of notebookDir & " --port " & notebookPort as string
end run

on open args
    repeat with anArg in args
        set theFilePath to POSIX path of anArg
        set theValidatedPath to checkIsOpenableIpynbAndValidate(theFilePath)
        if theValidatedPath is null then
            # seems that this line won't work either
            display alert message "Failed to open " & theFilePath
        else
            open location ("http://127.0.0.1:" & notebookPort as string) & "/notebooks/" & theValidatedPath
        end if
    end repeat
end open

on quit
    do shell script "/usr/local/bin/tmux kill-session -t jupyter-opener"
    continue quit
end quit

on checkIsOpenableIpynbAndValidate(aFilePath)
    try
        set relPath to do shell script "/usr/local/bin/realpath --relative-to " & quoted form of notebookDir & space & quoted form of aFilePath & " | perl -ne 'if(m(^\\.\\./) || !m(\\.ipynb$)){exit 1} else {print}'"
        # struggling to get this line work ...
        #set encodedRelPath to do shell script "echo -n " & quoted form of relPath & " | /usr/bin/python3 -c 'import urllib.parse; print(urllib.parse.quote(input()), end=\"\")'"
        #return encodedRelPath
        return relPath
    on error
        return null
    end try
end checkIsOpenableIpynbAndValidate

So for jupyterExecutable, I set it to whatever which jupyter returns. You should first check if all the involved command line utilities (like /usr/local/bin/tmux) have been installed. Paste the above code into Script Editor. Save as 'Application' File Format. And tick the 'Stay open after run handler' option.

Usage:

  1. Double click the saved application icon.
  2. In the pop-up folder selector, select the directory to be used as --notebook-dir value
  3. From any sub directory, drag-n-drop any ipynb file onto the application icon in the Dock. A tab should be opened in the default browser showing the notebook.
  4. Afterwards, right click the application icon in the Dock, and select Quit.

Mechanism:

It launches a tmux session running the jupyter on start, and kill that session on quit.

You should be aware that jupyter leaves .ipynb_checkpoints folder wherever it goes (under --notebook-dir). This script application is not responsible for cleaning it up.

Caveats:

  • no url escape is done. I tried, but it wouldn't work (see the comment in codes)
  • if you drag-n-drop, e.g. mp4 file, or files under parent folder of the selected notebook directory, it won't handle such case nicely. I tried, but it wouldn't work (also see the comment)

Hope someone could tell me how to get over these two problems.

What if you must open the ipynb file by double-clicking rather than drag-n-drop?

Probably you should follow @CJK's suggestion by setting it as default application. I didn't try it here because I'm now knowledgeable about applescript either, and I haven't figured out how to make it work by double-clicking only.

Hope it helps.

You must log in to answer this question.

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