You want to use external editor/viewer when browsing archives in 7-Zip? No problem, just use the options and use your favorite editor. ;)

…Well, not so fast, the bad thing is you can only specify the editor’s executable file, but you can’t really pass any arguments to the it! Now, that’s a shame, because you may want to specify some arguments, which may actually be needed so 7-Zip works fine.

How does 7-Zip works when viewing/editing file through its interface without actually unpacking the archive? Here how it works:

  1. Unpack the file in temporary location (e.g. %TEMP%\7z<SOME_HASH>\<FILENAME>)
  2. Run the external editor by passing the aforementioned file location
  3. Wait for the process to finish
  4. Delete the temporary file

After reading through the described steps, there are some things we can conclude.

  1. Each viewed/edited file should be open in a separate process
  2. The process should exit after the file was viewed/edited

This kinda reminds me of the git and how it handles external editor. It may not be 1:1 match, but the analogy will suffice. :)

So basically, if you want to setup external editor in 7-Zip you need to wrap the start of the editor with the appropriate arguments in a separate executable. Of course, first thing I can think of is - use a BATCH script. This works of course, but the horrible side effect is that a command window is left open until you close the editor, that’s doesn’t really work for me, so we should came with another solution.

I’ve started by writing a simple .NET app, which does exactly that, but it came to me it’s a bit of overhead without any actual benefits. Being a bit rusty with my syntax knowledge of AutoHotkey, I decided it’s time to write yet another simple script, which solves a nagging problem. AutoHotkey has some weird syntax regarding expressions, assigning string literals, etc. It’s strange that you can just use string literals when invoking functions (e.g. MsgBox), without actually using any quotes (singles or doubles). Lexikos, the person who is working on v2 of AutoHotkey is about to change that, but v2 is still in early alpha stage, so I haven’t really tested it out.

As in my previous post, I will comment inline what everything does. ;)

; Instruct AutoHotkey to NOT show tray icon when this script is running
; Note that, the script may be running multiple times, so spamming the
; tray area won't be the nicest thing. :)
#NoTrayIcon

; This is like BATCH's variables, AutoHotkey writes the number of args
; it receives. %1% will be the first argument, %2% the second, etc
if (%0% = 0)
{
    ; 0x10 specifies "ERROR" in the message box
    ; Note the weird string literals not enclosed in any quotes... :/
    MsgBox, 0x10, File not specified!, You need to specify a valid file path...

    ; Return non-zero exit code in case of invalid args
    Exit, 1
}

; This is weird too, the rvalue is a string literal, without any enclosing
; quotes. This means you can just quotes when you need to wrap paths containing
; spaces. Note the -multiInst and -nosession parameters, these ensure that
; Notepad++ will be run in a new instance with no previous session!
nppCmd = "C:/Program Files (x86)/Notepad++/notepad++.exe" -multiInst -notabbar -nosession -noPlugin "%1%"

; Execute the command, set the error level and wait for it to finish
RunWait, %nppCmd%,, UseErrorLevel

; Return the command's exit code
Exit %A_LastError%

That’s all there is to it! You can setup the AHK script or you can compile the script and use it on a machine without AutoHotkey installed! It’s very easy to do that, you just save your script and then execute the following command:

"C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe" /in <AHK_SCRIPT> /out <COMPILED_AHK_SCRIPT>

That’s it! You can use this on different machines, extend the script so you can detect if you use 64-bit Notepad++ or use 32-bit OS. Use your own favorite editor, use different arguments or whatever you want. Implement reading the settings from the registry, from a file, the possibilities are endless, but this is a script that does the work for me, hope it helps you too! :)