// SendToQuickLaunch.js - Version 1.3
// Copyright (c) 2010, 2013 Bill Chatfield <[email protected]>
// All rights reserved.
var programDesc = "Send to Quick Launch";
var quickLaunchMenuItem = "Quick Launch (create shortcut)";
var installFolder = getEnvironmentVariable("AppData") + "\\" + programDesc;
var installedScript = installFolder + "\\" + WScript.ScriptName;
var shell = null;
var environment = null;
var fileSystem = null;
/* Returns a FileSystemObject object */
function getFileSystem() {
if (fileSystem == null) {
fileSystem = WScript.CreateObject("Scripting.FileSystemObject");
}
return fileSystem;
}
/* Returns a WshShell object */
function getShell() {
if (shell == null) {
shell = WScript.CreateObject("WScript.Shell");
}
return shell;
}
/* Returns a WshEnvironment object */
function getEnvironment() {
if (environment == null) {
environment = getShell().Environment("Process");
}
return environment;
}
/* Returns a String object */
function getEnvironmentVariable(name) {
var env = getEnvironment();
return env(name);
}
/* String */
function askOperation() {
// Ask the user if they want to install.
var answer = getShell().Popup("Install \"Quick Launch\" option in the \"Send To\" menu?", 300, programDesc + " - Admin", 4 + 32);
if (answer == 6) { // If Yes
return 'install';
}
var answer = getShell().Popup("Remove \"Quick Launch\" option from the \"Send To\" menu?", 300, programDesc + " - Admin", 4 + 32);
if (answer == 6) { // If Yes
return 'remove';
}
return '';
}
/* void */
function install() {
// Copy this script into its formal install folder.
if (! getFileSystem().FolderExists(installFolder)) {
getFileSystem().CreateFolder(installFolder);
}
// Destination folder must end in a backslash to be recognized as a folder.
getFileSystem().CopyFile(WScript.ScriptFullName, installFolder + "\\", true);
// Create the link in the SendTo folder that is invoked by the
// "Quick Launch" menu item on the "Send To" menu. This also
// creates the menu item beause Windows creates the "Send To"
// menu from whatever is in the "SendTo" special folder.
var sendToFolder = getShell().SpecialFolders("SendTo");
var link = getShell().CreateShortcut(sendToFolder + "\\" + quickLaunchMenuItem + ".lnk");
link.TargetPath = installedScript;
link.WindowStyle = 1;
link.IconLocation = "wscript.exe, 3"; // The yellow JavaScript icon is at index 3.
link.Description = programDesc;
link.WorkingDirectory = installFolder;
link.Save();
// Create a link in the Start Menu to allow uinstalling of this script.
var programsFolder = getShell().SpecialFolders("Programs");
var startMenuFolder = programsFolder + "\\" + programDesc;
if (! getFileSystem().FolderExists(startMenuFolder)) {
getFileSystem().CreateFolder(startMenuFolder);
}
var startMenuLink = getShell().CreateShortcut(startMenuFolder + "\\Uninstall " + programDesc + ".lnk");
startMenuLink.TargetPath = installedScript;
startMenuLink.Arguments = "--uninstall";
startMenuLink.WindowStyle = 1;
startMenuLink.IconLocation = "wscript.exe, 3";
startMenuLink.Description = "Uninstall " + programDesc;
startMenuLink.WorkingDirectory = installFolder;
startMenuLink.Save();
// Tell the user it was successful.
getShell().Popup("The installation is complete.", 300, programDesc + " - Admin", 64);
}
/* void */
function uninstall() {
// Uninstall Send To link.
var sendToFolder = getShell().SpecialFolders("SendTo");
var link = sendToFolder + "\\" + quickLaunchMenuItem + ".lnk";
if (getFileSystem().FileExists(link)) {
getFileSystem().DeleteFile(link);
}
// Uninstall file and folder in Program Files.
if (getFileSystem().FileExists(installedScript)) {
getFileSystem().DeleteFile(installedScript);
}
if (getFileSystem().FolderExists(installFolder)) {
getShell().CurrentDirectory = "C:\\"; // Move away so we can delete it.
getFileSystem().DeleteFolder(installFolder);
}
// Uninstall Start Menu link and folder.
var programsFolder = getShell().SpecialFolders("Programs");
var startMenuFolder = programsFolder + "\\" + programDesc;
var startMenuLink = startMenuFolder + "\\Uninstall " + programDesc + ".lnk";
if (getFileSystem().FileExists(startMenuLink)) {
getFileSystem().DeleteFile(startMenuLink);
}
if (getFileSystem().FolderExists(startMenuFolder)) {
getFileSystem().DeleteFolder(startMenuFolder);
}
getShell().Popup("It has been removed.", 300, programDesc + " - Admin", 64);
}
/* void */
function createQuickLaunchLinkFor(targetName) {
var target = getFileSystem().GetFile(targetName);
var link = getShell().CreateShortcut(buildShortcutFileName(target));
link.TargetPath = target.Path;
link.WindowStyle = 1;
link.Description = "Shortcut to " + target.Path;
link.WorkingDirectory = target.ParentFolder;
link.Save();
getShell().Popup("A shortcut to " + targetName + " was added to the Quick Launch menu.", 300, programDesc, 64);
}
/* String */
function buildShortcutFileName(/* File */ target) {
var homeDir = getEnvironmentVariable("USERPROFILE");
var quickLaunchDir = homeDir + "\\Desktop";
var name = quickLaunchDir + "\\" + target.Name;
var duplicateFileStartIndex = 2;
var i = duplicateFileStartIndex;
// Check for extensions that should be removed.
if (target.Type == "Application") {
// Remove the extension.
name = name.replace(/\.exe$/i, "");
}
else if (target.Type == "MS-DOS Batch File") {
// Remove the extension.
name = name.replace(/\.bat$/i, "");
}
// If the target is a Shortcut already, then do not append an additional
// link extension to the new Shortcut name. Else...
if (target.Type != "Shortcut") {
// The new Shortcut name does not have a ".lnk" Shortcut extension
// because the target didn't have one, so it needs one now.
name += ".lnk";
}
// Resolve Shortcut name conflicts.
while (getFileSystem().FileExists(name)) {
if (i == duplicateFileStartIndex) {
name = name.replace(/\.lnk$/i, "(" + i + ").lnk");
}
else {
name = name.replace(/\(\d+\)\.lnk$/i, "(" + i + ").lnk");
}
i++; // Increment index and try again.
}
return name;
}
/* Boolean */
function isUninstallSwitch(arg) {
var switches = new Array("--remove", "-remove", "--uninstall", "-uninstall");
var i;
for (i in switches) {
if (arg == switches[i]) {
return true;
}
}
}
/////////////////////////////////////////////////////////////////////////////
//
// Main Program
//
/////////////////////////////////////////////////////////////////////////////
var targetName;
if (WScript.Arguments.length == 0) {
// If there is no argument, then perform an install or remove.
var op = askOperation();
if (op == 'install') {
install();
}
else if (op == 'remove') {
uninstall();
}
}
else {
// There is at least one command line argument.
targetName = WScript.Arguments(0);
if (isUninstallSwitch(targetName)) {
uninstall();
}
else {
// If we have an argument then we were invoked from the SendTo menu,
// so create a Quick Launch link for the given argument.
createQuickLaunchLinkFor(targetName);
}
}
主页
/
user-2280728