Newer
Older
GoModules / WindowsShortcutFull / WindowsShortcutFull.go
root on 30 Jan 2022 5 KB Typo
  1. /***
  2. * Module to manipulate windows .lnk files
  3. * modified from: https://github.com/nyaosorg/go-windows-shortcut
  4. * Typical usage:
  5.  
  6. //import
  7. shortcut "GoModules/Modules/WindowsShortcutFull"
  8.  
  9. //main
  10. // read shortcut data
  11. Description, workingDir, targetPath, Arguments, Icon, err := shortcut.Read("C:\\Users\\User\\Desktop\\vlc.lnk")
  12. // create new OR update existing shortcut
  13. err := shortcut.Make("C:\\Users\\User\\Desktop\\vlc.lnk", newDescription, newWorkingDir, newTarget, newArgs, newIcon)
  14.  
  15. // create VB script to generate/update a .lnk file (useful for debugging)
  16. shortcut.CreateVBS("C:\\Users\\User\\Desktop\\vlc.lnk", newTarget, newArgs, newWorkingDir, newDescription, newScriptName)
  17. */
  18. package WindowsShortcutFull
  19.  
  20. import (
  21. "bytes"
  22. "fmt"
  23. "io/ioutil"
  24. "os"
  25. "os/exec"
  26. "path/filepath"
  27.  
  28. "github.com/go-ole/go-ole"
  29. "github.com/go-ole/go-ole/oleutil"
  30. )
  31.  
  32. // _WShell is the OLE Object for "WScript.Shell"
  33. type _WShell struct {
  34. agent *ole.IUnknown
  35. dispatch *ole.IDispatch
  36. }
  37.  
  38. // _NewWShell creates OLE Object for "WScript.Shell".
  39. func _NewWShell() (*_WShell, error) {
  40. agent, err := oleutil.CreateObject("WScript.Shell")
  41. if err != nil {
  42. return nil, err
  43. }
  44. dispatch, err := agent.QueryInterface(ole.IID_IDispatch)
  45. if err != nil {
  46. agent.Release()
  47. return nil, err
  48. }
  49. return &_WShell{agent: agent, dispatch: dispatch}, nil
  50. }
  51.  
  52. // Close releases the OLE Object for "WScript.Shell".
  53. func (wsh *_WShell) Close() {
  54. wsh.dispatch.Release()
  55. wsh.agent.Release()
  56. }
  57.  
  58. // Read reads the data of shortcut file. `path` must be absolute path.
  59. func (wsh *_WShell) Read(path string) (description string, workingdir string, targetpath string, arguments string, icon string, err error) {
  60. shortcut, err := oleutil.CallMethod(wsh.dispatch, "CreateShortCut", path)
  61. if err != nil {
  62. return "", "", "", "", "", err
  63. }
  64. shortcutDis := shortcut.ToIDispatch()
  65. defer shortcutDis.Release()
  66. Description, err := oleutil.GetProperty(shortcutDis, "Description")
  67. if err != nil {
  68. return "", "", "", "", "", err
  69. }
  70. workingDir, err := oleutil.GetProperty(shortcutDis, "WorkingDirectory")
  71. if err != nil {
  72. return "", "", "", "", "", err
  73. }
  74. targetPath, err := oleutil.GetProperty(shortcutDis, "TargetPath")
  75. if err != nil {
  76. return "", "", "", "", "", err
  77. }
  78. Arguments, err := oleutil.GetProperty(shortcutDis, "Arguments")
  79. if err != nil {
  80. return "", "", "", "", "", err
  81. }
  82. Icon, err := oleutil.GetProperty(shortcutDis, "IconLocation")
  83. if err != nil {
  84. return "", "", "", "", "", err
  85. }
  86.  
  87. return Description.ToString(), workingDir.ToString(), targetPath.ToString(), Arguments.ToString(), Icon.ToString(), err
  88. }
  89.  
  90. func _read(path string) (Description string, workingDir string, targetPath string, Arguments string, Icon string, err error) {
  91. path, err = filepath.Abs(path)
  92. if err != nil {
  93. return "", "", "", "", "", err
  94. }
  95. wsh, err := _NewWShell()
  96. if err != nil {
  97. return "", "", "", "", "", err
  98. }
  99. defer wsh.Close()
  100.  
  101. return wsh.Read(path)
  102. }
  103.  
  104. // Make makes a shortcut file. `to` must be absolute path.
  105. func (wsh *_WShell) Make(to string, newDescription string, newWorkingDir string, newTarget string, newArgs string, newIcon string) error {
  106. shortcut, err := oleutil.CallMethod(wsh.dispatch, "CreateShortCut", to)
  107. if err != nil {
  108. return err
  109. }
  110. shortcutDis := shortcut.ToIDispatch()
  111. defer shortcutDis.Release()
  112. _ = oleutil.MustPutProperty(shortcutDis, "TargetPath", newTarget)
  113. if err != nil {
  114. return err
  115. }
  116. _, err = oleutil.PutProperty(shortcutDis, "WorkingDirectory", newWorkingDir)
  117. if err != nil {
  118. return err
  119. }
  120. _, err = oleutil.PutProperty(shortcutDis, "Arguments", newArgs)
  121. if err != nil {
  122. return err
  123. }
  124. _, err = oleutil.PutProperty(shortcutDis, "Description", newDescription)
  125. if err != nil {
  126. return err
  127. }
  128. _, err = oleutil.PutProperty(shortcutDis, "IconLocation", newIcon)
  129. if err != nil {
  130. return err
  131. }
  132. _, err = oleutil.CallMethod(shortcutDis, "Save")
  133. return err
  134. }
  135.  
  136. func _make(to string, newDescription string, newWorkingDir string, newTarget string, newArgs string, newIcon string) error {
  137. to, err := filepath.Abs(to)
  138. if err != nil {
  139. return err
  140. }
  141. wsh, err := _NewWShell()
  142. if err != nil {
  143. return err
  144. }
  145. defer wsh.Close()
  146.  
  147. return wsh.Make(to, newDescription, newWorkingDir, newTarget, newArgs, newIcon)
  148. }
  149.  
  150. func CreateVBS(linkName string, target string, arguments string, directory string, description string, destination string) {
  151. var scriptTxt bytes.Buffer
  152. scriptTxt.WriteString("option explicit\n\n")
  153. scriptTxt.WriteString("sub CreateShortCut()\n")
  154. scriptTxt.WriteString("dim objShell, strDesktopPath, objLink\n")
  155. scriptTxt.WriteString("set objShell = CreateObject(\"WScript.Shell\")\n")
  156. scriptTxt.WriteString("set objLink = objShell.CreateShortcut( \"")
  157. scriptTxt.WriteString(linkName)
  158. scriptTxt.WriteString(".lnk\")\n")
  159. scriptTxt.WriteString("objLink.Arguments = \"")
  160. scriptTxt.WriteString(arguments)
  161. scriptTxt.WriteString("\"\n")
  162. scriptTxt.WriteString("objLink.Description = \"")
  163. scriptTxt.WriteString(description)
  164. scriptTxt.WriteString("\"\n")
  165. scriptTxt.WriteString("objLink.TargetPath = \"")
  166. scriptTxt.WriteString(target)
  167. scriptTxt.WriteString("\"\n")
  168. scriptTxt.WriteString("objLink.WindowStyle = 1\n")
  169. scriptTxt.WriteString("objLink.WorkingDirectory = \"")
  170. scriptTxt.WriteString(directory)
  171. scriptTxt.WriteString("\"\n")
  172. scriptTxt.WriteString("objLink.Save\nend sub\n\n")
  173. scriptTxt.WriteString("call CreateShortCut()")
  174. fmt.Print(scriptTxt.String())
  175.  
  176. filename := fmt.Sprintf("lnkTo%s.vbs", destination)
  177. ioutil.WriteFile(filename, scriptTxt.Bytes(), 0777)
  178. cmd := exec.Command("wscript", filename)
  179. err := cmd.Run()
  180. if err != nil {
  181. fmt.Println(err)
  182. }
  183. cmd.Wait()
  184. os.Remove(filename)
  185. return
  186. }
Buy Me A Coffee