javascript - 如何在任务中使用命令行参数退出应用程序
问题描述
我正在使用电子 5.0.0,我正在尝试使用 windows JumpList 和任务类别来退出我的电子应用程序。
{
program: process.execPath,
arguments: '--new-window',
iconPath: process.execPath,
iconIndex: 0,
title: 'New Window',
description: 'Create a new window'
}
])
我正在尝试修改电子网站上的示例代码,我需要更改参数
“arguments String - 程序执行时的命令行参数。”
我知道 windows 已经内置了 --new-window 之类的参数
所以我的问题是windows是否有一些会退出应用程序的东西,或者我需要做一个自定义参数,如果是这样,我将如何去做
编辑:
我尝试使用第二个实例事件,但是当用户单击任务时似乎没有调用它
app.setUserTasks([
{
program: process.execPath,
arguments: '--force-quit',
iconPath: process.execPath,
iconIndex: 0,
title: 'Force Quit App',
description: 'This will close the app instead of minimizing it.'
}
])
app.on('second-instance', (e, argv)=>{
console.log("secinst" + argv)
if(argv === '--force-quit'){
win.destroy();
}
})
解决方案
如果你设置这样的任务:
app.setUserTasks([
{
program: process.execPath,
arguments: '--force-quit',
iconPath: process.execPath,
iconIndex: 0,
title: 'Force Quit App',
description: 'This will close the app instead of minimizing it.'
}
])
单击时,这将使用命令行参数启动应用程序的新实例--force-quit
。你应该处理那个论点。
仅当您允许运行应用程序的单个实例时,您的用例才有意义。你需要argv
从second-instance
事件中获得。
const { app } = require('electron')
let myWindow = null
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', (event, argv, workingDirectory) => {
// Someone tried to run a second instance
const forceQuit = argv.indexOf("--force-quit") > -1;
if (forceQuit) app.quit()
})
// Create myWindow, load the rest of the app, etc...
app.on('ready', () => {
})
}
推荐阅读
- php - .htaccess:从公共目录中提供静态文件,而不在 URL 中公开,否则重写为 index.php
- spring-boot - 将应用程序升级到 springboot 2.0.4.RELEASE 后应用程序无法运行
- puppeteer - Puppeteer:如何将相同的背景应用于 pdf 页面的页脚?
- api - 如何将 Gremlin 与 GraphDB Ontotext 连接起来
- sql - 基于 SQL Server 中日期的不同计数
- mysql - 如何在mysql中将表行显示为cols
- reactjs - React propType 指定允许的 props 值
- r - 我可以将我的 ggplot2 图例放在横轴标签旁边吗?
- r - 错误:R中的意外字符串常量“} else {”
- ios - 如何在 Xcode 中列出一个目标的所有文件