Skip to content

Instantly share code, notes, and snippets.

@xnoreq
Created September 3, 2020 20:21
Show Gist options
  • Save xnoreq/b0ebd0636676c6fd47a9ce990a4c7a52 to your computer and use it in GitHub Desktop.
Save xnoreq/b0ebd0636676c6fd47a9ce990a4c7a52 to your computer and use it in GitHub Desktop.
@echo off
setlocal enableextensions enabledelayedexpansion
path %SystemRoot%\System32;%SystemRoot%;%SystemRoot%\System32\Wbem
:: Unattended install flag. When set, the script will not require user input.
set unattended=no
if "%1"=="/u" set unattended=yes
:: Make sure this is Windows Vista or later
call :ensure_vista
:: Make sure the script is running as admin
call :ensure_admin
:: Command line arguments to use when launching qimgv from a file association
set qimgv_args=
:: Get qimgv.exe location
set qimgv_path=%~dp0qimgv.exe
if not exist "%qimgv_path%" call :die "qimgv.exe not found"
:: Get qimgv-document.ico location
set icon_path=%~dp0qimgv.exe,1
:: Register qimgv.exe under the "App Paths" key, so it can be found by
:: ShellExecute, the run command, the start menu, etc.
set app_paths_key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\qimgv.exe
call :reg add "%app_paths_key%" /d "%qimgv_path%" /f
:: Register qimgv.exe under the "Applications" key to add some default verbs for
:: when qimgv is used from the "Open with" menu
set classes_root_key=HKLM\SOFTWARE\Classes
set app_key=%classes_root_key%\Applications\qimgv.exe
call :reg add "%app_key%" /v "FriendlyAppName" /d "qimgv" /f
call :add_verbs "%app_key%"
:: Add qimgv to the "Open with" list for all image file types
call :reg add "%classes_root_key%\SystemFileAssociations\image\OpenWithList\qimgv.exe" /d "" /f
:: Add a capabilities key for qimgv, which is registered later on for use in the
:: "Default Programs" control panel
set capabilities_key=HKLM\SOFTWARE\Clients\Media\qimgv\Capabilities
call :reg add "%capabilities_key%" /v "ApplicationName" /d "qimgv" /f
call :reg add "%capabilities_key%" /v "ApplicationDescription" /d "qimgv image viewer" /f
:: Add file types
set supported_types_key=%app_key%\SupportedTypes
set file_associations_key=%capabilities_key%\FileAssociations
:: Uncompressed formats
call :add_type "image/bmp" "image" "Bitmap Image" ".bmp"
call :add_type "image/gif" "image" "GIF Image" ".gif"
:: Lossless
call :add_type "image/png" "image" "PNG Image" ".png"
call :add_type "image/tiff" "image" "TIFF Image" ".tif" ".tiff"
:: Lossy
call :add_type "image/jpeg" "image" "JPEG Image" ".jpeg" ".jpg"
:: Register "Default Programs" entry
call :reg add "HKLM\SOFTWARE\RegisteredApplications" /v "qimgv" /d "SOFTWARE\Clients\Media\qimgv\Capabilities" /f
echo.
echo Installed successfully^^! You can now configure qimgv's file associations in the
echo Default Programs control panel.
echo.
if [%unattended%] == [yes] exit 0
<nul set /p =Press any key to open the Default Programs control panel . . .
pause >nul
control /name Microsoft.DefaultPrograms
exit 0
:die
if not [%1] == [] echo %~1
if [%unattended%] == [yes] exit 1
pause
exit 1
:ensure_admin
set "dv==::"
if defined !dv! (
echo This batch script requires administrator privileges. Right-click on
echo qimgv-install.bat and select "Run as administrator".
call :die
)
goto :EOF
:ensure_vista
ver | find "XP" >nul
if not errorlevel 1 (
echo This batch script only works on Windows Vista and later. To create file
echo associations on Windows XP, right click on a image file and use "Open with...".
call :die
)
goto :EOF
:reg
:: Wrap the reg command to check for errors
>nul reg %*
if errorlevel 1 set error=yes
if [%error%] == [yes] echo Error in command: reg %*
if [%error%] == [yes] call :die
goto :EOF
:reg_set_opt
:: Set a value in the registry if it doesn't already exist
set key=%~1
set value=%~2
set data=%~3
reg query "%key%" /v "%value%" >nul 2>&1
if errorlevel 1 call :reg add "%key%" /v "%value%" /d "%data%"
goto :EOF
:add_verbs
set key=%~1
:: Set the default verb to "play"
call :reg add "%key%\shell" /d "play" /f
:: Hide the "open" verb from the context menu, since it's the same as "play"
call :reg add "%key%\shell\open" /v "LegacyDisable" /f
:: Set open command
call :reg add "%key%\shell\open\command" /d "\"%qimgv_path%\" %qimgv_args% \"%%%%L\"" /f
:: Add "play" verb
call :reg add "%key%\shell\play" /d "&Play" /f
call :reg add "%key%\shell\play\command" /d "\"%qimgv_path%\" %qimgv_args% \"%%%%L\"" /f
goto :EOF
:add_progid
set prog_id=%~1
set friendly_name=%~2
:: Add ProgId, edit flags are FTA_OpenIsSafe | FTA_AlwaysUseDirectInvoke
set prog_id_key=%classes_root_key%\%prog_id%
call :reg add "%prog_id_key%" /d "%friendly_name%" /f
call :reg add "%prog_id_key%" /v "EditFlags" /t REG_DWORD /d 4259840 /f
call :reg add "%prog_id_key%" /v "FriendlyTypeName" /d "%friendly_name%" /f
call :reg add "%prog_id_key%\DefaultIcon" /d "%icon_path%" /f
call :add_verbs "%prog_id_key%"
goto :EOF
:update_extension
set extension=%~1
set prog_id=%~2
set mime_type=%~3
set perceived_type=%~4
:: Add information about the file extension, if not already present
set extension_key=%classes_root_key%\%extension%
if not [%mime_type%] == [] call :reg_set_opt "%extension_key%" "Content Type" "%mime_type%"
if not [%perceived_type%] == [] call :reg_set_opt "%extension_key%" "PerceivedType" "%perceived_type%"
call :reg add "%extension_key%\OpenWithProgIds" /v "%prog_id%" /f
:: Add type to SupportedTypes
call :reg add "%supported_types_key%" /v "%extension%" /f
:: Add type to the Default Programs control panel
call :reg add "%file_associations_key%" /v "%extension%" /d "%prog_id%" /f
goto :EOF
:add_type
set mime_type=%~1
set perceived_type=%~2
set friendly_name=%~3
set extension=%~4
echo Adding "%extension%" file type
:: Add ProgId
set prog_id=io.qimgv%extension%
call :add_progid "%prog_id%" "%friendly_name%"
:: Add extensions
:extension_loop
call :update_extension "%extension%" "%prog_id%" "%mime_type%" "%perceived_type%"
:: Trailing parameters are additional extensions
shift /4
set extension=%~4
if not [%extension%] == [] goto extension_loop
goto :EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment