Skip to content

Instantly share code, notes, and snippets.

@AHK-just-me
Created December 4, 2013 14:27
Show Gist options
  • Save AHK-just-me/7788277 to your computer and use it in GitHub Desktop.
Save AHK-just-me/7788277 to your computer and use it in GitHub Desktop.
AHKRCMS - AutoHotkey Right-Click Menu Settings
; ======================================================================================================================
; AHKRCMS AutoHotkey Right-Click Menu Settings
; Function: Customise your AHK right-click menu.
; AHK version: AHK 1.1.13.01
; Tested on: Win XP (x86) / Win 7 (x64)
; Version: 1.0.00.00/2013-12-04/just me
; Remarks: All changes are stored in the key HKEY_CURRENT_USER\Software\Classes\AutoHotkey in the current user
; branch of the registry. Changes can be reset easily by clicking the "Restore AHK defaults" button or
; removing the entire key with RegEdit.exe manually.
; The script relies on the current AHK 1.1 default installation registry keys, folder structures and
; file names.
; ======================================================================================================================
#NoEnv
SetBatchLines, -1
; ======================================================================================================================
; Constants
; ======================================================================================================================
Global GUI_Title := "AHK Right-Click Menu Settings"
AHK_BinFiles := {"ANSI 32-bit.bin": "A32", "Unicode 32-bit.bin": "U32", "Unicode 64-bit.bin": "U64"}
AHK_DefBin := "AutoHotkeySC.Bin"
AHK_ExeFiles := {"AutoHotkeyA32.exe": "A32", "AutoHotkeyU32.exe": "U32", "AutoHotkeyU64.exe": "U64"}
AHK_DefExe := "AutoHotkey.exe"
AHK_RegKey := "AutoHotkeyScript"
HKCR_SubKey := "" . AHK_RegKey
HKCU_SubKey := "Software\Classes\" . AHK_RegKey
Errors := {Install: "AHK is not installed properly!`r`nThe program will exit!"}
; ======================================================================================================================
; Check installation
; ======================================================================================================================
; Get the installation directory
RegRead, InstallDir, HKLM, Software\AutoHotkey, InstallDir
If (ErrorLevel) && (A_Is64bitOS) ; check for 32-bit installation on 64-bit OS
RegRead, InstallDir, HKLM, Software\Wow6432Node\AutoHotkey, InstallDir
If (InstallDir = "")
ErrorExit(Errors.Install)
InstallDir .= "\"
CompilerDir := InstallDir . "Compiler\"
; ----------------------------------------------------------------------------------------------------------------------
; Get the installed AHK exe and bin files
DefVersion := A_IsUnicode ? (A_PtrSize = 8 ? "U64" : "U32") : "A32"
FileGetSize, DefExeSize, % InstallDir . AHK_DefExe
If (ErrorLevel)
ErrorExit(Errors.Install)
FileGetSize, DefBinSize, % CompilerDir . AHK_DefBin
If (ErrorLevel)
ErrorExit(Errors.Install)
InstalledExeFiles := {}
For File, Version In AHK_ExeFiles {
FileGetSize, FileSize, % InstallDir . File
If !(ErrorLevel) {
If (FileSize = DefExeSize)
DefVersion := Version
Else
InstalledExeFiles[File] := Version
}
}
InstalledBinFiles := {}
For File, Version In AHK_BinFiles {
FileGetSize, FileSize, % CompilerDir . File
If !(ErrorLevel) && (FileSize <> DefBinSize)
InstalledBinFiles[File] := Version
}
; ----------------------------------------------------------------------------------------------------------------------
; Check for existing user settings in HKCU
RegRead, RegValue, HKCU, % HKCU_SubKey
UserSettings := !(ErrorLevel)
; ----------------------------------------------------------------------------------------------------------------------
; Get the current settings for the right-click menu
RootKey := UserSettings ? "HKCU" : "HKCR"
SubKey := UserSettings ? HKCU_SubKey : HKCR_SubKey
Registry := {}
Loop, % RootKey, % SubKey . "\Shell", 0, 1
{
RegRead, RegValue
CurrentSubKey := SubStr(A_LoopRegSubKey, StrLen(SubKey) + 1)
Registry[CurrentSubKey] := {Name: A_LoopRegName, Type: A_LoopRegType, Value: RegValue}
}
DefAction := Registry["\Shell"].Value
DefEditor := Registry["\Shell\Edit\Command"].Value
DefRunCommand := Registry["\Shell\Open\Command"].Value
DefCompileCommand := Registry["\Shell\Compile\Command"].Value
; ======================================================================================================================
; GUI
; ======================================================================================================================
SysGet, CW, 71 ; SM_CXMENUCHECK -> width of the default menu check-mark bitmap, in pixels.
CW += 3
Gui, Margin, 2, 2
Gui, Add, Tab2, w600 h350, Defaults|Run|Compile
; ----------------------------------------------------------------------------------------------------------------------
; Default action and editor
Gui, Tab, Defaults
Gui, Add, Text, x+10 y+10 Section, Default menu action:
Gui, Add, Radio, xs y+5 vRBEdit, Edit Script
Gui, Add, Radio, x+10 yp vRBRun, Run Script
GuiControl, , % (DefAction = "Edit" ? "RBEdit" : "RBRun"), 1
Gui, Add, CheckBox, xs y+20 gDefaultEditor vCBEditor, Change default editor / options
Gui, Add, Edit, xs+%CW% y+5 h20 w560 vEditor Disabled, % DefEditor
; ----------------------------------------------------------------------------------------------------------------------
; Additional run options
Gui, Tab, Run
Gui, Add, CheckBox, x+10 y+20 vCBOpenDef gDefOption Checked Section, Run Script (%DefVersion% - default)
Gui, Add, Edit, xs+%CW% y+5 w560 vRunDef, % DefRunCommand
For File, Version In InstalledExeFiles {
Gui, Add, CheckBox, xs y+20 vCBOpen%Version% gOpen%Version%, Run Script (%Version%)
StringReplace, RunCommand, DefRunCommand, %AHK_DefExe%, %File%
Gui, Add, Edit, xs+%CW% y+5 w560 vRun%Version% Disabled, % RunCommand
If (Value := Registry["\Shell\Open" . Version . "\Command"].Value) {
GuiControl, , % "CBOpen" . Version, 1
GuiControl, , % "Run" . Version, % Value
GuiControl, Enable, % "Run" . Version
}
}
Gui, Add, CheckBox, xs y+20 vCBOpenV10 gOpenV10, Run Script (v1.0.48.05)
Gui, Add, Edit, xs+%CW% y+5 w560 Disabled vRunV10
If (Value := Registry["\Shell\OpenV10\Command"].Value) {
GuiControl, , CBOpenV10, 1
GuiControl, , RunV10, % Value
GuiControl, Enable, RunV10
}
Gui, Add, CheckBox, xs y+20 vCBOpenV2 gOpenV2, Run Script (v2 alpha)
Gui, Add, Edit, xs+%CW% y+5 w560 Disabled vRunV2
If (Value := Registry["\Shell\OpenV2\Command"].Value) {
GuiControl, , CBOpenV2, 1
GuiControl, , RunV2, % Value
GuiControl, Enable, RunV2
}
; ----------------------------------------------------------------------------------------------------------------------
; Additional compile options
Gui, Tab, Compile
Gui, Add, Text, x+10 y+20 Section, Compile using bin file:
Gui, Add, CheckBox, xs y+20 vCBCompileDef gDefOption Checked Section, Compile to %DefVersion% (default)
Gui, Add, Edit, xs+%CW% y+5 w560 vBinDef, % DefCompileCommand
For File, Version In InstalledBinFiles {
Gui, Add, CheckBox, xs y+20 vCBCompile%Version% gCompile%Version%, Compile to %Version%
Gui, Add, Edit, xs+%CW% y+5 w560 vBin%Version% Disabled, %DefCompileCommand% /bin "%File%"
If (Value := Registry["\Shell\Compile" . Version . "\Command"].Value) {
GuiControl, , CBCompile%Version%, 1
GuiControl, , Bin%Version%, % Value
GuiControl, Enable, Bin%Version%
}
}
; ----------------------------------------------------------------------------------------------------------------------
Gui, Tab
Gui, Add, Button, xm y354 w150 gApplySettings, Apply && Exit
Gui, Add, Button, x227 yp wp vRestore gRestoreDefaults, Restore AHK defaults
Gui, Add, Button, x452 yp wp gGuiClose Default, Abort
If (RootKey = "HKCR")
GuiControl, +Disabled, Restore
Gui, Add, StatusBar, , % " Current settings: " . RootKey
Gui, Show, , % GUI_Title
Return
; ======================================================================================================================
; GUI labels
; ======================================================================================================================
; Close the GUI, sub-command [, Param2, Param3, Param4]and exit
GuiClose:
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
; Allow to change the default editor and/or options
DefaultEditor:
GuiControlGet, Checked, , % A_GuiControl
GuiControl, Enable%Checked%, Editor
Return
; ----------------------------------------------------------------------------------------------------------------------
; Default options are always checked
DefOption:
GuiControl, , % A_GuiControl, 1
Return
; ----------------------------------------------------------------------------------------------------------------------
; Additional menu items to run other installed AHK 1.1 versions
OpenA32:
OpenU32:
OpenU64:
GuiControlGet, Checked, , % A_GuiControl
GuiControl, Enable%Checked%, % "Run" . SubStr(A_ThisLabel, 5)
Return
; ----------------------------------------------------------------------------------------------------------------------
; Additional menu items to run AHK 1.0.45.05 and v2
OpenV10:
OpenV2:
RunControl := "Run" . SubStr(A_ThisLabel, 5)
GuiControlGet, Checked, , % A_GuiControl
If !(Checked) {
GuiControl, , % RunControl
GuiControl, Disable, % RunControl
Return
}
; Get the path of the appropriate AutoHotkey.exe
Prompt := "Select AHK " . (A_ThisLabel = "OpenV10" ? "1.0" : "v2") . " executable file"
Gui, +OwnDialogs
FileSelectFile, Exe, 1, % AHK_DefExe, % Prompt, Exe (*.exe)
If (ErrorLevel) {
GuiControl, , % A_GuiControl, 0
GuiControl, , % RunControl
GuiControl, Disable, % RunControl
Return
}
; Check file name and version
SplitPath, Exe, ExeName
FileGetVersion, ExeVersion, %Exe%
FileVersion := A_ThisLabel = "OpenV10" ? "1.0.48.5" : "2"
If (ExeName <> AHK_DefExe) || (SubStr(ExeVersion, 1, StrLen(FileVersion)) <> FileVersion) {
GuiControl, , % A_GuiControl, 0
GuiControl, , % RunControl
GuiControl, Disable, % RunControl
Return
}
StringReplace, RunCommand, DefRunCommand, %InstallDir%%AHK_DefExe%, %Exe%
GuiControl, , % RunControl, % RunCommand
GuiControl, Enable, % RunControl
Return
; ----------------------------------------------------------------------------------------------------------------------
; Additional menu items to compile using other installed AHK 1.1 bin files
CompileA32:
CompileU32:
CompileU64:
GuiControlGet, Checked, , % A_GuiControl
GuiControl, Enable%Checked%, % "Bin" . SubStr(A_ThisLabel, 8)
Return
; ----------------------------------------------------------------------------------------------------------------------
; Restore default settings (delete HKCU registry entry)
RestoreDefaults:
RegDelete, HKCU, % HKCU_SubKey
Reload
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
; Apply the new settings
ApplySettings:
Gui, Submit, NoHide
; -------------------------------------------------------------------------------------------------------------------
; Delete the current user settings, if present
RegDelete, HKCU, % HKCU_SubKey
; -------------------------------------------------------------------------------------------------------------------
; Copy the default registry entries from HKCR to HKCU
Loop, HKCR, % HKCR_SubKey, 0, 1
{
RegRead, RegValue
StringReplace, CurrentSubKey, A_LoopRegSubKey, % HKCR_SubKey, % HKCU_SubKey
RegWrite, % A_LoopRegType, HKCU, % CurrentSubKey, % A_LoopRegName, % RegValue
}
; -------------------------------------------------------------------------------------------------------------------
; Tab Defaults
SubKey := HKCU_SubKey . "\Shell"
RegValue := RBEdit ? "Edit" : "Open"
RegWrite, REG_SZ, HKCU, % SubKey, , % RegValue
If (CBEditor)
RegWrite, REG_SZ, HKCU, % SubKey . "\Edit\Command", , % Editor
; -------------------------------------------------------------------------------------------------------------------
; Tab Run - I know it could be shortened, but I'm too lazy
GuiControlGet, OpenDef, , CBOpenDef, Text
RegWrite, REG_SZ, HKCU, % SubKey . "\Open", , % OpenDef
RegWrite, REG_SZ, HKCU, % SubKey . "\Open\Command", , % RunDef
If (CBOpenA32) {
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenA32", , Run Script (A32)
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenA32\Command", , % RunA32
}
If (CBOpenU32) {
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenU32", , Run Script (U32)
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenU32\Command", , % RunU32
}
If (CBOpenU64) {
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenU64", , Run Script (U64)
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenU64\Command", , % RunU64
}
If (CBOpenV10) {
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenV10", , Run Script (v1.0.48.05)
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenV10\Command", , % RunV10
}
If (CBOpenV2) {
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenV2", , Run Script (v2 alpha)
RegWrite, REG_SZ, HKCU, % SubKey . "\OpenV2\Command", , % RunV2
}
; -------------------------------------------------------------------------------------------------------------------
; Tab Compile - I know it could be shortened, but I'm too lazy
GuiControlGet, CompileDef, , CBCompileDef, Text
RegWrite, REG_SZ, HKCU, % SubKey . "\Compile", , % CompileDef
RegWrite, REG_SZ, HKCU, % SubKey . "\Compile\Command", , % BinDef
If (CBCompileA32) {
RegWrite, REG_SZ, HKCU, % SubKey . "\CompileA32", , Compile to A32
RegWrite, REG_SZ, HKCU, % SubKey . "\CompileA32\Command", , % CompileA32
}
If (CBCompileU32) {
RegWrite, REG_SZ, HKCU, % SubKey . "\CompileU32", , Compile to U32
RegWrite, REG_SZ, HKCU, % SubKey . "\CompileU32\Command", , % CompileU32
}
If (CBCompileU64) {
RegWrite, REG_SZ, HKCU, % SubKey . "\CompileU64", , Compile to U64
RegWrite, REG_SZ, HKCU, % SubKey . "\CompileU64\Command", , % CompileU64
}
ExitApp
; ======================================================================================================================
; Functions
; ======================================================================================================================
; Report error and exit
ErrorExit(Msg) {
MsgBox, 16, % GUI_Title, % Msg
ExitApp
}
; ======================================================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment