SUMMARY
In Registry put the target program as Context Menu Create File.exe which writes reg.txt file.
In your main program loop every 1 second to check if reg.txt exists. If it exists, kill the Context Menu Create File.exe and delete the reg.txt file. Then copy the selected file paths and manipulate with them.
If your program loops to check for reg.txt, then you need to start the program before executing context menu either on startup or manually.
I did this with AutoHotkey.
These 2 AutoHotkey scripts below allow you to add a P4 Merge Compare
context menu item in Windows Explorer to open multiple selected files.
You can leave all variable values as they are, but if you want to change the contextMenu
value and program
name then see 3.1.
INSTRUCTIONS:
Create 2 files in the same directory:
Create 1st program Add To Context Menu And Create Startup Shortcut.ahk
RunAsAdmin
Label ensures that the script runs as admin (fixes adding Registry values).
- The
If (!A_IsAdmin)
checks if current user is NOT admin, A_IsAdmin is a built in AutoHotkey variable that returns 1 if user is admin, 0 otherwise.
Run, \*RunAs "%A_ScriptFullPath%"
*RunAs parameter runs the script as admin, "%A_ScriptFullPath%" gets the full path of the current executing script.
ExitApp
exits the current script instance running without admin privileges.
- Because the
Run
command runs the script again with admin privileges it will skip the IF
condition and continue executing code below.
ContextMenuCreateFile:
Label creates a Context Menu Create File.exe which creates a file reg.txt and exits Context Menu Create File.exe after it has written the file reg.txt. Make sure you specify where your Ahk2Exe.exe
path is in the RunWait
command.
Add To Context Menu:
Label adds the Registry entry which runs Context Menu Create File.exe.
- Set the
contextMenu
variable to what needs to be displayed in the Context Menu. (The program
name is set to contextMenu
)
- Set the
regPath
to your desired Registry path.
- When it executes the
MsgBox
, check if the command is added to the Registry in the address bar.
CreateStartupShortcut:
Label creates the shortcut of the main program P4 Merge Compare.exe in Startup folder.
Add To Context Menu And Create Startup Shortcut.ahk
; =============Recommended Settings=============
#NoEnv
SetWorkingDir %A_ScriptDir%
#Warn
CoordMode, Mouse, Window
SendMode Input
#SingleInstance Force
SetTitleMatchMode 2
SetTitleMatchMode Fast
DetectHiddenWindows Off
DetectHiddenText On
#WinActivateForce
#NoTrayIcon
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1
#Persistent
#MaxThreadsPerHotkey 2
; =============Recommended Settings=============
AddToContextMenuAndCreateStartupShortcut:
RunAsAdmin: ; =============RunAsAdmin=============
If (!A_IsAdmin) ; IF NOT Admin
{
Run, *RunAs "%A_ScriptFullPath%" ; Run script as admin
ExitApp ; Exit the current instance running without admin privileges
}
ContextMenuCreateFile: ; =============ContextMenuCreateFile=============
contextMenuCreateFileAhk :=
(LTrim
"#NoEnv
#NoTrayIcon
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
SetBatchLines -1
ContextMenuCreateFile:
FileDelete, reg.txt
FileAppend, , reg.txt
ExitApp
Return"
) ; contextMenuCreateFileAhk
FileDelete, Context Menu Create File.exe ; DEL Context Menu Create File.exe
FileDelete, Context Menu Create File.ahk ; DEL Context Menu Create File.ahk
FileAppend, %contextMenuCreateFileAhk%, Context Menu Create File.ahk ; MAKE Context Menu Create File.ahk
RunWait, C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe /in "Context Menu Create File.ahk" /out "Context Menu Create File.exe" ; Convert AHK to EXE
FileDelete, Context Menu Create File.ahk ; DEL Context Menu Create File.ahk
AddToContextMenu: ; =============AddToContextMenu=============
path := "" ; path
program := "Context Menu Create File" ; program
contextMenu := "P4 Merge Compare" ; contextMenu
regPath := "HKCR\*\shell" ; regPath
StringReplace, regKey, contextMenu, %A_Space%, , A ; regKey
regKey := 0 regKey ; regKey
Loop, Files, %program%.exe, F ; Find Program.exe In Current Dir
{
path := A_LoopFileLongPath ; Set Program Path
}
cmd :=
(LTrim
"reg add """ regPath "\" regKey """ /ve /t REG_SZ /d """ contextMenu """ /f
reg add """ regPath "\" regKey "\command"" /ve /t REG_SZ /d ""\""" path "\""`"" /f"
) ; Registry
FileDelete, Add To Context Menu.bat ; CREATE Add To Context Menu.bat
FileAppend, %cmd%, Add To Context Menu.bat ; CREATE Add To Context Menu.bat
RunWait, Add To Context Menu.bat, , Hide ; RUN Add To Context Menu.bat (*RunAs ADMIN)
FileDelete, Add To Context Menu.bat ; DEL Add To Context Menu.bat
Run, regedit ; regedit
WinWait, Registry Editor ahk_class RegEdit_RegEdit ahk_exe regedit.exe ; Registry
Sleep, 333
ControlSetText, Edit1, %regPath%\%regKey%\command, Registry Editor ahk_class RegEdit_RegEdit ahk_exe regedit.exe ; regPath
ControlFocus, Edit1, Registry Editor ahk_class RegEdit_RegEdit ahk_exe regedit.exe ; regPath
ControlSend, Edit1, {Enter}, Registry Editor ahk_class RegEdit_RegEdit ahk_exe regedit.exe ; regPath
ControlSend, SysListView321, {Control Down}{NumpadAdd}{Control Up}, Registry Editor ahk_class RegEdit_RegEdit ahk_exe regedit.exe ; regPath
ControlSend, SysListView321, {F5}, Registry Editor ahk_class RegEdit_RegEdit ahk_exe regedit.exe ; regPath
MsgBox, 262192, CHECK, Check If Added %contextMenu% To Registry ; CHECK
CreateStartupShortcut: ; =============CreateStartupShortcut=============
path := "" ; path
program := contextMenu ; program
Loop, Files, %program%.exe, F ; Find Program.exe In Current Dir
{
path := A_LoopFileLongPath ; Set Program Path
}
FileCreateShortcut, %path%, %A_Startup%\%program%.lnk ; Create Startup Shortcut
Run, %A_Startup%, , Max ; Check If Shortcut Created
Run, "%program%.exe" ; Run Program
MsgBox, 262144, CHECK, Check If Shortcut Created ; CHECK
ExitApp ; ExitApp
Return
- Create 2nd program P4 Merge Compare.ahk which is the main program.
- Here a
Loop
is created and checks every 1 second if reg.txt exists.
IfExist, reg.txt
it kills the Context Menu Create File.exe and deletes the reg.txt.
- Then it activates explorer.exe window and copies all selected file paths to CLIPBOARD.
If CLIPBOARD contains .,\
to make sure CLIPBOARD contains path "\" with extension ".".
- The list of selected files is saved in
selectedFiles
variable.
- The Loop below
p4Params := ""
loops through selected files, gets the filePath
s.
- Then the
filePath
is concatenated to p4Params
.
StringTrimRight
removes the last space from p4Params
string.
- Then
Run, C:\Program Files\Perforce\p4merge.exe %p4Params%
is executed with %p4Params%
(list of selected files).
P4 Merge Compare.ahk
; =============Recommended Settings=============
#NoEnv
SetWorkingDir %A_ScriptDir%
#Warn
CoordMode, Mouse, Window
SendMode Input
#SingleInstance Force
SetTitleMatchMode 2
SetTitleMatchMode Fast
DetectHiddenWindows Off
DetectHiddenText On
#WinActivateForce
#NoTrayIcon
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1
#Persistent
#MaxThreadsPerHotkey 2
; =============Recommended Settings=============
P4MergeCompare:
Loop ; Loop Start
{
Sleep, 1000 ; Fix High CPU
IfExist, reg.txt ; IF reg.txt EXIST
{
RunWait, cmd /c taskkill /im "Context Menu Create File.exe" /f, , Hide ; Fix Opening 2 Compose Windows
FileDelete, reg.txt ; DEL reg.txt
WinActivate, ahk_class CabinetWClass ahk_exe explorer.exe ; Explorer
CLIPBOARD := "" ; Clear Clipboard
Send, {Control Down}{c}{Control Up} ; Copy File Paths
ClipWait, 0 ; Clip Wait
If CLIPBOARD contains .,\ ; IF CLIPBOARD contains .,\
{
selectedFiles := CLIPBOARD ; selectedFiles
p4Params := "" ; p4Params
Loop, Parse, selectedFiles, `n, `r ; Loop Start selectedFiles
{
filePath := """" A_LoopField """" ; filePath
p4Params .= filePath . " " ; p4Params .= %filePath%,
}
StringTrimRight, p4Params, p4Params, 1 ; Remove Last Space
Run, C:\Program Files\Perforce\p4merge.exe %p4Params% ; Open Files In P4Merge
}
}
}
Return
Convert both Add To Context Menu And Create Startup Shortcut.ahk and P4 Merge Compare.ahk to EXE files in the same directory using Ahk2Exe.exe --> (find in Start Menu, just browse file and hit convert)
Execute the Add To Context Menu And Create Startup Shortcut.exe
Select files, right click and the P4 Merge Compare context menu item should appear.