Skip to content

Instantly share code, notes, and snippets.

@rolltidehero
Created March 16, 2022 16:29
Show Gist options
  • Save rolltidehero/8faca313faef07ac91bb44082df29fd0 to your computer and use it in GitHub Desktop.
Save rolltidehero/8faca313faef07ac91bb44082df29fd0 to your computer and use it in GitHub Desktop.
/*#############################################################################\
Shortcuts.xys
Library for creating shortcut files.
[ABOUT]
Author = TheQwerty
Version = 1.0
Date = 2014-11-11 22:00z
Requires = XYplorer v14.50.0000
[]
\#############################################################################*/
/*******************************************************************************
** Prompt User to Create Shortcut
** Small wizard to create a shortcut.
*******************************************************************************/
"&Prompt User to Create Shortcut : createShortcutWizard"
Global $G_PROMPT_USER = true;
Sub '_create';
/*************************************************** END createShortcutWizard */
/*******************************************************************************
** Create Shortcut to run script in XY
** Creates a shortcut to run a script when starting XYplorer.
*******************************************************************************/
"Create Shortcut to &Run Script in XY : createRunXYScriptShortcut"
$scriptFile = InputFile(<xyscripts>, 'xys', 'Select script file to run');
End $scriptFile == '', 'No script file selected';
End 1 != Exists($scriptFile), 'Selected item is not a file.';
$scriptName = GetPathComponent($scriptFile, 'base');
$scriptPath = ResolvePath($scriptFile, <xyscripts>, 1);
$folder = InputFolder(<curpath>, 'Select folder to save shortcut');
if ($folder == '') {
$folder = "%desktopreal%";
}
End 2 != Exists($folder), 'Selected item is not a folder.';
Global $G_SHORTCUT = Trim($folder, '\/', 'R') . '\Run ' . $scriptName . 'in XYplorer.lnk';
Global $G_TARGET = <xy>;
Global $G_ARGS = "/script=""$scriptPath""";
Global $G_DESCRIPTION = "Shortcut to run $scriptName in XYplorer.";
Unset $scriptFile, $scriptName, $scriptPath, $folder;
Sub '_create';
Global $G_ERR;
if ($G_ERR == '') {
Msg "Successfully created shortcut!<crlf 2>$G_SHORTCUT";
} else {
Text "Could not create shortcut.<crlf 2>$G_ERR";
}
/********************************************** END createRunXYScriptShortcut */
/*******************************************************************************
** Delete Temporary VBScripts
** Deletes any left over script files.
*******************************************************************************/
"&Delete Temporary VBScripts : deleteTempScripts"
$vbScript = 'XYplorer-Create-Shortcut*.vbs';
Delete 1, 1, "%temp%\$vbScript|%tmp%\$vbScript|<xyscripts>\$vbScript";
Unset $vbScript;
/****************************************************** END deleteTempScripts */
"-" //--------------------------------------------------------------------------
/*******************************************************************************
** Create Shortcut to XY Example
** Show usage example.
*******************************************************************************/
"&Create Shortcut to XY Example : createExample"
$myPath = Self('file');
if ($myPath != '') {
$myPath = ResolvePath($myPath, <xyscripts>, 1);
} else {
$myPath = 'Shortcut';
}
$fakeDesktop = '%desktopreal%';
$fakeXY = '<xy>';
Text <<<EXAMPLE
The following example demonstrates how to use the Create Shortcut script to
create a shortcut to XYplorer on the user's desktop.
--------------------------------------------------------------------------------
"Create Shortcut to XY on Desktop Example"
// CALL TO CREATE:
// Input: Path to shortcut to create.
Global $G_SHORTCUT = "$fakeDesktop\Awesome File Manager.lnk";
// Input: Path to shortcut's target.
Global $G_TARGET = "$fakeXY";
// Make sure to adjust the path to Shortcut if needed.
Load '$myPath', '_create', 'f';
// Return: Empty string if successful, otherwise error message.
Global $G_ERR;
if ($G_ERR == '') {
goto $G_SHORTCUT;
} else {
echo "$G_ERR";
}
EXAMPLE
, /*width*/, /*height*/, 'Example usage of Create Shortcut';
/********************************************************** END createExample */
"- : _-" //---------------------------------------------------------------------
"- : _-" //---------------------------------------------------------------------
/*******************************************************************************
** Create Shortcut
** Creates a shortcut to a target item.
**
** This script creates and executes a VBScript via CScript.exe to generate a
** shortcut file.
**
** Parameters ------------------------------------------------------------------
** - REQUIRED INPUTS: ----------------------------------------------------------
** G_SHORTCUT
** The file path and name of the shortcut to create.
** This must end with extension '.lnk' or '.url' - if it does not this
** script will automatically append '.lnk'.
** G_TARGET
** The path to the shortcut's target.
**
** - OPTIONAL INPUTS: ----------------------------------------------------------
** G_FAIL_SILENTLY
** Set to true to skip showing error dialogs.
** Defaults to false.
** G_PROMPT_USER
** Set to true to prompt the user for empty/not specified variable values.
** Defaults to false.
** G_OVERWRITE_SHORTCUT
** Set to true to automatically overwrite the shortcut if it exists.
** Defaults to false.
** G_WORKING_DIR
** The directory where target should be started.
** This must be a directory.
** If omitted target's parent is used.
** G_ARGS
** Arguments to pass to target.
** G_DESCRIPTION
** Description / Comments.
** G_HOTKEYS
** Hotkey used to run shortcut.
** G_ICON
** Shortcut's icon.
** Format: "file[, index]" (e.g. "xyplorer.exe, 0")
** G_WINDOW_STYLE
** 1 = Normal Window
** 3 = Maximized
** 7 = Minimized
** if omitted '1' is used.
** G_TIMEOUT
** Maximum time in seconds to wait for script to run.
** This is used when running the VBScript.
** If omitted '60' is used.
**
** - OUTPUTS: ------------------------------------------------------------------
** G_ERR
** If there is any error this variable will be set to a string message.
** Otherwise it will be empty ''.
** -----------------------------------------------------------------------------
**
** See Also:
** "CreateShortcut Method"
** http://msdn.microsoft.com/en-us/library/xsy6k3ys(v=vs.84).aspx
** "WshShortcut Object Properties and Methods"
** http://msdn.microsoft.com/en-us/library/f5y78918(v=vs.84).aspx
*******************************************************************************/
"Create Shortcut||4 : _create"
// ERROR MESSAGES
Global $G_ERR = '';
// FAIL SILENTLY
Global $G_FAIL_SILENTLY;
$beQuiet = $G_FAIL_SILENTLY == '' ? false : $G_FAIL_SILENTLY;
// PROMPT USER
Global $G_PROMPT_USER;
$promptUser = $G_PROMPT_USER == '' ? false : $G_PROMPT_USER;
// OVERWRITE EXISTING SHORTCUTS
Global $G_OVERWRITE_SHORTCUT;
$overwrite = $G_OVERWRITE_SHORTCUT == '' ? false : $G_OVERWRITE_SHORTCUT;
// PATH TO NEW SHORTCUT FILE
Global $G_SHORTCUT;
$shortcutPath = $G_SHORTCUT;
if ($shortcutPath == '') {
if ($promptUser) {
// Prompt user if not specified.
// Ask for folder and file name separately because InputFile would force
// the file to exist, which makes it difficult to preventing overwriting.
$shortcutPath = InputFolder("<curpath>", 'Where to Create Shortcut');
$shortcutFile = Input('Create Shortcut', 'Name of shortcut file.', 'Shortcut.lnk', 's', '');
if ($shortcutFile == '') {
$G_ERR = "Error: User did not enter shortcut file name.";
End true, $beQuiet ? '' : $G_ERR, true;
}
$shortcutPath = $shortcutPath . '\' . $shortcutFile;
Unset $shortcutFile;
} else {
$G_ERR = 'Error: Shortcut path not specified';
End true, $beQuiet ? '' : $G_ERR, true;
}
}
// The shortcut path must end in lnk or url.
if ($shortcutPath UnLikeI '*.lnk' && $shortcutPath UnLikeI '*.url') {
$shortcutPath = $shortcutPath . '.lnk';
}
// Treat collisions as an error.
if (! $overwrite) {
if (Exists($shortcutPath) != 0) {
$G_ERR = "Error: Shortcut already exists at '$shortcutPath'.";
End true, $beQuiet ? '' : $G_ERR, true;
}
}
Unset $overwrite;
// PATH TO TARGET - without arguments.
Global $G_TARGET;
$target = $G_TARGET;
if ($target == '') {
if ($promptUser) {
// Prompt user if not specified.
// We're only going to prompt for files.
$target = InputFile("<curpath>",, 'Shortcut Target');
} else {
$G_ERR = 'Error: Target path not specified';
End true, $beQuiet ? '' : $G_ERR, true;
}
}
// Ensure target exists.
if (Exists($target) == 0) {
$G_ERR = "Error: Target path '$target' does not exist.";
End true, $beQuiet ? '' : $G_ERR, true;
}
// WORKING DIRECTORY
Global $G_WORKING_DIR;
$workingDir = $G_WORKING_DIR;
if ($workingDir == '') {
// Default to target's parent.
$workingDir = GetPathComponent($target, 'path');
if ($workingDir LikeI '?:') {
// Add trailing '\' for drives.
$workingDir = $workingDir . '\';
}
if ($promptUser) {
// Prompt user if not specified.
$workingDir = InputFolder($workingDir, 'Working Directory');
}
}
// Ensure working directory exists.
if (Exists($workingDir) != 2) {
$G_ERR = "Error: Working directory '$workingDir' is not a folder.";
End true, $beQuiet ? '' : $G_ERR, true;
}
// ARGUMENTS
Global $G_ARGS;
$args = $G_ARGS;
if ($args == '' && $promptUser) {
// Prompt user if not specified.
$args = Input('Target Arguments', 'Arguments with which to start target.', '', 's', '');
}
// DESCRIPTION
Global $G_DESCRIPTION;
$description = $G_DESCRIPTION;
if ($description == '' && $promptUser) {
// Prompt user if not specified.
$description = Input('Description', 'Comments for this shortcut.', '', 's', '');
}
// KEYBOARD SHORTCUT
Global $G_HOTKEY;
// ALT+, CTRL+, SHIFT+, EXT+
// Keyname: a-z, 0-9, F1-F12
// Ctrl+Alt+e
$hotkey = $G_HOTKEY;
if ($hotkey == '' && $promptUser) {
// Prompt user if not specified.
$hotkey = Input('Keyboard Shortcut', "Shortcut to start shortcut.<crlf>Can only use modifiers ALT+, CTRL+, or SHIFT+ and keys 0-9, a-f, and F1-F12.<crlf>e.g. CTRL+ALT+e", '', 's', '');
}
// ICON
// Format: file[, idx]
Global $G_ICON;
$icon = $G_ICON;
if ($icon == '') {
if ($promptUser) {
// Prompt user if not specified.
$icon = InputFile($workingDir, 'ico;icl;exe;dll', 'Icon');
// No prompting for index since it would just be asking for a number.
} else {
$icon = $target;
}
}
// WINDOW STYLE
// 1 = Normal Window
// 2 = Maximized
// 7 = Minimized
Global $G_WINDOW_STYLE;
$window = $G_WINDOW_STYLE;
if ($window == '' && $promptUser) {
// Prompt user if not specified.
$window = InputSelect('Window Style', 'Normal|Maximized|Minimized', '|', 32+128, '');
// Remap 2 > 3 and 3 > 7.
$window = ReplaceList($window, '2|3', '3|7', '|');
}
// Default to 1.
if ($window UnLikeI '[137]') {
$window = '1';
}
// TIMEOUT
Global $G_TIMEOUT;
$timeout = $G_TIMEOUT == '' ? 60 : $G_TIMEOUT;
Unset $promptUser;
// Escape quotes for VBS.
$searchList = '"';
$replaceList = '""';
$sep = '|';
$matchCase = 1;
$shortcutPath = ReplaceList($shortcutPath, $searchList, $replaceList, $sep, $matchCase);
$target = ReplaceList($target , $searchList, $replaceList, $sep, $matchCase);
$args = ReplaceList($args , $searchList, $replaceList, $sep, $matchCase);
$description = ReplaceList($description , $searchList, $replaceList, $sep, $matchCase);
$hotkey = ReplaceList($hotkey , $searchList, $replaceList, $sep, $matchCase);
$icon = ReplaceList($icon , $searchList, $replaceList, $sep, $matchCase);
$window = ReplaceList($window , $searchList, $replaceList, $sep, $matchCase);
$workingDir = ReplaceList($workingDir , $searchList, $replaceList, $sep, $matchCase);
Unset $searchList, $replaceList, $sep, $matchCase;
// Create contents of the vbscript to run.
$vbScriptContents = <<<VBSCRIPT
set WshShell = WScript.CreateObject("WScript.Shell")
set oShellLink = WshShell.CreateShortcut("$shortcutPath")
oShellLink.TargetPath = "$target"
oShellLink.Arguments = "$args"
oShellLink.Description = "$description"
oShellLink.Hotkey = "$hotkey"
oShellLink.IconLocation = "$icon"
oShellLink.WindowStyle = $window
oShellLink.WorkingDirectory = "$workingDir"
oShellLink.Save
Set oShellLink = Nothing
VBSCRIPT;
Unset $shortcutPath, $target, $workingDir, $args, $description, $hotkey, $icon, $window;
// Try to write this vbscript to a temp folder.
if (Exists("%temp%") == 2) {
$vbScript = "%temp%";
} elseif (Exists("%tmp%") == 2) {
$vbScript = "%tmp%";
} else {
$vbScript = "<xyscripts>";
}
$vbScript = Trim($vbScript, '\/', 'R') . '\XYplorer-Create-Shortcut.vbs';
// Write out the vbscript file - rename with suffix if needed.
$res = WriteFile($vbScript, $vbScriptContents, 'r', 't');
// Check if written file was renamed and use that instead.
if ($res LikeI '4*') {
$vbScript = GetToken($res, 2, '|');
}
Unset $vbScriptContents;
// Run the vbscript file.
$res = RunRet(<<<COMMAND
"cscript.exe" "$vbScript" //nologo //t:$timeout
COMMAND
);
// Clean up after ourselves.
Delete 1, 0, $vbScript;
Unset $vbScript, $timeout;
// Check for any errors from vbscript.
if ($res != '') {
$G_ERR = 'Error running script.' . <crlf 2> . $res;
End true, $beQuiet ? '' : $G_ERR, true;
}
Unset $beQuiet, $res;
/**************************************************************** END _create */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment