johnslattery
6/15/2017 - 3:52 PM

Create or edit a windows shell link (shortcut) even if the target doesn't exist.

Create or edit a windows shell link (shortcut) even if the target doesn't exist.

function main() {
  var shell = WScript.CreateObject("WScript.Shell");
  
  // Name
  var shellLinkName = "some-application.lnk";
  var link = shell.CreateShortcut(shellLinkName);

  // Target
  link.TargetPath =
    "c:\\program files (x86)\\microsoft office\\office14\\msaccess.exe";

  // Arguments
  link.Arguments =
    "\\\\host\\share\\dir\\file /wrkgrp \\\\host\\share\\dir\\file";

  // Description
  link.Description = "Some Microsoft Access application.";

  // Hot Key
  // If unset, 'None' will appear in properties. Setting to 'None' raises an
  // exception. Setting to an empty string causes the property to appear
  // empty. It doesn't seem possible to clear the hot key of an existing
  // shell link and return the property display to 'None'.
  //link.HotKey = "ALT+CRTL+A";

  // Icon
  // It seems the icon image is embedded in the shell link. If the icon
  // location is removed, the shell link maintains its face.
  link.IconLocation =
    "c:\\program files (x86)\\microsoft office\\office14\\msaccess.exe, 0";

  // Window Style
  // Normal window, 1; Maximized, 3; Minimized, 7.
  link.WindowStyle = "1";

  // Working Directory
  link.WorkingDirectory =
    "c:\\program files (x86)\\microsoft office\\office14";

  link.Save();
}

main();