Lua中创建lnk快捷方式(不使用lfs)

我想从我的Lua脚本中编写一个函数来创建一个Windows .lnk文件。我在LuaFileSystem 中找到了一个函数。有没有不使用库实现这个的方法?(原因:我正在为多个用户编写脚本,如果我们不必在每台机器上安装库就好了。)

感谢帮助!

点赞
用户6834680
用户6834680

制作一个快捷方式(.lnk文件)

-- 你的.lnk文件
local your_shortcut_name = "your_shortcut.lnk"

-- 包括路径的目标文件(或文件夹)
local your_target_filespec = [[C:\Windows\notepad.exe]]

local ps = io.popen("powershell -command -", "w")
ps:write("$ws = New-Object -ComObject WScript.Shell;$s = $ws.CreateShortcut('"..your_shortcut_name.."');$s.TargetPath = '"..your_target_filespec.."';$s.Save()")
ps:close()

使用os.execute"mklink ..."创建符号链接

2016-12-09 16:43:04
用户8301479
用户8301479

使用 luacom 比 Powershell 更快

local luacom=require'luacom'
local shortcut_file_path='test_create_shortcut.lnk'
local target_file_path=arg[0]
local shellObject=luacom.CreateObject("WScript.Shell")
local shortcut=shellObject:CreateShortcut(shortcut_file_path)
shortcut.TargetPath=target_file_path
shortcut:Save()
assert(io.open(shortcut_file_path)):close()--shortcut file exist now
os.remove(shortcut_file_path)

而使用 FileSystemObject 对象 (另一个 COM),或 Windows shell link file format spec for Kaitai Struct (解析二进制文件结构以获取有关各种文件格式的信息)来检索快捷方式信息。这是 'lfs' 现在无法完成的。

参见:使用 Windows Script Host 创建桌面快捷方式- Windows Client | Microsoft Docs LuaCOM 用户手册(版本 1.3)

2022-05-22 13:02:49