Skip to content

Instantly share code, notes, and snippets.

@zudsniper
Last active April 10, 2023 01:39
Show Gist options
  • Save zudsniper/1166ce1610bf28d76459a4aead2ba6a3 to your computer and use it in GitHub Desktop.
Save zudsniper/1166ce1610bf28d76459a4aead2ba6a3 to your computer and use it in GitHub Desktop.
[ChatGPT-4] Bash script to create Windows shortcuts (don't ask why bash I fucking hate Windows)

UPDATE

As of 04/09/2023, neither of these scripts work. ... so maybe we will have jobs for a bit longer.

make_shortcut.sh

generate Windows shortcuts with injected environment variables and a custom icon in the image formats PNG, JPEG, SVG, or ICO.

by ChatGPT-4
human involved being @zudsniper


Windows Batch Mirror

I absolutely despise batch scripting, as it is in every way inferior to bash scripting. However, due to the fact that the target operating system for this script is ONLY windows users due to the windows specific Shortcuts being generated, I decided to cursorily include a make_shortcut.bat for any of you that are down so bad that you lack a bash interpreter like MSYS2 or using WSL. My heart goes out to you; I do not know how you are still alive.

The batch script works in exactly the same way as the bash script, so if you for some god forsaken reason want to avoid bash, you may simply use make_shortcut.bat instead of ./make_shortcut.sh and all examples should work in the same way.

Usage

Replace ./make_shortcut.sh with the path to the script file.

./make_shortcut.sh [OPTIONS] EXECUTABLE

Options

This table outlines all the command line flag options available for this script.

Flag Description
--file (-f) Indicates that the environment variable argument is a file path.
--env (-e) VALUE Specifies an environment variable or a file containing environment variables in the format VAR=VALUE.
--icon FILE Specifies an icon file to use for the shortcut. The file must be in .ico, .jpg, .jpeg, .svg, or .png format.
--help (-h) Display a help message with a list of options and example usage cases.

Examples

This is a set of examples which will hopefully show some potential use cases for this script.

Create a shortcut for my_exe.exe with the environment variables VAR1=value1 and VAR2=value2:

$ ./make_shortcut.sh /path/to/my_exe.exe -e "VAR1=value1 VAR2=value2"

Create a shortcut for my_exe.exe with environment variables from a file:

$ ./make_shortcut.sh /path/to/my_exe.exe -f -e /path/to/.env

Create a shortcut for my_exe.exe with an icon file:

$ ./make_shortcut.sh /path/to/my_exe.exe --icon /path/to/icon.png

Create a shortcut for my_exe.exe with all options:

$ ./make_shortcut.sh /path/to/my_exe.exe -f -e /path/to/.env --icon /path/to/icon.ico

zod.tf

Discord GitHub issue custom search GitHub followers

fullstack development, server administration, web design, branding creation, musical scoring, video editing, and idk another thing

second zod.tf logo

License

Script is MIT Licensed.

@echo off
setlocal EnableDelayedExpansion
REM Make sure at least one argument was provided
if "%~1" == "" (
echo Usage: %~nx0 [OPTIONS] EXECUTABLE
echo.
echo Options:
echo -f Indicates that the environment variable argument is a file path.
echo -e VALUE Specifies an environment variable or a file containing environment variables in the format VAR=VALUE.
echo --icon FILE Specifies an icon file to use for the shortcut. The file must be in .ico, .jpg, .jpeg, .svg, or .png format.
echo -h, --help Display this help message with a list of options and example usage cases.
exit /b 1
)
REM Parse command line arguments
set "executable=%~1"
set "env_values="
set "env_file="
set "icon_file="
shift
:parse_args
if "%~1" == "" goto :done_args
if /i "%~1" == "-f" (
set "env_file=%~2"
shift
) else if /i "%~1" == "-e" (
set "env_values=%~2"
shift
) else if /i "%~1" == "--icon" (
set "icon_file=%~2"
shift
) else if /i "%~1" == "-h" (
call :help
exit /b 0
) else if /i "%~1" == "--help" (
call :help
exit /b 0
) else (
echo Invalid argument: %~1
exit /b 1
)
shift
goto :parse_args
:done_args
REM Convert Unix-style paths to Windows-style paths
for /f "tokens=2 delims=/" %%i in ("%executable%") do set "drive=%%i:"
set "executable=%drive%%~p1%~n1%~x1"
if defined env_file (
for /f "tokens=2 delims=/" %%i in ("%env_file%") do set "drive=%%i:"
set "env_file=%drive%%~p2%~n2%~x2"
)
REM Generate the shortcut file
set "temp_file=%TEMP%\make_shortcut_%RANDOM%.bat"
echo @(setlocal
echo @echo off
echo set "target=%executable%"
if defined env_file (
echo set "env_vars=!env_vars!& call "%env_file%" 2>nul"
) else if defined env_values (
echo set "env_vars=!env_vars!& set "!env_values:!=!"
)
echo set "icon_file=%icon_file%"
echo set "shortcut_file=%executable%.lnk"
echo set "wsh_shell=WScript.CreateObject(\"WScript.Shell\")"
echo set "desktop_folder=wsh_shell.SpecialFolders(\"Desktop\")"
echo set "shortcut=wsh_shell.CreateShortcut(\"%shortcut_file%\")"
echo set "shortcut.TargetPath=%target%"
echo set "shortcut.IconLocation=%icon_file%"
echo set "shortcut.Arguments=%env_vars%"
echo set "shortcut.Save()"
echo endlocal) > "%temp_file%"
call "%temp_file%"
del "%temp_file%"
echo Shortcut created: %shortcut_file%
exit /b 0
:help
echo Usage: %~nx0 [OPTIONS] EXECUTABLE
echo.
echo Usage: %~nx0 [OPTIONS] EXECUTABLE
echo.
echo Options:
echo -f Indicates that the environment variable argument is a file path.
echo -e VALUE Specifies an environment variable or a file containing environment variables in the format VAR=VALUE.
echo --icon FILE Specifies an icon file to use for the shortcut. The file must be in .ico, .jpg, .jpeg, .svg, or .png format.
echo -h, --help Display this help message with a list of options and example usage cases.
exit /b 0
#!/bin/bash
#
# Author: zudsniper
# Date: 04/09/2023
# Filename: make_shortcut.sh
# Description: This script generates a new shortcut for a given executable file,
# with specified environment variables and an icon file added to its command line.
# Parse command line arguments
exe_file=""
env_file=""
env_values=""
icon_file=""
file_flag=false
while [[ $# -gt 0 ]]; do
case "$1" in
--file|-f)
file_flag=true
shift
;;
--env|-e)
shift
if $file_flag; then
env_file="$1"
else
env_values="$1"
fi
shift
;;
--icon|-ico)
shift
icon_file="$1"
shift
;;
--help|-h)
echo "Usage: $0 [OPTIONS] EXECUTABLE"
echo "Generate a new shortcut for the specified executable, with optional environment variables and icon file."
echo ""
echo "Options:"
echo " -f Indicates that the environment variable argument is a file path."
echo " -e VALUE Specifies an environment variable or a file containing environment variables in the format VAR=VALUE."
echo " --icon FILE Specifies an icon file to use for the shortcut."
echo " -h, --help Display this help message and exit."
echo ""
echo "Examples:"
echo " $0 /d/my_exe.exe -e \"VAR1=value1 VAR2=value2\""
echo " $0 /d/my_exe.exe -f -e /path/to/.env --icon /path/to/icon.ico"
echo " $0 /d/my_exe.exe -e \"VAR1=value1\" --icon /path/to/icon.png"
echo " $0 /d/my_exe.exe -e /path/to/.env"
exit 0
;;
*)
exe_file="$1"
shift
;;
esac
done
if [[ -z "$exe_file" ]]; then
echo "Error: No executable file specified."
exit 1
fi
if [[ ! -e "$exe_file" ]]; then
echo "Error: The specified executable file does not exist."
exit 1
fi
# Set environment variables if provided
if [[ -n "$env_file" ]]; then
if [[ ! -e "$env_file" ]]; then
echo "Error: The specified environment variable file does not exist."
exit 1
fi
source "$env_file"
elif [[ -n "$env_values" ]]; then
export $env_values
fi
# Convert UNIX-like path to Windows path if necessary
if [[ "$exe_file" == /?/* ]]; then
exe_file="$(echo "$exe_file" | sed 's/^\/\([a-zA-Z]\)\//\1:\//')"
fi
# Create shortcut
shortcut_file="${exe_file%.*}.lnk"
cmd "/c start /b \"\" \"$exe_file\""
powershell -Command "Start-Sleep -Milliseconds 100; \
\$shortcut = (New-Object -ComObject WScript.Shell).CreateShortcut(\"$shortcut_file\"); \
\$shortcut.TargetPath=\"$exe_file\"; \
\$shortcut.WorkingDirectory=\"\`$(dirname $exe_file)\`\";"
# Set icon if provided
if [[ ! -e "$icon_file" ]]; then
echo "Error: The specified icon file does not exist."
exit 1
fi
extension="${icon_file##*.}"
if [[ "$extension" == "ico" ]]; then
powershell -Command "\$shortcut.IconLocation=\"$icon_file\"; \
\$shortcut.Save()"
else
powershell -Command "\$icon = (New-Object System.Drawing.Icon \"$icon_file\").ToBitmap(); \
\$icon_path = \"${shortcut_file%.*}.png\"; \
\$icon.Save(\$icon_path); \
\$shortcut.IconLocation=\"\$icon_path\"; \
\$shortcut.Save(); \
Remove-Item \"\$icon_path\""
fi
else
powershell -Command "\$shortcut.Save()"
fi
echo "Shortcut created: $shortcut_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment