Open in external app using "Open file" and links


#1

I would like to be able to use “Open file” from the menu to open a file with an external application, as required (e.g., open a .pdf file with the default PDF reader). Similarly, if I click on a link to a path (e.g., …/pru.pdf) I would like that file to open with the PDF reader rather than as another (text) file in Atom. I have used the package https://atom.io/packages/open-unsupported-files, which works great, but it seems to work only if the file is selected from the tree view. However, it would be really very useful if the feature of external opening is also activated when clicking on a link and when opening a file from the menu (these two options lead to the file being opened as a text file directly in Atom).

Any way to do that? Could the package be adapted easily to support this? Or is there any other alternative? Thank you very much.


#2

You can easily do this in your init.coffee file using Node’s child_process module:

{spawn} = require 'child_process'

atom.contextMenu.add {
  ".tree-view": [{
    label: "Open file in default program"
    command: "custom:open-in-default"
  }]
}

atom.commands.add ".tree-view", "custom:open-in-default", (evt) ->
  spawn evt.target.attributes.getNamedItem("data-path").value, {shell: true}

Open git bash from Atom
#3

Thank you very much for the nice code !

However, I think that I did not explain my problem correctly. Opening with the default external program using the tree view already works in my system (thanks to the package https://atom.io/packages/open-unsupported-files). I understand that the code proposed is an alternative to using that package (I tried it and it seems to work perfectly also).

However, what I want is that the file is opened in the default application even if I don’t open the file through the tree view. Particularly, when clicking on a link within a text file opened in Atom, such as:

…/pru.pdf

Probably the hyperclick package is at work here.

Any suggestion is welcome, as this would be really very useful.

Thank you.


#4

hyperclick offers an API to provider packages to create clickable text, so I suspect that the important factor is which package is actually creating the link. What providers do you have installed?


#5

I think it is path-hyperclick (https://atom.io/packages/path-hyperclick).

Thank you.


#6

Here’s the DOM view of a link that’s been highlighted and is clickable with hyperclick and path-hyperclick:

There’s no scope here that identifies it as a link, so the only option for tweaking the behavior of the clickable area is to make a change to the package itself.