Jump to content

_ShellFile() - Create an entry in the shell contextmenu when selecting an assigned filetype, includes the program icon as well.


Recommended Posts

Posted (edited)

I think you need to learn how to debug your code correctly. Look at the way you're enumerating through the $CmdLine array, if the first element is /Allow why would it be a file? Also I can't see any real need for the functions I created, based on the code you have posted.

For $i = 2 $CmdLine[0] ; Start from the 2nd index.

Next

I hope you can understand that this thread has now turned into a "general help and support" section rather than discussion about the UDF, which was fine up until the point you took it away from using the shell context menu.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

You are absolutely right. I'm so sorry.

I'll try to keep my off-topic posts in the help forums.

Again thank you for your precious help.

  • 1 month later...
Posted

Great UDF all work good. Guinness there is a way to know if the a file association is associated with my script?

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Posted

Look at _WinAPI_AssocQueryString in WinAPIEx.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 7 months later...
Posted

G'day guinness

As you are the expert on this subject (you wrote a UDF about it :)) just wondering what the difference between

shellopen

and

shellrun

Obviously I know one is used to RUN the file and one is used to Open the file.

However, if I use _shellInstall then "double click" on a file with the new extension intending to RUN it and there is only the Open key what is the difference?

Also any chance of adding a "_ShellFile_Exists" function to check if the extension has already been setup?

A simple version could just return true/false a more complex could return the elements already set (i.e. the opposite of _ShellFile_Install).

I can write it myself but thought it would be better coming from the source. :)

Thanks for the UDF it's saved me a lot of work!

John

Posted

About _ShellFile_Exists() why not look at using _WinAPI_AssocQueryString() in WinAPIEx.au3 (found in the beta of AutoIt)? As to your other question I will have to do some research.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Thanks guinness

I'd like to keep it in the current version for the moment.  Maybe when the beta is released. :)

I was thinking more along these lines though

; #FUNCTION# ====================================================================================================================
; Name ..........: _ShellFile_Exists
; Description ...: Checks if the filetype is already defined.
; Syntax ........: _ShellFile_Exists($sFileType[, $fAllUsers = False])
; Parameters ....: $sFileType           - Filetype to be associated with the application e.g. .autoit or autoit.
;                  $fAllUsers           - [optional] Check for Current Users (False) or All Users (True) Default is False.
; Return values .: Exists - Returns True
;                  Failure - Returns False and sets @error to non-zero.
; Author ........: Storm-E (Just a slighly modified  _ShellFile_Uninstall)
; Example .......: no
; ===============================================================================================================================
Func _ShellFile_Exists($sFileType, $fAllUsers = False)
    Local $i64Bit = '', $sRegistryKey = ''

    If @OSArch = 'X64' Then
        $i64Bit = '64'
    EndIf
    If $fAllUsers Then
        $sRegistryKey = 'HKEY_LOCAL_MACHINE' & $i64Bit & '\SOFTWARE\Classes\'
    Else
        $sRegistryKey = 'HKEY_CURRENT_USER' & $i64Bit & '\SOFTWARE\Classes\'
    EndIf

    $sFileType = StringRegExpReplace($sFileType, '^\.+', '')
    If StringStripWS($sFileType, 8) = '' Then
        Return SetError(1, 0, False)
    EndIf

    Local $sName = RegRead($sRegistryKey & '.' & $sFileType, '')
    If @error Then
        ; Key not found
        Return False
    EndIf
    ;Key Found *** Possible extention return $sName (Name of the program) *** Not sure why it'd be needed
    Return True
EndFunc   ;==>_ShellFile_Uninstall

To keep it all in the same coding style.

I did some research on the OPEN/RUN differences and everything I found was divided with no clear answer what to use.  Except that "run" was the "correct" way of thinking when "running" a file and "Open" was appropriate if you were "opening" a file.

I'd be great if you found a clear answer.

Thanks for your great work you do here!

John

Posted (edited)

OK, no problem.I have updated the UDF in the first post by removing the 'magic numbers'. I will look at adding your idea at a later date.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 4 months later...
Posted

nice!!

