2020-10-29 Node.js 创建桌面快捷方式调用vbs
2020-10-29 本文已影响0人
deadcalm
此片文章没啥卵用,主要是想实现重装系统、或者到新系统需要软件 安装起来很麻烦。
就想以前安装好的软件直接点脚本就能创建快捷方式使用。
vbs脚本借鉴 https://github.com/nwutils/create-desktop-shortcuts
const { spawn } = require('child_process')
const fs = require('fs')
const path = require('path')
const os = require('os')
/** vbs脚本语句 */
const commandVbs = `' Code based on https://forums.techguy.org/threads/solved-vbscript-create-a-shortcut-within-a-folder.886401/
' and https://www.vbsedit.com/html/a239a3ac-e51c-4e70-859e-d2d8c2eb3135.asp
option explicit
dim strOutputPath, strFilePath, strArgs, strComment, strCwd, strIcon, strWindowMode, strHotkey
strOutputPath = Wscript.Arguments(0)
strFilePath = Wscript.Arguments(1)
strArgs = Wscript.Arguments(2)
strComment = Wscript.Arguments(3)
strCwd = Wscript.Arguments(4)
strIcon = Wscript.Arguments(5)
strWindowMode = Wscript.Arguments(6)
strHotkey = Wscript.Arguments(7)
sub createFile()
dim objShell, objLink
set objShell = CreateObject("WScript.Shell")
' objShell.Run "cmd /c yourcommands", 0, True
set objLink = objShell.CreateShortcut(strOutputPath)
objLink.TargetPath = strFilePath
objLink.Arguments = strArgs
objLink.Description = strComment
objLink.WorkingDirectory = strCwd
objLink.IconLocation = strIcon
objLink.WindowStyle = strWindowMode
objLink.Hotkey = strHotkey
objLink.Save
end sub
call createFile()`
/** 库文件夹 */
const libdir = path.join(__dirname, 'lib')
/** 脚本路径 */
const wscript = path.join(libdir, 'windows.vbs')
/** 桌面路径 */
const desktop = path.resolve(os.homedir(), 'Desktop')
// 检测脚本是否存在
if (!fs.existsSync(libdir)) {
fs.mkdirSync(libdir)
fs.writeFileSync(wscript, commandVbs)
} else if (!fs.existsSync(wscript)) {
fs.writeFileSync(wscript, commandVbs)
}
/**
* 创建桌面快捷方式
* @param {*} target
*/
function lnk2Desktop (target = '', deskname = '') {
// 路径处理
target = path.resolve(target)
if (!fs.existsSync(target)) return
if (!path.isAbsolute(target)) target = path.resolve(__dirname, target)
let fileDes = path.parse(target)
// 桌面文件地址
let deskpath = path.resolve(desktop, `${deskname || fileDes.name}.lnk`)
// 图标
let icon = /(\.exe)|(\.dll)/.test(fileDes.ext) ? `${target},0` : target
// 执行创建桌面图标
spawn('wscript', [wscript, deskpath, target, '', '', fileDes.dir, icon, 1, ''])
}
// 读取需要创建文件的配置表
const desktopContent = fs.readFileSync('desktop.txt').toString().split('\n')
desktopContent.forEach(n => lnk2Desktop(...n.split('=').reverse()))