We help IT Professionals succeed at work.

Autohotkey: Open/edit file based on file extension

Hello experts,
I am looking for an AutoHotkey to easily open/edit document based on extension.
The idea is the following:
Send F10+Tab 3 if file has ahk extension and just send F10 +Tab 2 if file finishes with other extension.
Option 1 will allows me to open/edit file with my SciTE4AutoHotkey and option 2 to open/edit with Notepad++. If there is better way to do this, please let me know.
Option 1:
edit-ahk.pngOption2:
edit-notepad-plus-plus.pngThank you for your help.
Comment
Watch Question

Duncan RoeSoftware Developer
CERTIFIED EXPERT

Commented:
Under Linux, you no longer need to depend on extensions. Use the file command to find out what the file contains and proceed accordingly
Luis DiazIT consultant

Author

Commented:
Hello Duncan,
I am using W10.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
If there is better way to do this, please let me know.
Hi Luis,
I can think of two better ways, but a few questions first:

(1) What file extension(s) do you want opened with Notepad++?
(2) What file extension(s) do you want opened with SciTE4AutoHotkey?
(3) Where is the location of your NPP executable? For example, mine is:
     C:\Program Files\Notepad++\notepad++.exe
(4) Where is the location of your SciTE4AutoHotkey executable? For example, mine is:
     C:\Program Files\AutoHotkey\SciTE\SciTE.exe

Regards, Joe
Developer
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018
Commented:
Hi Luis,
I haven't heard back from you on my questions above, so decided to go with these answers, as I'd like to wrap up this one before calling it quits tonight:

(1) Anything except AHK
(2) AHK
(3) C:\Program Files\Notepad++\notepad++.exe
(4) C:\Program Files\AutoHotkey\SciTE\SciTE.exe

I presume that you'll be able to fix the script below yourself if any of those is wrong, but if not, let me know and I'll help you through it.

Here's an AutoHotkey script that does what you want:

; Joe Winograd 9-Sep-2019
#NoEnv ; avoid checking empty variables to see if they are environment variables
#SingleInstance Force ; replace old instance immediately
SetBatchLines,-1 ; run at maximum speed

; *** begin variables to change
NPPexe:="C:\Program Files\Notepad++\notepad++.exe"
SciTE4exe:="C:\Program Files\AutoHotkey\SciTE\SciTE.exe"
; *** end variables to change

!^F12:: ; Alt+Ctrl+F12 - as always, make hotkey whatever you want
FileSelected:=GetSelectedFile()
SplitPath,FileSelected,,,FileExt
If (FileExt="ahk")
  Run,%SciTE4exe% %FileSelected%
Else
  Run,%NPPexe% %FileSelected%
Return

GetSelectedFile(hwnd="") {
; this function by sinkfaze: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=77#p395
  WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
  WinGetClass class, ahk_id %hwnd%
  if  (process = "explorer.exe")
    if  (class ~= "Progman|WorkerW") {
      ControlGet, files, List, Selected Col1, SysListView321, ahk_class %class%
      Loop, Parse, files, `n, `r
        ToReturn .= A_Desktop "\" A_LoopField "`n"
    }
    else  if (class ~= "(Cabinet|Explore)WClass") {
      for window in ComObjCreate("Shell.Application").Windows
        if  (window.hwnd==hwnd)
          sel :=  window.Document.SelectedItems
      for item in sel
        ToReturn .= item.path "`n"
    }
    return  Trim(ToReturn,"`n")
}

Open in new window

I always like to give credit where credit is due, so I want to state that I did not write or even modify the GetSelectedFile function (all I did was rename it from Explorer_GetSelection). That was written by sinkfaze and posted  here. My script calls that function to get the file that is selected in Windows/File Explorer, then simply runs SciTE on that file if the file extension is AHK or runs NPP if it is not AHK...no error checking is done. Regards, Joe
Luis DiazIT consultant

Author

Commented:
Joe,
You took the right decision as the need was to open with notepad++ the various files which have an extension different that .ahk
I tested the proposal and it works here.
Just a little strange behavior that I don't understand:
I have the following script placed above your proposal which allows me to open downloads folder:

#^d::Run explorer  %USERPROFILE%\Downloads
Return
 

Open in new window


When I run the proposal without commenting the following line:
#NoEnv ; avoid checking empty variables to see if they are environment variables

Open in new window

and I try to open download folder I got Document folder.
I comment the previous line and I retested the script which allows me to open download folder and it works.
2 questions came across:
1-Can I comment #NoEnv or your recommendation is keep it as is?
2-If so how should I proceed to avoid regression related to:
#^d::Run explorer  %USERPROFILE%\Downloads
Return
 

