使用 Electron 通过 Squirrel 事件创建桌面快捷方式
Creating a desktop shortcut via Squirrel events with Electron
我有一个 Electron 应用程序,我正在开发一个 Mac 来为它制作一个 Windows 安装程序。
现在我有一个 /installers 目录和一个处理所有 Squirrel 事件的 setupEvents.js 文件。大部分来自Windows installer documentation:
import { app } from 'electron';
module.exports = {
handleSquirrelEvent: function() {
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;
}
}
}
到目前为止,一切正常,只是添加到桌面的快捷方式图标的标题为 "Electron",我不确定如何更改它。我的 package.json 中有名称和产品名称:
{
"name": "my app",
"description": "my app description",
"productName": "my app",
"appCopyright": "me",
"appCategoryType": "Productivity",
...
我的安装程序配置如下所示:
{
appDirectory: path.join(outPath, 'myapp-win32-ia32/'),
authors: 'me',
noMsi: true,
outputDirectory: path.join(outPath, 'windows-installer'),
exe: 'myapp.exe',
setupExe: 'myappInstaller.exe',
setupIcon: path.join(rootPath, 'assets', 'win', 'icon.ico'),
skipUpdateIcon: true
}
我不知道在哪里告诉安装程序快捷方式图标应该有我的应用程序的名称,而不仅仅是 "Electron"。
提前致谢!
终于想通了
在您的 project.json
文件中,用于构建应用程序的命令就是代码所在的位置。
您要查找的部分是 --version-string.ProductName=\"My App Name\"
,可在 "scripts": { "build": "YOUR CODE HERE"}
:
中找到
旁注...我正在使用 electron-packager。
这是一个使用我的代码的示例:
"pack:win64": "electron-packager ./ --overwrite --asar=true --platform=win32 --arch=x64 --ignore=assets --ignore=build --ignore=installers --icon=./images/icons/icon.ico --prune=true --out=build/win --version-string.ProductName=\"My App Name\""
我有一个 Electron 应用程序,我正在开发一个 Mac 来为它制作一个 Windows 安装程序。
现在我有一个 /installers 目录和一个处理所有 Squirrel 事件的 setupEvents.js 文件。大部分来自Windows installer documentation:
import { app } from 'electron';
module.exports = {
handleSquirrelEvent: function() {
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;
}
}
}
到目前为止,一切正常,只是添加到桌面的快捷方式图标的标题为 "Electron",我不确定如何更改它。我的 package.json 中有名称和产品名称:
{
"name": "my app",
"description": "my app description",
"productName": "my app",
"appCopyright": "me",
"appCategoryType": "Productivity",
...
我的安装程序配置如下所示:
{
appDirectory: path.join(outPath, 'myapp-win32-ia32/'),
authors: 'me',
noMsi: true,
outputDirectory: path.join(outPath, 'windows-installer'),
exe: 'myapp.exe',
setupExe: 'myappInstaller.exe',
setupIcon: path.join(rootPath, 'assets', 'win', 'icon.ico'),
skipUpdateIcon: true
}
我不知道在哪里告诉安装程序快捷方式图标应该有我的应用程序的名称,而不仅仅是 "Electron"。
提前致谢!
终于想通了
在您的 project.json
文件中,用于构建应用程序的命令就是代码所在的位置。
您要查找的部分是 --version-string.ProductName=\"My App Name\"
,可在 "scripts": { "build": "YOUR CODE HERE"}
:
旁注...我正在使用 electron-packager。
这是一个使用我的代码的示例:
"pack:win64": "electron-packager ./ --overwrite --asar=true --platform=win32 --arch=x64 --ignore=assets --ignore=build --ignore=installers --icon=./images/icons/icon.ico --prune=true --out=build/win --version-string.ProductName=\"My App Name\""