1. 程式人生 > 其它 >記一次失敗的vbs移植lua luacom

記一次失敗的vbs移植lua luacom

vbs(或者powershell程式碼)

Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
Set shortcut=objShell.CreateShortcut("test.remote.lnk")
shortcut.TargetPath="C:\Users\rober\Documents\Lua\test.lua"
shortcut.Save()

對應

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()--short cut file exist now
os.remove(shortcut_file_path)

特別注意的是,vbs的objShell.CreateShortcut(..)對應lua的shellObject:CreateShortcut(..)(其中的冒號:)。
否則,shellObject.CreateShortcut(shortcut_file_path)報錯"快捷方式路徑名稱需以 .lnk 或 .url 結尾。"("The shortcut pathname must end with .lnk or .url")。
以為是編碼問題折騰了很久..
已在luacom反饋(不過作者消失很久了!),應該報錯校驗self引數失敗,或者2#引數缺失。
shortcut.Save()會報錯"self parameter not found."。