ome
ome

Reputation: 63

NodeJS open local file using the default preview

I am trying to create a desktop app using electron which includes a function where a user can open a desired file saved in the local storage. Sine I am using MacOS, I want to use nodejs to be able to open the file (e.g. PDF doc) in the default preview software. is there any way to do this?

Thank you in advance.

Upvotes: 0

Views: 799

Answers (2)

Showing The Path
Showing The Path

Reputation: 11

A portable way is to make use of the Electron's shell API, specifically the shell.openPath method:

const { shell } = require('electron');
shell.openPath("/fullpath/to/file");

It is available both in the main process and in the renderer process, and it can also be used to open a folder in the Finder.

Upvotes: 1

ome
ome

Reputation: 63

if anyone is wondering I found a way of using

const { exec } = require('child_process')    
exec('open ~/path/to/file')

this used command shell (terminal) to open it. If there is any other better way to do it please let us know!

Upvotes: 0

Related Questions