AutoHotKey: Quick Access to Files and Folders

I’m a big fan of AutoHotKey a great scriptable automation tool for Windows and I use it for a number of things. One of these is to quickly access favourite files and folders.

When I started my latest job I found that I frequently needed to access folders that were deeply nested in file structures and also that these changed over time as the clients/projects I was working with changed. I also found that there were some files that I needed to open on an infrequent basis (e.g. ‘hosts’ file) that I also wanted to keep quickly available. Now whilst I have (and still do) make use of Portable Start Menu to access some of my most useful files and folders I figured I might be able to create something a little quicker to access and to modify using AutoHotKey.

The approach I took was to create a pop up list of files and folders from which I can open (or trigger) using a double click.

The first part of the script sets the hot key for the pop-up to be the Windows key and zero (but you can set this to whatever you require). Following this we create a dialog box and add a list to it. Note that the list has two columns - a description and a full path.

;WIN+0
#0::
Gui, Add, Text,, Pick a folder/file to open from the list below.
Gui, Add, ListView, vMyListBox gMyListBox w220 r14, Description|Full Path

Next we need to add some entries to the list. The two lines below show the addition of a file and a folder (respectively) to the list. Each entry takes four parameters. The first we just leave as an empty string. The second is the text we want to appear in our list. The third is the path to our item and the final entry is either FILE or FOLDER and is just something to use to tell the script what to do with the path (execute the default action (e.g. run/open) or open it in Windows explorer respectively).

LV_Add("","VBScript help","C:\Reference\VBS\VBS55.CHM","FILE")
LV_Add("","Screenshots","C:\Documents and Settings\sylumer\My Documents\screenshots\","FOLDER")

At this point what we’ve done is populate the list box with three columns of data. These correspond to the non-empty parameters specified in the LV_Add lines. We now set all three columns to resize to fit the longest text in each. Then we hide the second and third columns so we just have the text we want to show in our list.

;Auto resize the columns then show only the first one
LV_ModifyCol()
LV_ModifyCol(2, 0)
LV_ModifyCol(3, 0)

Once the list has been smartened up with resizing and hiding, we can display the pop-up window.

Gui, Show, , Quick Open...
return

As you might have realised, all we’ve done so far is build the interface; there’s no functionality built in yet. So now we’ll add a section to do just that.

QuickBox:
if A_GuiEvent <> DoubleClick
    return
GuiControlGet, QuickBox  ;Retrieve the ListBox's current selection.
LV_GetText(MyPath, A_EventInfo, 2)
LV_GetText(MyType, A_EventInfo, 3)
If MyType = FILE
{
     Run, %MyPath%, , UseErrorLevel
     if ErrorLevel = ERROR
          MsgBox Could not launch the specified file.  Perhaps it is not associated with anything?`nPATH:"%MyPath%"
}
Else
{
     Run, explorer /e`,%MyPath%,, UseErrorLevel
     if ErrorLevel = ERROR
          MsgBox Could not open the specified directory.`nPATH:"%MyPath%"
}
Gui, Destroy
return

So what does this section do? Well we look for a double click in the list box. We take the item that was double clicked and get the path and the type of entry. If the entry is a file then we try and “run” it (i.e. carry out the default action on the file - just like you were double clicking on the file in explorer). If the entry is for a folder then we open it in Windows explorer (and I’ve added a /e to give me a folder tree as I prefer things like that). Finally we automatically close the pop-up window.

Finally we’ll just add in the same code to close the window (or any other window we might be creating using AutoHotKey) on clicking the window’s close button or pressing the ESC key.

GuiClose:
GuiEscape:
Gui, Destroy
return

So there you have it. the full script is given below so you can just copy & paste it in one go. It’s fairly rough and ready so if you have any ideas for ways to tweak it or even if you just find it useful, why not leave a comment at the end of the post?

;WIN+0
#0::
Gui, Add, Text,, Pick a folder/file to open from the list below.
Gui, Add, ListView, vMyListBox gMyListBox w220 r14, Description|Full Path
LV_Add("","VBScript help","C:\Reference\VBS\VBS55.CHM","FILE")
LV_Add("","Screenshots","C:\Documents and Settings\sylumer\My Documents\screenshots\","FOLDER")

;Auto resize the columns then show only the first one
LV_ModifyCol()
LV_ModifyCol(2, 0)
LV_ModifyCol(3, 0)

Gui, Show, , Quick Open...
return


QuickBox:
if A_GuiEvent <> DoubleClick
    return
GuiControlGet, QuickBox  ;Retrieve the ListBox's current selection.
LV_GetText(MyPath, A_EventInfo, 2)
LV_GetText(MyType, A_EventInfo, 3)
If MyType = FILE
{
     Run, %MyPath%, , UseErrorLevel
     if ErrorLevel = ERROR
          MsgBox Could not launch the specified file.  Perhaps it is not associated with anything?`nPATH:"%MyPath%"
}
Else
{
     Run, explorer /e`,%MyPath%,, UseErrorLevel
     if ErrorLevel = ERROR
          MsgBox Could not open the specified directory.`nPATH:"%MyPath%"
}
Gui, Destroy
return


GuiClose:
GuiEscape:
Gui, Destroy
return
Author: Stephen Millard
Tags: | autohotkey |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read