How can i add 'right-click index' for .au3 file types, and put something like 'Compile This to EXE'

And au2exe will compile the selected .au3 into .exe with the same name as .au3, in the same folder?

Please enlighten me

Posted

Doesn't AutoIt already offer this option when you install it? If not you need to edit the au3 shell/open/command to point to the compiler.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  On 10/10/2013 at 7:56 AM, storme said:

I did some research on the OPEN/RUN differences and everything I found was divided with no clear answer what to use.  Except that "run" was the "correct" way of thinking when "running" a file and "Open" was appropriate if you were "opening" a file.

I'd be great if you found a clear answer.

 

this is the same difference when you use ShellExecute() or Run()

try to use

ShellExecute('hostfolderprogram.exe')

Run('hostfolderprogram.exe')

and then you see a difference with security.

Using Run you security prompt not appear.

This is only one difference which I know.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 1 year later...
Posted

It could do with some refactoring, but I have other priorities right now.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 2 months later...
Posted (edited)

@guinness Looks like you have forgot about it :P, Here is some advice for cleaning up:

1. Code from line 50 to 61 is not necessary
2. Change $fAllUsers to $bAllUsers and $fExtended to $bExtended
3. Change $i64Bit to $s64Bit
4. Change $sFilePath to $sProgramPath or $sScriptPath
5. Add a optional $sCommandline = ' "%1"' parameter.

 

TD :D
 

Edited by TheDcoder
Added 5th point and formatted text

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted (edited)

If you don't mind, I modified the UDF for you :)

#include-once

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7

; #INDEX# =======================================================================================================================
; Title .........: _ShellFile
; AutoIt Version : v3.2.12.1 or higher
; Language ......: English
; Description ...: Create an entry in the shell contextmenu when selecting an assigned filetype, includes the program icon as well.
; Note ..........:
; Author(s) .....: guinness
; Remarks .......:
; ===============================================================================================================================

; #INCLUDES# ====================================================================================================================
#include <Constants.au3>

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

; #CURRENT# =====================================================================================================================
; _ShellFile_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 an assigned filetype to the program.
; _ShellFile_Uninstall: Deletes an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu.
; ===============================================================================================================================

