【问题标题】:Electron app installed but doesn't appear in start menuElectron 应用程序已安装但未出现在开始菜单中
【发布时间】:2016-06-23 19:39:07
【问题描述】:

我使用 Electron 创建了一个应用程序,并将其与 electron-builder 捆绑在一个 .exe 文件中。

当我运行生成的可执行文件时,应用程序以 electron-builder 使用的默认安装 GIF 启动,正如预期的那样。

GIF 完成后,应用会重新启动并正常运行。它甚至出现在控制面板的程序列表中

但是,如果我在开始菜单应用程序中查找它,它不存在(按名称搜索只会返回上述.exe 安装程序)。
正因为如此,一旦应用程序关闭,重新打开它的唯一方法就是再次运行安装程序。

为什么会这样?有没有办法让它和其他程序一起出现?

【问题讨论】:

    标签: node.js build electron


    【解决方案1】:

    electron-builder 安装程序(和 electron-windows-installer)使用Squirrel 来处理安装。 Squirrel 在安装时使用您需要处理的参数启动您的应用程序。可以在windows installer github docs上找到一个示例

    处理松鼠事件

    Squirrel 将在首次运行、更新和卸载时使用命令行标志生成您的应用。您的应用应尽早处理这些事件,并在处理完这些事件后立即退出,这一点非常重要。 Squirrel 将为您的应用提供很短的时间(约 15 秒)来应用这些操作并退出。

    electron-squirrel-startup 模块将为您处理最常见的事件,例如管理桌面快捷方式。只需将以下内容添加到 main.js 的顶部即可:

    if (require('electron-squirrel-startup')) return;
    

    您应该在应用的主入口点处理这些事件,例如:

    const app = require('app');
    
    // this should be placed at top of main.js to handle setup events quickly
    if (handleSquirrelEvent()) {
      // squirrel event handled and app will exit in 1000ms, so don't do anything else
      return;
    }
    
    function handleSquirrelEvent() {
      if (process.argv.length === 1) {
        return false;
      }
    
      const ChildProcess = require('child_process');
      const path = require('path');
    
      const appFolder = path.resolve(process.execPath, '..');
      const rootAtomFolder = path.resolve(appFolder, '..');
      const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
      const exeName = path.basename(process.execPath);
    
      const spawn = function(command, args) {
        let spawnedProcess, error;
    
        try {
          spawnedProcess = ChildProcess.spawn(command, args, {detached: true});
        } catch (error) {}
    
        return spawnedProcess;
      };
    
      const spawnUpdate = function(args) {
        return spawn(updateDotExe, args);
      };
    
      const squirrelEvent = process.argv[1];
      switch (squirrelEvent) {
        case '--squirrel-install':
        case '--squirrel-updated':
          // Optionally do things such as:
          // - Add your .exe to the PATH
          // - Write to the registry for things like file associations and
          //   explorer context menus
    
          // Install desktop and start menu shortcuts
          spawnUpdate(['--createShortcut', exeName]);
    
          setTimeout(app.quit, 1000);
          return true;
    
        case '--squirrel-uninstall':
          // Undo anything you did in the --squirrel-install and
          // --squirrel-updated handlers
    
          // Remove desktop and start menu shortcuts
          spawnUpdate(['--removeShortcut', exeName]);
    
          setTimeout(app.quit, 1000);
          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;
      }
    };
    

    请注意,安装程序第一次启动您的应用时,您的应用会看到一个 --squirrel-firstrun 标志。这允许您执行诸如显示初始屏幕或显示设置 UI 之类的操作。另一件需要注意的是,由于应用程序是由 squirrel 生成的,并且 squirrel 在安装过程中会获取文件锁,所以直到几秒钟后 squirrel 释放锁时,您才能成功检查应用程序更新。

    在此示例中,您可以看到它运行 Update.exe(松鼠可执行文件),带有添加开始菜单和桌面快捷方式的参数 --create-shortcut。

    【讨论】:

    • 返回对我来说会引发错误。关于返回在函数之外的一些事情
    【解决方案2】:

    现在是 2021 年,我仍然遇到非常相似的问题。 我的应用程序安装正确,并且使用上面的脚本也成功地将桌面链接添加到我的应用程序。 但是: Windows 开始菜单中没有添加快捷方式。 使用上面的脚本,这也应该添加到开始菜单中,对吧? 上面的一条评论说:

    // Install desktop and start menu shortcuts
    spawnUpdate(['--createShortcut', exeName]);
    

    我错过了什么?任何提示都非常感谢...

    【讨论】:

      【解决方案3】:

      对我来说,它有助于将图标和 setupIcon 添加到 package.json 文件中,在该文件中配置了制造商。在我的应用程序没有出现在“开始”菜单中之前,并且使用下面的制造商配置,它确实出现了。我不知道为什么。

        "makers": [
          {
            "name": "@electron-forge/maker-squirrel",
            "config": {
              "name": "cellmonitor",
              "icon": "favicon.ico",
              "setupIcon": "favicon.ico"
            }
          }
        ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-03
        • 2017-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多