Results 1 to 2 of 2

Thread: Shell Extensions

  1. #1

    Default Image Conversion Shell Extensions (VBScript)

    Hi all, I've updated my VBScript shell extension for Irfanview to work with the 64 bit version. It will now work with either version of Irfanview, but be aware that if you have both versions installed it will prefer the 64-bit version. Theoretically you could force it to install for 32-bit by running the file from the Irfanview folder to install. I know Irfan has his own shell extensions, but I prefer mine, and maybe you will too. It supports silent installation. It writes its own ini file in a folder called i_conv in the program's installation folder, I noticed that Irfanview 64 bit now uses a unicode ini file by default, but the utf-8 file made for the shell extension image conversions is accepted just fine, so I don't want to change it and possibly break compatibility with older versions. It doesn't matter if Irfanview converts the file, it's never read by the VBScript.

    Download Link

    You don't have to download it, you can paste the code into notepad, review it and then save it if you prefer. It's commented and easy to understand.

    Code:
    ' 2006-2018 coded by Aractus: https://blog.aractus.com/i_conv.vbs
    ' GPL License: https://www.gnu.org/licenses/gpl.html
    '
    ' This program is designed for use with IrfanView. It will add or remove
    ' shell extensions for converting between several popular image formats.
    ' If required you could add more to the list.
    ' In no way am I affiliated with IrfanView.
    '
    ' Installation:
    ' 1. Double click on i_conv.vbs to install/uninstall
    ' 2. Run "WScript.exe i_conv.vbs" from a command prompt or the run dialogue.
    ' 3. Run "WScript.exe i_conv.vbs /s" to achieve a silent installation.
    '
    ' Usage:
    ' Right click a picture with a supported format (by default BMP, PNG, GIF,
    ' JPG and ICO) and click "Convert to XXX" Where XXX is desired type.
    '
    ' Notes on installation:
    ' i_conv.vbs can be run from anywhere to install so long as IrfanView is
    ' installed. If IrfanView isn't found the script will not install the
    ' extensions, but will uninstall if asked. If IrfanView was not installed
    ' correctly, or if it isn't found automatically with installation simply
    ' copy this file to the IrfanView folder and run from there.
    '
    ' When this file is run it will automatically place a copy into your
    ' IrfanView folder (where possible), and you can delete any other installation
    ' point as it will not be needed.
    '
    ' You can uninstall the shell extensions through "Add or Remove Programs" in
    ' Control Panel. Uninstallation will not delete i_conv.vbs, delete the file
    ' manually if required.
    '
    ' If you prefer you can use /silent instead of /s. Silent installations
    ' suppress error messages if it cannot be installed.
    '
    ' Other Notes:
    ' This will also write a configuration file for use exclusively with the shell
    ' extensions. You can modify it at any time. It is put in a new folder named
    ' "i_conv".
    ' --------------------------------------------------------------------------
    
    ' Define variables.
    Option Explicit
    On Error Resume Next
    Dim WshShell,fso,a,f,fn,i,j,o,p,p2,p3,p4,t,u,v,w:w=0
    Set WshShell=WScript.CreateObject("WScript.Shell")
    Set fso=CreateObject("Scripting.FileSystemObject")
    p=Left(WScript.ScriptFullName,Len(WScript.ScriptFullName)-Len(WScript.ScriptName))
    If Wscript.Arguments.Count=1 Then
      If Wscript.Arguments(0)="REM" Then
    ' This is invoked from "Add or Remove Programs".
    	v=MsgBox("Are you sure you want to remove the image-conversion shell extensions?",292,"Remove shell extensions confirmation")
    	If v=7 Then WshShell=Nothing:Wscript.Quit(1)
    	w=7
      ElseIf UCase(Wscript.Arguments(0))="/S" Or UCase(Wscript.Arguments(0))="/SILENT" Then
    ' If We are running a silent install preset variables.
    	w=6
      Else
    ' We have invalid command parameters.
    	ErrHndlr()
      End If
    
    ' This is the part used for conversion, it's extremely simple - it takes two
    ' variables, one is a file name, the other a file extensions - such as BMP and
    ' JPG, and tells IrfanView to convert file name to file format, after checking
    ' if destination file exists, and if necessary asking the user whether to
    ' overwrite or not.
    ElseIf Wscript.Arguments.Count=2 Then
      If fso.fileexists(p&"i_view32.exe") Then
            fn=p&"i_view32.exe"
      Else
            fn=p&"i_view64.exe"
      End If
      t=Wscript.Arguments(0):u=Wscript.Arguments(1)
      For i=Len(t) To 2 Step -1
    	If Mid(t,i,1)="." Then Exit For
      Next'i
      If i=1 Then ErrHndlr()' We have invalid command parameters.
      v=Right(t,Len(t)-i)
      If v=UCase(v)Then u=UCase(u)
      u=Left(t,i)&u
      If fso.fileexists(u) Then
    	v=MsgBox("Destination file exists, ok to overwrite?",292,"Overwrite confirmation")
    	If v=7 Then WshShell=Nothing:Wscript.Quit(1)
      End If
      WshShell.Run(""""&fn&""" """&t&""" /convert="""&u& """ /ini="""&p&"i_conv"""),1
      Set WshShell=Nothing
      Wscript.Quit(1)
    ElseIf Wscript.Arguments.Count<>0 Then
    ' We have invalid command parameters.
      ErrHndlr()
    End If
    
    ' Everything after this is only run if you open the file directly. This means
    ' you either want to set up the shell extensions, or remove them. We check
    ' first for IrfanView, and if we can't find it then installing the shell
    ' extensions will be disabled.
    If fso.fileexists(p&"i_view32.exe") Or fso.fileexists(p&"i_view64.exe") Then
      If UCase(WScript.ScriptFullName)<>UCase(p&"I_CONV.VBS") Then
    ' If IrfanView is found in the folder this file is run from, and i_conv.vbs
    ' is incorrectly named, create a usable copy called i_conv.vbs.
    ' Otherwise we are sweet to go!
    	fso.CopyFile WScript.ScriptFullName,p&"i_conv.vbs"
      End If
    Else
    ' IrfanView wasn't found in the folder that i_conv.vbs was run from, so let's
    ' search for it.. best place to look first is in the registry, IrfanView
    ' doesn't have it's own key under HKLM\SOFTWARE, so we'll see if its
    ' uninstall entry will point the way.
      p=WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\IrfanView64\UninstallString")
      p=Left(p,Len(p)-16)
      p2=WshShell.RegRead("HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\IrfanView64\UninstallString")
      p2=Left(p2,Len(p2)-16)
      p3=WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\IrfanView\UninstallString")
      p3=Left(p,Len(p)-16)
      p4=WshShell.RegRead("HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\IrfanView\UninstallString")
      p4=Left(p2,Len(p2)-16)
    
    ' If IrfanView is found then all we need do is put i_conv.vbs into its folder.
    ' We check two locations for 64bit Windows.
      If fso.fileexists(p&"i_view64.exe") Then
    	fso.CopyFile WScript.ScriptFullName,p&"i_conv.vbs"
      ElseIf fso.fileexists(p2&"i_view64.exe") Then
    	p=p2
    	fso.CopyFile WScript.ScriptFullName,p&"i_conv.vbs"
      ElseIf fso.fileexists(p3&"i_view32.exe") Then
    	p=p3
    	fso.CopyFile WScript.ScriptFullName,p&"i_conv.vbs"
      ElseIf fso.fileexists(p4&"i_view32.exe") Then
    	p=p4
    	fso.CopyFile WScript.ScriptFullName,p&"i_conv.vbs"
    
    ' We still haven't found IrfanView... so the other place to look is in the
    ' computer's Program Files folder. We won't find it if the user has installed
    ' to a personalized location.
    ' As above we will need to put i_conv.vbs in the IrfanView folder.
      ElseIf fso.fileexists(WshShell.Environment("PROCESS")("ProgramFiles")&"\irfanview\i_view64.exe") Then
    	p=WshShell.Environment("PROCESS")("ProgramFiles")&"\IrfanView\"
    	fso.CopyFile WScript.ScriptFullName,p&"i_conv.vbs"
      ElseIf fso.fileexists(WshShell.Environment("PROCESS")("ProgramFiles")&"\irfanview\i_view32.exe") Then
    	p=WshShell.Environment("PROCESS")("ProgramFiles")&"\IrfanView\"
    	fso.CopyFile WScript.ScriptFullName,p&"i_conv.vbs"
      ElseIf fso.fileexists(WshShell.Environment("PROCESS")("ProgramFiles(x86)")&"\irfanview\i_view32.exe") Then
    	p=WshShell.Environment("PROCESS")("ProgramFiles(x86)")&"\IrfanView\"
    	fso.CopyFile WScript.ScriptFullName,p&"i_conv.vbs"
      Else
    ' IrfanView was not found. Ask if user wants to uninstall extensions and exit.
    ' Unless we're meant to be doing a silent install, in which case we'll just
    ' end without alerting the user.
    	If w=6 Then
    	  WshShell=Nothing:Wscript.Quit(0)
    	ElseIf w<>7 Then
    	  v=MsgBox("IrfanView not found. Would you like to remove the shell extensions?",292,"Remove shell extensions")
    	  If v=7 Then WshShell=Nothing:Wscript.Quit(1)
    	End If
      End If
    End If
    
    ' OK we're now ready to add or remove the shell extensions!
    ' Define the image formats we are going to use - I've included 5 as default,
    ' feel free to add more just remember to change the declaration of the "q"
    ' array to the correct size. It is important that if you want to do this to
    ' first remove the shell extensions before modifying the file.
    Dim q(4):q(0)="bmp":q(1)="gif":q(2)="ico":q(3)="jpg":q(4)="png"
    
    ' Ask user for input, unless we already know to install/uninstall that is.
    If w=0 Then w=MsgBox("Do you want the image-conversion shell extensions?",292,"Enable/Disable shell extensions")
    
    ' Write a custom .ini file for use with conversions, you may customize this
    ' but it's probably easier to edit the .ini once it's installed.
    If w=6 Then
      If fso.fileexists(p&"i_view32.exe") Then
            fn=p&"i_conv\i_view32.ini"
      Else
            fn=p&"i_conv\i_view64.ini"
      End If
      o="[PNG]"&vbCrLf&"CompressionLevel=6"&vbCrLf&"[JPEG]"&vbCrLf&"Save Quality=85"&vbCrLf&"Save Progressive=0"&vbCrLf&_
    	"Save Grayscale=0"&vbCrLf&"KeepExif=0"&vbCrLf&"KeepCom=0"&vbCrLf&"KeepIptc=0"&vbCrLf&"[MultiGIF]"&vbCrLf&_
    	"SaveInterlaced=0"&vbCrLf&"SaveTransparent=0"&vbCrLf&"UsePalette=0"&vbCrLf&"Transparency=0"
      fso.CreateFolder(p&"i_conv"):fso.DeleteFile(fn)
      Set f=fso.CreateTextFile(fn, True):f.Write o:f.Close
    Else
    ' Or delete it if we're uninstalling.
      fso.DeleteFile(fn)
      fso.DeleteFolder(p&"i_conv")
    End If
    ' Go through the registry and make the necessary changes...
    For i=0 To UBound(q)
      a=WshShell.RegRead("HKCR\."&q(i)&"\")
      If a<>"" Then
    	a="HKCR\"&a&"\shell\"
    	For j=0 To UBound(q)
    	  If i<>j Then
    		t=a&"Convert_to_"&UCase(q(j))&"\"
    		If w=6 Then
    		  WshShell.RegWrite t,"Convert to "&UCase(q(j))
    		  WshShell.RegWrite t&"command\","WScript.exe """&p&"i_conv.vbs"" ""%d"" "&q(j)
    		Else
    		  WshShell.RegDelete t&"command\":WshShell.RegDelete t
    		End If
    	  End If
    	Next'j
    	t=a&"Remove_Image_Conversion_Shell_Extensions\"
    	t="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\i_conv\"
    	If w=6 Then
    	  WshShell.RegWrite t,""
    	  WshShell.RegWrite t&"DisplayName","Image Conversion Shell Extensions (Removal)"
    	  WshShell.RegWrite t&"UninstallString","WScript.exe """&p&"i_conv.vbs"" REM"
    	  WshShell.RegWrite t&"NoModify",1,"REG_DWORD"
    	  WshShell.RegWrite t&"NoRepair",1,"REG_DWORD"
    	Else
    	  WshShell.RegDelete t&"NoModify":WshShell.RegDelete t&"NoRepair"
    	  WshShell.RegDelete t&"UninstallString":WshShell.RegDelete t&"DisplayName":WshShell.RegDelete t
    	End If
      End If
    Next'i
    Set WshShell=Nothing
    Wscript.Quit(1)
    
    sub ErrHndlr()
      MsgBox "Invalid parameters were given. The only correct user command line is /s or /silent.",16,"i_conv.vbs error"
      Set WshShell=Nothing
      Wscript.Quit(0)
    End Sub
    You can edit the image formats on line 164... here are the formats I use personally at present:

    Code:
    Dim q(6):q(0)="tif":q(1)="bmp":q(2)="gif":q(3)="ico":q(4)="jp2":q(5)="jpg":q(6)="png"
    ENJOY.

    Key differences compared to the "IrfanView Shell Extension" plugin:

    • My extension only applies to image file formats - Irfanview's one adds the extension to all files! PDF, TXT, etc.
    • My extension will ask if you want to overwrite an existing file - Irfanview's renames the destination file automatically if a file exists.
    • Irfanview's plugin only supports converting to JPG, PNG, TIF, GIF, and BMP. Mine supports ICO by default and you can easily edit the file to add other formats you need, like JP2 and TIF. I didn't support TIF by default because it's a barely used format.
    • My extension uses its own ini file for the conversions, thus allowing you to modify it as you see fit and leaving the main settings untouched. This is useful to set your presets, and when you want to do something unusual you can open irfan view and save manually - like if you want to convert to greyscale JPEG. The advantage being that after you made your greyscale JPEG your shell extension conversion settings are untouched and will still make colour JPEGs.
    • You can uninstall from "Programs and Features" in the Control Panel. But why would you want to?
    Last edited by Aractus; 27.05.2018 at 04:33 AM.

  2. #2
    IV Newbie
    Join Date
    May 2018
    Location
    Санкт-Петербург
    Posts
    1

    Default

    Helpful information.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •