Skip to content

Instantly share code, notes, and snippets.

@decors
Last active September 18, 2021 15:45
Show Gist options
  • Save decors/7b1cb44a2b9e18df58e8 to your computer and use it in GitHub Desktop.
Save decors/7b1cb44a2b9e18df58e8 to your computer and use it in GitHub Desktop.
Electron open-file
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Electron</title>
</head>
<body>
<script>
var ipc = require('ipc');
ipc.on('open-file', function(filepath) {
alert(filepath);
});
</script>
</body>
</html>
'use strict';
var app = require('app'),
BrowserWindow = require('browser-window');
require('crash-reporter').start();
var mainWindow;
var filepath;
var ready = false;
app.on('ready', function() {
mainWindow = new BrowserWindow({ 'width': 800, 'height': 600 });
mainWindow.loadUrl('file://' + __dirname + '/index.html');
mainWindow.focus();
mainWindow.on('closed', function() {
mainWindow = null;
});
mainWindow.webContents.on('did-finish-load', function() {
if (filepath) {
mainWindow.webContents.send('open-file', filepath);
filepath = null;
}
});
ready = true;
});
app.on("open-file", function(event, path) {
event.preventDefault();
filepath = path;
if (ready) {
mainWindow.webContents.send('open-file', filepath);
filepath = null;
return;
}
});
app.on('window-all-closed', function() {
app.quit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment