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.


#7

I still was trying to solve this problem, with no success. Clicking on something like:

file://D:/pru.pdf

works (the file pru.pdf is opened with the external PDF reader when clicking on that link).

However, the problem with using the file:// protocol is that I can only specify an absolute path but not a relative path.

If I use:

./pru.pdf

Then the binary code of the .pdf file is opened directly in the Atom editor instead of using the external application, which is bad.

I have defined the .pdf extension in the settings of the open-unsupported-files package (https://atom.io/packages/open-unsupported-files).

If you any suggestions to try to solve this problem (i.e., for this to work also for relative paths), it would be great. I would prefer not having to modify packages…

Thank you.


#8

Well, if you want to manipulate data in that way (transform a relative path into an absolute path), you’re going to have to write code to make Atom do that. You can do it from init.coffee if you want. It would work very similarly to the first code I posted, except that instead of getting the data-path attribute from a tree-view item, you would probably want to use selected text and run that through an algorithm that resolves the path before running the spawn() method. (You technically don’t even need to resolve the path, but you should because otherwise you run the risk of your working directory not being what you expect it to be, and that can cause hard-to-pinpoint errors.)