Forum Discussion

Poida's avatar
Poida
Occasional Contributor
6 years ago

Testing Destination of Windows Shortcut lnk in Start Menu Win7x64

Hi,

 

I'm trying to get this JScript from https://support.smartbear.com/viewarticle/8967/ working in TestComplete 12.6 on Win7x64.

 

It's to verify the target value of the shortcut (what it's trying to run).

 

This script is throwing an error:

JavaScript runtime error

ReferenceError: ActiveXObject is not defined

Error location:

Unit "script unit name"

Line 14 Column 18



{
  if (!IsShortcutValid("Programs|Accessories|Paint", "|")) {
    Log.Error("Shortcut is not valid");
    return;
  }
  Log.Message("Shortcut is valid");
}
 
function IsShortcutValid(shortcutPath, separator)
{
  var WshShell, fso, startDir, allUsersStartDir, path, shortcut;
  
  WshShell = new ActiveXObject("WScript.Shell");
  startDir = WshShell.SpecialFolders("StartMenu") + "\\";
  allUsersStartDir = WshShell.SpecialFolders("AllUsersStartMenu") + "\\";
  path = shortcutPath.split(separator).join("\\") + ".lnk";
  fso = new ActiveXObject("Scripting.FileSystemObject");
  if (fso.FileExists(startDir + path)) {
    shortcut = WshShell.CreateShortcut(startDir + path);
    return fso.FileExists(shortcut.TargetPath);
  }
  if (fso.FileExists(allUsersStartDir + path)) {
    shortcut = WshShell.CreateShortcut(allUsersStartDir + path);
    return fso.FileExists(shortcut.TargetPath);
  }
  return false;
}

Any advice greatly appreciated.

 

Pete

  • Poida's avatar
    Poida
    6 years ago

    Thanks for the tips Robert.   This is what I ended up with in case anyone else is looking for this later, in javascript

    function Test_SCExists_GetSCPath()
    {
      if (!IsShortcutValid("Programs|Accessories|Paint", "|")) {
        Log.Error("Shortcut is not valid");
        return;
      }
      Log.Message("Shortcut is valid");
      
      
      var target = GetShortcutTarget("Programs|Accessories|Paint", "|");
      if (!target) {
        Log.Error("Shortcut doesn't exist");
        return;
      }
      Log.Message(target);
      
    }
     
    function IsShortcutValid(shortcutPath, separator)
    {
      var StartDir, path, shortcut;
      StartDir = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\";
      path = shortcutPath.split(separator).join("\\") + ".lnk";
      if (aqFileSystem.Exists(StartDir + path)) {
        shortcut = WshShell.CreateShortcut(StartDir + path);
        return aqFileSystem.Exists(shortcut.TargetPath);
      }
      return false;
    }
    
    
    function GetShortcutTarget(shortcutPath, separator)
    {
      var StartDir, path, shortcut;
      StartDir = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\";
      path = shortcutPath.split(separator).join("\\") + ".lnk";
      if (aqFileSystem.Exists(StartDir + path)) {
        shortcut = WshShell.CreateShortcut(StartDir + path);
        return shortcut.TargetPath;
      }
      return false;
    }
    • Poida's avatar
      Poida
      Occasional Contributor

      Thanks for the tips Robert.   This is what I ended up with in case anyone else is looking for this later, in javascript

      function Test_SCExists_GetSCPath()
      {
        if (!IsShortcutValid("Programs|Accessories|Paint", "|")) {
          Log.Error("Shortcut is not valid");
          return;
        }
        Log.Message("Shortcut is valid");
        
        
        var target = GetShortcutTarget("Programs|Accessories|Paint", "|");
        if (!target) {
          Log.Error("Shortcut doesn't exist");
          return;
        }
        Log.Message(target);
        
      }
       
      function IsShortcutValid(shortcutPath, separator)
      {
        var StartDir, path, shortcut;
        StartDir = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\";
        path = shortcutPath.split(separator).join("\\") + ".lnk";
        if (aqFileSystem.Exists(StartDir + path)) {
          shortcut = WshShell.CreateShortcut(StartDir + path);
          return aqFileSystem.Exists(shortcut.TargetPath);
        }
        return false;
      }
      
      
      function GetShortcutTarget(shortcutPath, separator)
      {
        var StartDir, path, shortcut;
        StartDir = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\";
        path = shortcutPath.split(separator).join("\\") + ".lnk";
        if (aqFileSystem.Exists(StartDir + path)) {
          shortcut = WshShell.CreateShortcut(StartDir + path);
          return shortcut.TargetPath;
        }
        return false;
      }