Search
« Use AutoHotKey to create a daily folder | Main | Quick Copy Evernote Note Links »
Thursday
Aug302012

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

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (2)

Hi Stephen,

thanks a lot for the cool script. improved my productivity by a good 50%.
I was wondering if you would know how to change the script so that it doesn't capture double click to open or run a folder or a file, but instead, it would be great to simply use the return (enter) key?
I'm trying to get rid of my mouse as much as possible.

thanks in advance.
Ben

March 25, 2013 | Unregistered Commenterben

Hi Ben.

Great suggestion and I'm no racking my brain trying to figure out what on earth possessed me not to do that in the first place?!

So I've created an updated script that I know works in my full script - I'm pretty sure I've translated it to the equivalent test script as set out above and modified in the file linked to below.

The new script selects the top item in the list by default (so you can cursor key down the list to your desired selection). A hidden button has been created as a default - hitting enter or double clicking an item will now trigger a call to a sub routine to process the selection.

· fav_ver2.ahk.txt.

Hopefully I have got the script right (I would have given it a proper test had my Windows laptop battery not just given out and I've now switched to my Mac), but if you get any issues, let me know and I'll do my best to sort things out.

Regards,

Stephen.

March 31, 2013 | Registered CommenterStephen Millard

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>