Skip to content

Instantly share code, notes, and snippets.

@davidair
Created April 18, 2022 16:06
Show Gist options
  • Save davidair/b592969b8b19e39f478d3f0411ed4a24 to your computer and use it in GitHub Desktop.
Save davidair/b592969b8b19e39f478d3f0411ed4a24 to your computer and use it in GitHub Desktop.
Batch file that adds the "ShrinkPDF" context menu
@echo off
echo Installing the Shrink PDF shell extension
::Detecting gswin
where /q gswin64c.exe
IF ERRORLEVEL 1 (
echo Cannot find gswin64c.exe. Make sure you've installed Ghostscript.
echo You can install it with "choco install ghostscript".
EXIT /B
)
:: Admin elevation code via https://stackoverflow.com/a/24665214/497403
:: Check privileges
net file 1>NUL 2>NUL
if not '%errorlevel%' == '0' (
powershell Start-Process -FilePath "%0" -ArgumentList "%cd%" -verb runas >NUL 2>&1
exit /b
)
:: Change directory with passed argument. Processes started with
:: "runas" start with forced C:\Windows\System32 workdir
cd /d %1
for /F "tokens=* USEBACKQ" %%F in (`where gswin64c.exe`) do (
set gswin_path=%%F
)
echo Found gswin: %gswin_path%
reg ADD "HKEY_CLASSES_ROOT\*\shell\ShrinkPDF\command" /ve /t REG_SZ /d "cmd /c move \"%%L\" \"%%L.old.pdf\" & \"%gswin_path%\" -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dQUIET -sOutputFile=\"%%L\" \"%%L.old.pdf\" & del \"%%L.old.pdf\"" /f
echo All done
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment