2

I would like to write a function to create a windows .lnk file from my lua script. I found a function in the LuaFileSystem library . Is there a way to do this without the library? (The reason: I am writing the script for multiple users, would be nice if we don't have to install the library on every machine.)

I appreciate the help!

2
  • 1
    Do you need symlink to a file of shortcut (with an icon) to a file? Commented Dec 9, 2016 at 16:23
  • I doubt that "lfs" library can make a shortcut on Windows. Commented Dec 9, 2016 at 16:44

2 Answers 2

1

To make a shortcut (an .lnk file)

-- your .lnk file
local your_shortcut_name = "your_shortcut.lnk"      

-- target (file or folder) with full path
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()

To make a symlink simply use os.execute"mklink ..."

Sign up to request clarification or add additional context in comments.

Comments

0

Use luacom is faster than 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)

And use FileSystemObject object (another COM), or Windows shell link file format spec for Kaitai Struct (parse binary file struct to get info on various file format) to retrieve shortcut info. Which 'lfs' can't do now.

see: Create a desktop shortcut with Windows Script Host - Windows Client | Microsoft Docs LuaCOM User Manual (Version 1.3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.