Handle Squirrel's event on an Electron app

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

These days I'm toyng around with Electron to build a small native app for Windows and I'm using Grunt Electron Installer to create an installer for my application.

The installer is created successfully but I don't know how ho handle Squirrel's events inside my app, as stated in the docs I've added this to the entry point of my app:

var handleStartupEvent = function() {     if (process.platform !== 'win32') {         return false;     }      var squirrelCommand = process.argv[1];     switch (squirrelCommand) {         case '--squirrel-install':         case '--squirrel-updated':              // Optionally do things such as:             //             // - Install desktop and start menu shortcuts             // - Add your .exe to the PATH             // - Write to the registry for things like file associations and             //   explorer context menus              // Always quit when done             app.quit();              return true;         case '--squirrel-uninstall':             // Undo anything you did in the --squirrel-install and             // --squirrel-updated handlers              // Always quit when done             app.quit();              return true;         case '--squirrel-obsolete':             // This is called on the outgoing version of your app before             // we update to the new version - it's the opposite of             // --squirrel-updated             app.quit();             return true;     } };  if (handleStartupEvent()) {     return; }

But I don't know what to do inside this switch statement to, for example, create shortcuts for my application. Actually I don't even know if this switch works at all because when I install (or uninstall) my application it get launched and never quits.

Any help is appreciated!

回答1:

You could handle each Squirrel event and create shortcuts:

  case '--squirrel-install':           target = path.basename(process.execPath);           updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');           var createShortcut = updateDotExe + ' --createShortcut=' + target + ' --shortcut-locations=Desktop,StartMenu' ;           console.log (createShortcut);           exec(createShortcut);           // Always quit when done           app.quit();           return true;  case '--squirrel-uninstall':             // Undo anything you did in the --squirrel-install and             // --squirrel-updated handlers             target = path.basename(process.execPath);             updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');             var createShortcut = updateDotExe + ' --removeShortcut=' + target ;             console.log (createShortcut);             exec(createShortcut);             // Always quit when done             app.quit();             return true;



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!