// 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);
}
}
发布的代码包含不需要的安装和删除例程,最初是为了在 QuickLaunch 文件夹中创建快捷方式而编写的,并不处理创建文件夹的快捷方式。从该代码开始会给简单的要求增加不必要的复杂性。
Send to
要复制上下文菜单根目录下的>的功能Desktop (create shortcut)
,您只需要一个上下文菜单注册表项AllFileSystemObjects
和一个用于创建快捷方式的简单脚本。该脚本可以用 PowerShell 编写,但除非脚本由 VBScript 或JScript调用,否则控制台会短暂闪烁,因此直接用这两种语言之一编写是有意义的。由于 VBScript 将在 2027 年成为未安装的按需功能,因此JScript是更好的选择。
更改 reg 文件以匹配您保存脚本的位置。
创建桌面快捷方式JS.reg
创建桌面快捷方式.js
根据要求,这里有一个 PowerShell 版本。它的启动速度比JScript慢,并且会闪烁 PowerShell 窗口,因此这是一个较差的解决方案。
创建桌面快捷方式PS.reg
创建桌面快捷方式.ps1
我留给读者一个练习,将上面的 PowerShell 代码转换为一行代码,这样就不需要外部文件了,但请注意,就像微软的
Open PowerShell window here
命令一样,单行代码会被带有撇号的文件夹或文件名所绊倒,例如Bob's Files
。使用注册表或临时文件传递的解决方法%1
是可行的,但很复杂。