简体   繁体   中英

How to associate file extension to an AutoIt script?

For my text editor made in AutoIt I created a new extension in the Regsitry (.text) and associated it to my program (TextEdit.exe). But when I open filename.text with TextEdit.exe it does not show the title nor content of that file (nothing changes).

How can I associate the file extension (.text) to TextEdit.exe so that it displays the filename as title and file content in the GUICtrlCreateEdit() ? Registry setup for the new extension:

HKEY_CLASSES_ROOT\TextEditor.text\DefaultIcon\Standard (REG_SZ) : E:\Icon.ico
HKEY_CLASSES_ROOT\TextEditor.text\Shell\ Standard (REG_SZ) : Open
HKEY_CLASSES_ROOT\TextEditor.text\Shell\Open\Command\ Standard (REG_SZ) : "E:\TextEditor.exe" --started-from-file "%1"

AutoIt code:

Global $TextEditGUI = GUICreate("TextEditor", 650, 550)
Global $TextEdit_Content = GUICtrlCreateEdit("", 0, 0, 650, 550)
GUISetState()

While 1
  Switch GUIGetMsg()
    Case -3
      Exitloop
  EndSwitch
Wend

"… so that when I open a file.text the TextEditor displays the filename as title and the content into the Edit. "

As per Documentation - Intro - Running Scripts - Command Line Parameters :

The special array $CmdLine is initialized at the start of the script with the command line parameters passed to your AutoIt script.

  1. Change \\Shell\\Open\\Command from "E:\\TextEditor.exe" --started-from-file "%1" to "E:\\TextEditor.exe" "%1" (or address filename by $cmdline[2] instead).
  2. Retrieve file content.
  3. Write content to GUI.

Example:

#include <GUIConstantsEx.au3>; $GUI_EVENT_CLOSE
#include <FileConstants.au3>;  $FO_READ, $FO_UTF8_NOBOM

Global Const $g_sGuiTitle = 'TextEditor - %s'
Global Const $g_iGuiDimX  = 650, _
             $g_iGuiDimY  = 550

Global       $g_hGui      = -1, _
             $g_hCtrlEdit = -1

Main()

Func Main()

    _GuiCreate($g_hGui, $g_hCtrlEdit)
    If $CmdLine[0] And FileExists($CmdLine[1]) Then _TextLoad($g_hGui, $g_hCtrlEdit, $CmdLine[1])

    While Not (GUIGetMsg() = $GUI_EVENT_CLOSE)

        Sleep(0)

    WEnd

    GUIDelete($g_hGui)

    Exit
EndFunc

Func _GuiCreate(ByRef $hGui, ByRef $hCtrlEdit)

    $hGui      = GUICreate(StringFormat($g_sGuiTitle, 'Untitled'), $g_iGuiDimX, $g_iGuiDimY)
    $hCtrlEdit = GUICtrlCreateEdit('', 0, 0, $g_iGuiDimX, $g_iGuiDimY)
    GUISetState(@SW_SHOW, $hGui)

EndFunc

Func _FileGetText(Const $sPathFile)
    Local       $hFile = FileOpen($sPathFile, BitOR($FO_READ, $FO_UTF8_NOBOM))
    Local Const $sText = FileRead($hFile)

    FileClose($hFile)

    Return $sText
EndFunc

Func _TextLoad(ByRef $hGui, ByRef $hCtrlEdit, Const $sPathFile)
    Local Const $sText = _FileGetText($sPathFile)

    WinSetTitle( _
        $hGui, _
        '', _
        StringFormat( _
            $g_sGuiTitle, _
            StringRight( _
                $sPathFile, _
                StringLen($sPathFile) - StringInStr($sPathFile, '\', 0, -1) _
            ) _
        ) _
    )
    GUICtrlSetData($hCtrlEdit, $sText)

EndFunc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM