Okay, new version. I took rajat's idea and ran with it, enabling the menu in a number of different locations. Here's the breakdown:
Normal: Opens a new explorer window at that folder.
Explorer Window: Switches to that folder.
Console Window: cds to that folder.
File Dialog: As before.
I couldn't think of any more off the top of my head. I'm going to modify it for my own personal usage to work in individual lister panes in Directory Opus if anyone's interested.
I changed the class variable to f_class to fit with the convention of the rest of the script, and I added a few comments.
I think this general idea could be used for a number of things, such as a web favorites menu that works across browsers and even outside of them, or one of frequently edited files that works across editors. They could even be combined easily just by adding the menus together as submenus after they're built.
; Update the special commented section below to list your menu items.
; Specify the name of the menu item first, followed by a semicolon,
; followed by the name of the actual favorite path or file. Use a
; blank line to create a separator line.
/*
ITEMS IN FAVORITES MENU (do not change the contents of this line)
Desktop ; %UserProfile%\Desktop
Favorites ; %UserProfile%\Favorites
My Documents ; %UserProfile%\My Documents
Program Files; %ProgramFiles%
*/
; Do not make changes below this point unless you want to change
; the basic functionality of the script.
; Used to reliably determine whether script is compiled:
SplitPath, A_ScriptName,,, f_FileExt
if f_FileExt = Exe ; Read the menu items from an external file.
f_FavoritesFile = %A_ScriptDir%\Favorites.ini
else ; Read the menu items directly from this script file.
f_FavoritesFile = %A_ScriptFullPath%
;----Read the configuration file.
f_AtStartingPos = n
f_MenuItemCount = 0
Loop, Read, %f_FavoritesFile%
{
if f_FileExt <> Exe
{
; Since the menu items are being read directly from this
; script, skip over all lines until the starting line is
; arrived at.
if f_AtStartingPos = n
{
IfInString, A_LoopReadLine, ITEMS IN FAVORITES MENU (do
f_AtStartingPos = y
continue ; Start a new loop iteration.
}
else ; The closing comment symbol marks the end of the list.
{
if A_LoopReadLine = */
break ; terminate the loop
}
}
; Menu separator lines must also be counted to be compatible
; with A_ThisMenuItemPos:
f_MenuItemCount++
if A_LoopReadLine = ; Blank indicates a separator line.
Menu, Favorites, Add
else
{
StringSplit, f_line, A_LoopReadLine, `;
Transform, f_path%f_MenuItemCount%, deref, %f_line2%
Transform, f_line1, deref, %f_line1%
Menu, Favorites, Add, %f_line1%, f_OpenFavorite
}
}
return ;----End of auto-execute section.
;----Open the selected favorite
f_OpenFavorite:
StringTrimLeft, f_path, f_path%A_ThisMenuItemPos%, 0
if f_path =
return
if f_class = #32770
{
; If over a standard file open/save dialog, jump to that folder.
; Activate the window so that if the user is middle-clicking
; outside the dialog, subsequent clicks will also work:
WinActivate ahk_id %f_window_id%
ControlGetText, f_text, Edit1, ahk_id %f_window_id%
ControlSetText, Edit1, %f_path%, ahk_id %f_window_id%
ControlSend, Edit1, {Enter}, ahk_id %f_window_id%
Sleep, 100 ; It needs extra time on some dialogs or in some cases.
ControlSetText, Edit1, %f_text%, ahk_id %f_window_id%
}
else
{
if f_class = ConsoleWindowClass
{
; If over a console window, cd to that directory.
ControlSend, ahk_parent, cd %f_path%{Enter}, ahk_id %f_window_id%
}
else
{
if f_class = CabinetWClass
{
; If over an explorer window, switch folders.
ControlSetText, Edit1, %f_path%, ahk_id %f_window_id%
ControlSend, Edit1, {Enter}, ahk_id %f_window_id%
}
else
{
; If over nothing in particular, open up an explorer window.
Run, explorer %f_path%
}
}
}
return
^+P::Run, explorer.exe C:\documents and settings\ken allen\my documents
;----Display the menu
~MButton::
WinGet, f_window_id, ID, A
WinGetClass, f_class, ahk_id %f_window_id%
Menu, Favorites, show
return
Edit: Oops, left in some debug code
. Removed.