Oct1a

基于node.js的sudo-prompt库控制系统开机项

因utools自带node.js,所以

const sudo = require('sudo-prompt');
const path = require("path")

window.exports = {
    "addStart": {
        mode: "none",
        args: {
            enter: async(action) => {
                let name = action.payload[0].name
                let file_name = name.substr(0,name.lastIndexOf('.'))
                
                let ext = name.substr(name.lastIndexOf('.')+1);
                
                let appDataPath = utools.getPath('appData')

                let startUpPath = path.join(appDataPath, '/Microsoft/Windows/Start Menu/Programs/Startup',ext =='lnk'?name: file_name)
                
                if(ext =='lnk'){
                    // 已经是快捷方式图标,则直接copy
                    sudo.exec(`copy "${action.payload[0].path}" "${startUpPath}"`,{name:'utools'},
                    function(error, stdout, stderr) {
                        if (error) throw error;
                        utools.showNotification(`添加${file_name}为开机启动项成功`)
                        console.log('stdout: ' + stdout);
                    })
                }else{
                    sudo.exec(`mklink "${startUpPath}" "${action.payload[0].path}"`,{
                        name: 'utools',
                    },
                    function(error, stdout, stderr) {
                        if (error) throw error;
                        utools.showNotification(`添加${file_name}为开机启动项成功`)
                        console.log('stdout: ' + stdout);
                    }
                    );
                }
                utools.hideMainWindow()
                utools.outPlugin()
            }
        }
    }
}

【参考资料】

nodejs使用child_process.exec时,使用管理员权限执行命令_胡桃夹夹子的博客-CSDN博客

https://github.com/jorangreef/sudo-prompt

本作品采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 进行许可。