gpt4 book ai didi

javascript - 如何在任务中使用命令行参数退出应用程序

转载 作者:行者123 更新时间:2023-12-02 22:54:34 25 4
gpt4 key购买 nike

我正在使用 Electron 5.0.0,我正在尝试使用 Windows JumpList 和任务类别来退出我的 Electron 应用程序。

    {
program: process.execPath,
arguments: '--new-window',
iconPath: process.execPath,
iconIndex: 0,
title: 'New Window',
description: 'Create a new window'
}
])

我正在尝试修改 Electron 网站的示例代码,我需要更改参数

“arguments String - 程序执行时的命令行参数。”

我知道 Windows 内置了像 --new-window 这样的参数

所以我的问题是 Windows 是否有一些可以退出应用程序的东西,或者我是否需要进行自定义参数,如果是的话我该怎么做

我希望它具有与 Skype 相同的功能(参见图片) enter image description here

编辑:

我尝试使用第二个实例事件,但当用户单击任务时它似乎没有被调用

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启动应用程序的新实例。你应该处理这个争论。

仅当您允许 single instance 时,您的用例才有意义。您的应用程序正在运行。您需要获取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', () => {
})
}

关于javascript - 如何在任务中使用命令行参数退出应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58045513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com