Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron open file/directory in specific application

Tags:

I'm building a sort of File explorer / Finder using Electron. I want to open some file types with a specific application.

I've tried the approach from this answer: Open external file with Electron

import { spawn } from 'child_process' spawn('/path/to/app/superApp.app', ['/path/to/file']) 

But when I do that, I get a EACCES error as follows.

Is this the right approach? If yes, how can I fix the issue? If not, what is the right approach?

like image 493
denislexic Avatar asked May 16 '17 01:05

denislexic


2 Answers

You can open a file or folder through shell commands from the electron module. The commands work on both main and renderer process.

const {shell} = require('electron') // deconstructing assignment  shell.showItemInFolder('filepath') // Show the given file in a file manager. If possible, select the file. shell.openPath('folderpath') // Open the given file in the desktop's default manner. 

More info on https://github.com/electron/electron/blob/master/docs/api/shell.md

like image 146
sawa Avatar answered Sep 21 '22 23:09

sawa


For display native system dialogs for opening and saving files, alerting, etc. you can use dialog module from electron package.

const electron = require('electron'); var filePath = __dirname; console.log(electron.dialog.showOpenDialog)({   properties:['openFile'],   filters:[     {name:'Log', extentions:['csv', 'log']}   ] }); 

A very prompt explanation is provided at Electron Docs.

like image 35
Jabed Avatar answered Sep 22 '22 23:09

Jabed