[Gui Script] Register .ahkh file type for H1

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

[Gui Script] Register .ahkh file type for H1

Post by tmplinshi » 26 Sep 2020, 03:56

Code: Select all

RunAsAdmin(true)

Gui, Add, Text, w50 Right , Dir:
Gui, Add, Edit, x+10 w400 HWNDhEdit1 vDir, ahkdll-v1-release-master\Win32w
Gui, Add, Text, w50 Right xm, Editor:
Gui, Add, Edit, x+10 w400 vEditor, notepad.exe
Gui, Add, Button, y+30 xp+100, Register
Gui, Show
Return

ButtonRegister:
	Gui, Submit, NoHide
	if RegisterAhkH1(dir, editor)
		MsgBox, 64,, Register success!
Return

GuiClose:
ExitApp

; RegisterAhkH1( "C:\code\ahkdll-v1-release-master\Win32w")
RegisterAhkH1(dir, editor := "notepad.exe") {
	RunAsAdmin(true)

	ext          := ".ahkh"
	fileType     := "AutoHotkeyScriptH1"
	fileTypeName := "AutoHotkey Script H1"

	dir := Trim(dir, """")

	ahkh_exe     := dir . "\AutoHotkey.exe"
	ahkh_ahk2exe := dir . "\..\Compiler\Ahk2Exe.ahk"
	ahkh_bin     := dir . "\AutoHotkeySC.bin"

	for i, path in [dir, ahkh_exe, ahkh_ahk2exe, ahkh_bin]
		if !FileExist(path)
			throw path " does not exist"

	RegWrite, REG_SZ, HKEY_CLASSES_ROOT\%ext%,, %fileType%
	RegWrite, REG_SZ, HKEY_CLASSES_ROOT\%ext%\ShellNew, FileName, Template.ahk

	regPath := "HKEY_CLASSES_ROOT\" . fileType
	RegWrite, REG_SZ, %regPath%,, %fileTypeName%
	RegWrite, REG_SZ, %regPath%\DefaultIcon,, %ahkh_exe%`,7
	RegWrite, REG_SZ, %regPath%\Shell,, Open
	RegWrite, REG_SZ, %regPath%\Shell\Compile,, Compile Script
	RegWrite, REG_SZ, %regPath%\Shell\Compile\Command,, "%ahkh_exe%" "%ahkh_ahk2exe%" /bin "%ahkh_bin%" /in "`%l" `%*
	RegWrite, REG_SZ, %regPath%\Shell\Edit,, Edit Script
	RegWrite, REG_SZ, %regPath%\Shell\Edit\Command,, "%editor%" `%1
	RegWrite, REG_SZ, %regPath%\Shell\Open,, Run Script
	RegWrite, REG_SZ, %regPath%\Shell\Open\Command,, "%ahkh_exe%" "`%1" `%*
	RegWrite, REG_SZ, %regPath%\Shell\RunAs, HasLUAShield
	RegWrite, REG_SZ, %regPath%\Shell\RunAs\Command,, "%ahkh_exe%" "`%1" `%*
	RegWrite, REG_SZ, %regPath%\ShellEx\DropHandler,, {86C86720-42A0-1069-A2E8-08002B30309D}
	return true
}

; DeleteFileType(".ahkh")
DeleteFileType(ext) {
	RunAsAdmin(true)

	MsgBox, 36, Confirm Delete, Delete file type %ext%?
	IfMsgBox, No
		return

	RegRead, fileType, HKEY_CLASSES_ROOT\%ext%
	if (fileType != "") {
		RegDelete, HKEY_CLASSES_ROOT\%ext%
		RegDelete, HKEY_CLASSES_ROOT\%fileType%
		return true
	}
}

RunAsAdmin(exitIfNotAdmin)  {
   commandLine := DllCall("GetCommandLine", "str")
   isRestarted := !!RegExMatch(commandLine, " /restart(?!\S)")
   
   while !( A_IsAdmin || isRestarted )  {
      try  {
         if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
         else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
      }
      catch
         break 
      ExitApp
   }
   
   if !A_IsAdmin  {
      MsgBox, Failed to run the script as admin!
      if exitIfNotAdmin
         ExitApp
   }
}
After register:
ahkh.png
ahkh.png (7.58 KiB) Viewed 1734 times

Return to “AutoHotkey_H”