Sign in to follow this  
Followers 0

_ShellAll() - Create an entry in the shell contextmenu when selecting a file and folder, includes the program icon as well.

6 posts in this topic

Posted (edited)

I created this after I developed and because I wanted to add an entry to the contextmenu when selecting any file or folder.

The entry will pass the file/folder name to your program via a commandline argument, so you'll have to use $CmdLine/$CmdLineRaw to access the file/folder that was selected.

Any problems or suggestions then please post below. Thanks.

UDF:

#include-once

; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
; #INDEX# =======================================================================================================================
; Title .........: _ShellAll
; AutoIt Version : v3.2.12.1 or higher
; Language ......: English
; Description ...: Create an entry in the shell contextmenu when selecting a file and folder, includes the program icon as well.
; Note ..........:
; Author(s) .....: guinness
; Remarks .......: Special thanks to KaFu for EnumRegKeys2Array() which I used as inspiration for enumerating the Registry Keys.
; ===============================================================================================================================

; #INCLUDES# =========================================================================================================
; None

; #GLOBAL VARIABLES# =================================================================================================
; None

; #CURRENT# =====================================================================================================================
; _ShellAll_Install: Creates an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu, but only displays when selecting a file and folder.
; _ShellAll_Uninstall: Deletes an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu.
; ===============================================================================================================================

; #INTERNAL_USE_ONLY#============================================================================================================
; __ShellAll_RegistryGet ......; Retrieve an array of registry entries for a specific key.
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name ..........: _ShellAll_Install
; Description ...: Creates an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu, but only displays when selecting a file and folder.
; Syntax ........: _ShellAll_Install($sText[, $sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $sIconPath = @ScriptFullPath[,
;                  $iIcon = 0[, $fAllUsers = False[, $fExtended = False]]]]]])
; Parameters ....: $sText               - Text to be shown in the contextmenu.
;                  $sName               - [optional] Name of the program. Default is @ScriptName.
;                  $sFilePath           - [optional] Location of the program executable. Default is @ScriptFullPath.
;                  $sIconPath           - [optional] Location of the icon e.g. program executable or dll file. Default is @ScriptFullPath.
;                  $iIcon               - [optional] Index of icon to be used. Default is 0.
;                  $fAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
;                  $fExtended           - [optional] Show in the Extended contextmenu using Shift + Right click. Default is False.
; Return values .: Success - RegWrite() Return code.
;                  Failure - none
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _ShellAll_Install($sText, $sName = @ScriptName, $sFilePath = @ScriptFullPath, $sIconPath = @ScriptFullPath, $iIcon = 0, $fAllUsers = False, $fExtended = False)
    Local $aArray[3] = [2, '*', 'Directory'], $i64Bit = ''

    If $iIcon = Default Then
        $iIcon = 0
    EndIf
    If $sFilePath = Default Then
        $sFilePath = @ScriptFullPath
    EndIf
    If $sIconPath = Default Then
        $sIconPath = @ScriptFullPath
    EndIf
    If $sName = Default Then
        $sName = @ScriptName
    EndIf
    If @OSArch = 'X64' Then
        $i64Bit = '64'
    EndIf
    For $i = 1 To $aArray[0]
        If $fAllUsers Then
            $aArray[$i] = 'HKEY_LOCAL_MACHINE' & $i64Bit & 'SOFTWAREClasses' & $aArray[$i] & 'shell'
        Else
            $aArray[$i] = 'HKEY_CURRENT_USER' & $i64Bit & 'SOFTWAREClasses' & $aArray[$i] & 'shell'
        EndIf
    Next

    $sName = StringRegExpReplace($sName, '.[^./]*$', '')
    If StringStripWS($sName, 8) = '' Or FileExists($sFilePath) = 0 Then
        Return SetError(1, 0, 0)
    EndIf

    _ShellAll_Uninstall($sName, $fAllUsers)

    For $i = 1 To $aArray[0]
        RegWrite($aArray[$i] & $sName, '', 'REG_SZ', $sText)
        RegWrite($aArray[$i] & $sName, 'Icon', 'REG_EXPAND_SZ', $sIconPath & ',' & $iIcon)
        RegWrite($aArray[$i] & $sName & 'command', '', 'REG_SZ', '"' & $sFilePath & '" "%1"')
        If $fExtended Then
            RegWrite($aArray[$i], 'Extended', 'REG_SZ', '')
        EndIf
    Next
    Return SetError(@error, 0, @error)
EndFunc   ;==>_ShellAll_Install