; #INTERNAL_USE_ONLY#============================================================================================================
; None
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name ..........: _ShellFile_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 an assigned filetype to the program.
; Syntax ........: _ShellFile_Install($sText, $sFileType[, $sName = @ScriptName[, $sProgramPath = @ScriptFullPath[, $sIconPath = @ScriptFullPath[,
;                  $iIcon = 0[, $bAllUsers = False[, $bExtended = False]]]]]])
; Parameters ....: $sText               - Text to be shown in the contextmenu.
;                  $sFileType           - Filetype to be associated with the application e.g. .autoit or autoit.
;                  $sName               - [optional] Name of the program. Default is @ScriptName.
;                  $sProgramPath        - [optional] Location of the program executable. Default is @ScriptFullPath.
;                  $sCommandline        - [optional] The commandline parameters which should be passed to the program (%1 substitutes for the path of the executed file). Default is "%1".
;                  $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.
;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
;                  $bExtended           - [optional] Show in the Extended contextmenu using Shift + Right click. Default is False.
; Return values .: Success - Returns True
;                  Failure - Returns False and sets @error to non-zero.
; Author ........: guinness
; Modified ......: TheDcoder
; Example .......: Yes
; ===============================================================================================================================
Func _ShellFile_Install($sText, $sFileType, $sName = @ScriptName, $sProgramPath = Default, $sCommandline = '"%1"', $sIconPath = @ScriptFullPath, $iIcon = 0, $bAllUsers = False, $bExtended = False)
    Local $s64Bit = '', $sRegistryKey = ''

    If $sProgramPath = Default Then
        $sProgramPath = @AutoItExe
        If Not @Compiled Then $sCommandline = ' "' & @ScriptFullPath & '" ' & $sCommandline
    EndIf
    If @OSArch = 'X64' Then
        $s64Bit = '64'
    EndIf
    If $bAllUsers Then
        $sRegistryKey = 'HKEY_LOCAL_MACHINE' & $s64Bit & '\SOFTWARE\Classes\'
    Else
        $sRegistryKey = 'HKEY_CURRENT_USER' & $s64Bit & '\SOFTWARE\Classes\'
    EndIf

    $sFileType = StringRegExpReplace($sFileType, '^\.+', '')
    $sName = StringLower(StringRegExpReplace($sName, '\.[^.\\/]*$', ''))
    MsgBox(0, 0, FileExists($sProgramPath))
    If StringStripWS($sName, $STR_STRIPALL) = '' Or FileExists($sProgramPath) = 0 Or StringStripWS($sFileType, $STR_STRIPALL) = '' Then
        Return SetError(1, 0, False)
    EndIf

    _ShellFile_Uninstall($sFileType, $bAllUsers)

    Local $iReturn = 0
    $iReturn += RegWrite($sRegistryKey & '.' & $sFileType, '', 'REG_SZ', $sName)
    $iReturn += RegWrite($sRegistryKey & $sName & '\DefaultIcon\', '', 'REG_SZ', $sIconPath & ',' & $iIcon)
    $iReturn += RegWrite($sRegistryKey & $sName & '\shell\open', '', 'REG_SZ', $sText)
    $iReturn += RegWrite($sRegistryKey & $sName & '\shell\open', 'Icon', 'REG_EXPAND_SZ', $sIconPath & ',' & $iIcon)
    $iReturn += RegWrite($sRegistryKey & $sName & '\shell\open\command\', '', 'REG_SZ', '"' & $sProgramPath & '" ' & $sCommandline)
    $iReturn += RegWrite($sRegistryKey & $sName, '', 'REG_SZ', $sText)
    $iReturn += RegWrite($sRegistryKey & $sName, 'Icon', 'REG_EXPAND_SZ', $sIconPath & ',' & $iIcon)
    $iReturn += RegWrite($sRegistryKey & $sName & '\command', '', 'REG_SZ', '"' & $sProgramPath & '" ' & $sCommandline)
    If $bExtended Then
        $iReturn += RegWrite($sRegistryKey & $sName, 'Extended', 'REG_SZ', '')
    EndIf
    Return $iReturn > 0
EndFunc   ;==>_ShellFile_Install

; #FUNCTION# ====================================================================================================================
; Name ..........: _ShellFile_Uninstall
; Description ...: Deletes an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu.
; Syntax ........: _ShellFile_Uninstall($sFileType[, $bAllUsers = False])
; Parameters ....: $sFileType           - Filetype to be associated with the application e.g. .autoit or autoit.
;                  $bAllUsers           - [optional] Add to Current Users (False) or All Users (True) Default is False.
; Return values .: Success - Returns True
;                  Failure - Returns False and sets @error to non-zero.
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _ShellFile_Uninstall($sFileType, $bAllUsers = False)
    Local $s64Bit = '', $sRegistryKey = ''

    If @OSArch = 'X64' Then
        $s64Bit = '64'
    EndIf
    If $bAllUsers Then
        $sRegistryKey = 'HKEY_LOCAL_MACHINE' & $s64Bit & '\SOFTWARE\Classes\'
    Else
        $sRegistryKey = 'HKEY_CURRENT_USER' & $s64Bit & '\SOFTWARE\Classes\'
    EndIf

    $sFileType = StringRegExpReplace($sFileType, '^\.+', '')
    If StringStripWS($sFileType, $STR_STRIPALL) = '' Then
        Return SetError(1, 0, False)
    EndIf

    Local $iReturn = 0, $sName = RegRead($sRegistryKey & '.' & $sFileType, '')
    If @error Then
        Return SetError(2, 0, False)
    EndIf
    $iReturn += RegDelete($sRegistryKey & '.' & $sFileType)
    $iReturn += RegDelete($sRegistryKey & $sName)
    Return $iReturn > 0
EndFunc   ;==>_ShellFile_Uninstall

 

Edited by TheDcoder
Added Compatablity for .au3 files

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted

Knock yourself out. I would have added a couple of additional fixes/changes, so maybe when I back in AutoIt mode I will do it. Thanks

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Great. You can create an new thread if you want and maintain your own version from there.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...