Open in new window

Knowing that everything should be in the same file.
Thank you for your help.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018
Commented:
As I mentioned at a previous question, userprofile is an environment variable. There is not a built-in A_ variable for it, and %userprofile% does not have it. You need to get it via EnvGet (depending on what environment variable you want) and/or build it up with the various A_ built-in variables.

So, to your questions:

> 1-Can I comment #NoEnv or your recommendation is keep it as is?

My recommendation is to keep it as is. You may learn about it here:
https://www.autohotkey.com/docs/commands/_NoEnv.htm

> 2-If so how should I proceed to avoid regression related to

You should change your AutoHotkey code to this:

#^d::
DownloadsFolder:="c:\users\" . A_UserName . "\Downloads"
Run,explorer.exe %DownloadsFolder%
Return

Open in new window

Regards, Joe
Luis DiazIT consultant

Author

Commented:
Thank you very much Joe.
I tested your proposal and it works!
I am trying to understand the NoEnv doc but I cannot find a logical approach about the fact that I have script such as:

#^m::Run explorer  %USERPROFILE%\Documents
Return

Open in new window

and I can run it with:
#NoEnv

Open in new window

Thank you in advance for your help.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Sorry, Luis, my mistake on that! With #NoEnv in effect, it will not check empty variables to see if they are environment variables. For example, userprofile will be an empty variable and it will not check to see if it is an environment variable. Of course, it is! Therefore, if #NoEnv is NOT in effect, it will, indeed, find the environment variable userprofile. You can show this easily with these two AutoHotkey scripts:

#NoEnv
msgbox % userprofile

Open in new window

That one will display an empty MsgBox.

msgbox % userprofile

Open in new window

That one will display the value of the userprofile environment variable.

All of that said. it is still a good idea to use #NoEnv and, in fact, it is the default in AutoHotkey V2. It is important to note that it is NOT a positional directive, that is, having it anywhere in a script prevents empty variables from being checked as potential environment variables.

As I mentioned earlier, when #NoEnv is in effect, you should use EnvGet and/or the built-in A_ variables. For example:

EnvGet,Profile,UserProfile
msgbox % profile

Open in new window

Regards, Joe
Luis DiazIT consultant

Author

Commented:
Thank you very much Joe for this update.
I had some issues with the following:
It works in one computer but not in my other computer :-(

DownloadsFolder:="c:\users\" . A_UserName . "\Downloads"
Run,explorer.exe %DownloadsFolder%
Return

Open in new window

As a result I proceed like this:

#^d::
EnvGet, USERPROFILE, USERPROFILE
Run,explorer.exe %USERPROFILE%\Downloads
Return

Open in new window


and it works in both computers.
One question came across:
I have in a single file with the following AutoHotkeys:

;====================================
;Open UserProfile folder
;====================================

#^l::
EnvGet, USERPROFILE, USERPROFILE
Run explorer %USERPROFILE%
Return

;====================================
; Open Documents folder
;====================================

#^m::
EnvGet, USERPROFILE, USERPROFILE
Run explorer %USERPROFILE%\Documents
Return

;====================================
; Open Downloads folder
;====================================

#^d::
EnvGet, USERPROFILE, USERPROFILE
Run,explorer.exe %USERPROFILE%\Downloads
Return

Open in new window


and in the same file your solution:
Your solution
with #NoEnv option

I am having some doubts and I would like to have your advice on the following:
It makes sense to declare for every AutoHotkey which use USERPROFILE:
1-EnvGet, USERPROFILE, USERPROFILE or just one time is enough?
2-How you declare the USERPROFILE variable:
EnvGet, USERPROFILE, USERPROFILE  or EnvGet,Profile,UserProfile
Thank you for your help.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
Hi Luis,
Yes, EnvGet for the UserProfile environment variable is the correct way to do it (better than building it up with "c:\users\" and A_UserName).

> 1-EnvGet, USERPROFILE, USERPROFILE or just one time is enough?

You may do it just once at the top of your AutoHotkey file that has all of your hotkeys/hotstrings in it. This works because the UserProfile doesn't change in a Windows session.

> 2-How you declare the USERPROFILE variable:
> EnvGet, USERPROFILE, USERPROFILE
> EnvGet,Profile,UserProfile

This is a matter of personal taste...coding style. I don't like the name of a local variable to be the same as an environment variable, but there's no reason why it can't be, as long as #NoEnv is in effect so that there's no confusion between local variables and environment variables (of course, the upper/lower case in variable names doesn't matter...that also comes down to personal taste/coding style...and code readability). Regards, Joe
Luis DiazIT consultant

Author

Commented:
Noted, thank you very much for your feedback.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
You're very welcome!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.