; #FUNCTION# ====================================================================================================================
; Name ..........: _ShellAll_Uninstall
; Description ...: Deletes an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu.
; Syntax ........: _ShellAll_Uninstall([$sName = @ScriptName[, $fAllUsers = False]])
; Parameters ....: $sName               - [optional] Name of the Program. Default is @ScriptName.
;                  $fAllUsers           - [optional] Was it added to Current Users (False) or All Users (True) Default is False.
; Return values .: Success - Returns 2D Array of registry entries.
;                  Failure - Returns 0 and sets @error to non-zero.
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _ShellAll_Uninstall($sName = @ScriptName, $fAllUsers = False)
    Local $aArray[3] = [2, '*', 'Directory'], $i64Bit = ''

    If $sName = Default Then
        $sName = @ScriptName
    EndIf
    If @OSArch = 'X64' Then
        $i64Bit = '64'
    EndIf
    For $i = 1 To $aArray[0]
        If $fAllUsers Then
            $aArray[$i] = 'HKEY_LOCAL_MACHINE' & $i64Bit & 'SOFTWAREClasses' & $aArray[$i] & 'shell'
        Else
            $aArray[$i] = 'HKEY_CURRENT_USER' & $i64Bit & 'SOFTWAREClasses' & $aArray[$i] & 'shell'
        EndIf
    Next

    $sName = StringRegExpReplace($sName, '.[^./]*$', '')
    If StringStripWS($sName, 8) = '' Then
        Return SetError(1, 0, 0)
    EndIf

    Local $aFinal[1][5] = [[0, 5]], $aReturn = 0, $sDelete = ''
    For $i = 1 To $aArray[0]
        $aReturn = __ShellAll_RegistryGet($aArray[$i])

        If $aReturn[0][0] > 0 Then
            For $j = 1 To $aReturn[0][0]
                If $aReturn[$j][0] = $sName And $sDelete <> $aReturn[$j][1] Then
                    $sDelete = $aReturn[$j][1]
                    RegDelete($sDelete)
                EndIf
            Next

            ReDim $aFinal[$aFinal[0][0] + $aReturn[0][0] + 1][$aReturn[0][1]]
            For $j = 1 To $aReturn[0][0]
                $aFinal[0][0] += 1
                For $k = 0 To $aReturn[0][1] - 1
                    $aFinal[$aFinal[0][0]][$k] = $aReturn[$j][$k]
                Next
            Next
            $aFinal[0][1] = $aReturn[0][1]
        EndIf
    Next
    Return $aFinal
EndFunc   ;==>_ShellAll_Uninstall

; #INTERNAL_USE_ONLY#============================================================================================================
Func __ShellAll_RegistryGet($sRegistryKey)
    Local $aArray[1][5] = [[0, 5]], $iCount_1 = 0, $iCount_2 = 0, $iDimension = 0, $iError = 0, $sRegistryKey_All = '', $sRegistryKey_Main = '', $sRegistryKey_Name = '', _
            $sRegistryKey_Value = ''

    While 1
        If $iError Then
            ExitLoop
        EndIf
        $sRegistryKey_Main = RegEnumKey($sRegistryKey, $iCount_1 + 1)
        If @error Then
            $sRegistryKey_All = $sRegistryKey
            $iError = 1
        Else
            $sRegistryKey_All = $sRegistryKey & $sRegistryKey_Main
        EndIf

        $iCount_2 = 0
        While 1
            $sRegistryKey_Name = RegEnumVal($sRegistryKey_All, $iCount_2 + 1)
            If @error Then
                ExitLoop
            EndIf

            If ($aArray[0][0] + 1) >= $iDimension Then
                $iDimension = ($aArray[0][0] + 1) * 2
                ReDim $aArray[$iDimension][$aArray[0][1]]
            EndIf

            $sRegistryKey_Value = RegRead($sRegistryKey_All, $sRegistryKey_Name)
            $aArray[$aArray[0][0] + 1][0] = $sRegistryKey_Main
            $aArray[$aArray[0][0] + 1][1] = $sRegistryKey_All
            $aArray[$aArray[0][0] + 1][2] = $sRegistryKey & $sRegistryKey_Main & '' & $sRegistryKey_Name
            $aArray[$aArray[0][0] + 1][3] = $sRegistryKey_Name
            $aArray[$aArray[0][0] + 1][4] = $sRegistryKey_Value
            $aArray[0][0] += 1
            $iCount_2 += 1
        WEnd
        $iCount_1 += 1
    WEnd
    ReDim $aArray[$aArray[0][0] + 1][$aArray[0][1]]
    Return $aArray
EndFunc   ;==>__ShellAll_RegistryGet

Example 1:

#include '_ShellAll.au3'

Example()

Func Example()
    _ShellAll_Install('Start ShellAll') ; Add the running EXE to the Shell ContextMenu. This will only display when selecting a file and folder.
    Sleep(10000)
    _ShellAll_Uninstall() ; Remove the running EXE from the Shell ContextMenu.
EndFunc   ;==>Example

All of the above has been included in a ZIP file. ShellAll.zip

Extended: A example.

Edited by guinness
BinaryBrother likes this

Share this post


Link to post
Share on other sites



Posted

As always, thanks for sharing!

Share this post


Link to post
Share on other sites

Posted

Thanks. I have a couple more of these explorer 'ContextMenu' addition UDF's in the pipeline.

Share this post


Link to post
Share on other sites

Posted (edited)

I've updated the syntax of the UDF and fixed a bug when checking whether or not an entry existed (though not critical.) Please see the original post for more details. Additionally the Default keyword is now supported. Thanks.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted

hi guinness, thanks for this great UDF, it's exactly what I am after.

One question: how do I set up a context menu item with the sole function of simply getting the full file path of the selected file(s) and pass to array?

E.g. I highlight several files in explorer and right click and select "Get Path" and I get an array with each individual file path

Really appreciate your work and help!

Share this post


Link to post
Share on other sites

Posted

This requires a little bit more work, there is an example in the forum I created. Just search for 'ShellFile' in the General Support Forum.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0