Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use in electron? #9

Closed
crylg opened this issue Jun 2, 2017 · 6 comments
Closed

how to use in electron? #9

crylg opened this issue Jun 2, 2017 · 6 comments

Comments

@crylg
Copy link

crylg commented Jun 2, 2017

var electron = require('electron');
//自动更新
var ws = require('windows-shortcuts');

const autoUpdater = electron.autoUpdater;

// Module to control application life.
var app = electron.app;
// Module to create native browser window.
var BrowserWindow = electron.BrowserWindow;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow;
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({ width: 1024, height: 680 });
// and load the index.html of the app.
mainWindow.loadURL("file://" + __dirname + "/index.html");
// Open the DevTools.
// mainWindow.webContents.openDevTools();//开发者
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready',
function(){
if(!process.argv[1]) createWindow();
function startupEventHandle(){
if(require('electron-squirrel-startup')) return;
var handleStartupEvent = function () {
if (process.platform !== 'win32') {
return false;
}
var squirrelCommand = process.argv[1];
switch (squirrelCommand) {
case '--squirrel-install':
case '--squirrel-updated':
install();
return true;
case '--squirrel-uninstall':
uninstall();
app.quit();
return true;
case '--squirrel-obsolete':
app.quit();
return true;
}
// 安装
function install() {
var cp = require('child_process');
var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
var target = path.basename(process.execPath);
// var child = cp.spawn(updateDotExe, ["--createShortcut", target], { detached: true });
ws.create("%APPDATA%/Microsoft/Windows/Start Menu/Programs/MedSpace.lnk", target);
ws.create("%APPDATA%/../../Desktop/MedSpace.lnk",target,function(e){app.quit();});
child.on('close', function(code) {
app.quit();
});
}
// 卸载
function uninstall() {
var cp = require('child_process');
var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
var target = path.basename(process.execPath);
var child = cp.spawn(updateDotExe, ["--removeShortcut", target], { detached: true });
child.on('close', function(code) {
app.quit();
});
}
};
if (handleStartupEvent()) {
return ;
}
}
function updateHandle(){
ipc.on('check-for-update', function(event, arg) {
let appName='MedSpace系统';
let appIcon=__dirname + '/logo/logo.ico';
let message={
error:'检查更新出错',
checking:'正在检查更新……',
updateAva:'下载更新成功',
updateNotAva:'现在使用的就是最新版本,不用更新',
downloaded:'最新版本已下载,将在重启程序后更新'
};
const os = require('os');
const {dialog} = require('electron');
autoUpdater.setFeedURL('http://update.leiguan.me/medspace');
autoUpdater.on('error', function(error){
return dialog.showMessageBox(mainWindow, {
type: 'info',
icon: appIcon,
buttons: ['OK'],
title: appName,
message: message.error,
detail: '\r'+error
});
})
.on('checking-for-update', function(e) {
return dialog.showMessageBox(mainWindow, {
type: 'info',
icon: appIcon,
buttons: ['OK'],
title: appName,
message: message.checking
});
})
.on('update-available', function(e) {
var downloadConfirmation = dialog.showMessageBox(mainWindow, {
type: 'info',
icon: appIcon,
buttons: ['OK'],
title: appName,
message: message.updateAva
});
if (downloadConfirmation === 0) {
return;
}
})
.on('update-not-available', function(e) {
return dialog.showMessageBox(mainWindow, {
type: 'info',
icon: appIcon,
buttons: ['OK'],
title: appName,
message: message.updateNotAva
});
})
.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
var index = dialog.showMessageBox(mainWindow, {
type: 'info',
icon: appIcon,
buttons: ['现在重启','稍后重启'],
title: appName,
message: message.downloaded,
detail: releaseName + "\n\n" + releaseNotes
});
if (index === 1) return;
force_quit = true;
autoUpdater.quitAndInstall();
});
autoUpdater.checkForUpdates();
});
}
//自动更新

}

);
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
//# sourceMappingURL=electron-main.js.map


Error:Cannot dind module 'windows-shortcuts'

@asoap
Copy link

asoap commented Jun 19, 2017

I have it working on electron. The issue is that it won't work when I try install the application. Once the application is run it will execute the shortcuts just fine. But something is stopping it while squirrel is installing the application.

I even changed execFile vs. exec to see if spawning a shell would fix it. But that didn't do it. :(

@j201
Copy link
Owner

j201 commented Jun 28, 2017

Is the .exe file showing up in the correct location? I don't have time to really dig into this right now, but my suspicion is that these tools have some issue with node modules that contain Windows executables.

@Kreshnik
Copy link

Has anyone managed to get it working on electron? For me the creation of shortcuts is not working on Windows 10.

@moeinrahimi
Copy link

anyone ?

@mats-codes
Copy link

Luckily, electron added a native feature for that: https://www.electronjs.org/docs/api/shell#shellreadshortcutlinkshortcutpath-windows

@j201
Copy link
Owner

j201 commented Mar 19, 2020

Thanks, I'll add a note on that to the readme.

@j201 j201 closed this as completed Mar 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants