home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / AutoHotkey104805_Install.exe / AutoHotkey.chm / docs / scripts / favoritefolders.ahk < prev    next >
Encoding:
Text File  |  2009-09-25  |  7.1 KB  |  187 lines

  1. ; Easy Access to Favorite Folders -- by Savage
  2. ; http://www.autohotkey.com
  3. ; When you click the middle mouse button while certain types of
  4. ; windows are active, this script displays a menu of your favorite
  5. ; folders.  Upon selecting a favorite, the script will instantly
  6. ; switch to that folder within the active window.  The following
  7. ; window types are supported: 1) Standard file-open or file-save
  8. ; dialogs; 2) Explorer windows; 3) Console (command prompt) windows.
  9. ; The menu can also be optionally shown for unsupported window
  10. ; types, in which case the chosen favorite will be opened as a new
  11. ; Explorer window.
  12.  
  13. ; Note: In Windows Explorer, if "View > Toolbars > Address Bar" is
  14. ; not enabled, the menu will not be shown if the hotkey chosen below
  15. ; has a tilde.  If it does have a tilde, the menu will be shown
  16. ; but the favorite will be opened in a new Explorer window rather
  17. ; than switching the active Explorer window to that folder.
  18.  
  19. ; CONFIG: CHOOSE YOUR HOTKEY
  20. ; If your mouse has more than 3 buttons, you could try using
  21. ; XButton1 (the 4th) or XButton2 (the 5th) instead of MButton.
  22. ; You could also use a modified mouse button (such as ^MButton) or
  23. ; a keyboard hotkey.  In the case of MButton, the tilde (~) prefix
  24. ; is used so that MButton's normal functionality is not lost when
  25. ; you click in other window types, such as a browser.  The presence
  26. ; of a tilde tells the script to avoid showing the menu for
  27. ; unsupported window types.  In other words, if there is no tilde,
  28. ; the hotkey will always display the menu; and upon selecting a
  29. ; favorite while an unsupported window type is active, a new
  30. ; Explorer window will be opened to display the contents of that
  31. ; folder.
  32. f_Hotkey = ~MButton
  33.  
  34. ; CONFIG: CHOOSE YOUR FAVORITES
  35. ; Update the special commented section below to list your favorite
  36. ; folders.  Specify the name of the menu item first, followed by a
  37. ; semicolon, followed by the name of the actual path of the favorite.
  38. ; Use a blank line to create a separator line.
  39.  
  40. /*
  41. ITEMS IN FAVORITES MENU <-- Do not change this string.
  42. Desktop      ; %A_Desktop%
  43. Favorites    ; %A_Desktop%\..\Favorites
  44. My Documents ; %A_MyDocuments%
  45.  
  46. Program Files; %A_ProgramFiles%
  47. */
  48.  
  49.  
  50. ; END OF CONFIGURATION SECTION
  51. ; Do not make changes below this point unless you want to change
  52. ; the basic functionality of the script.
  53.  
  54. #SingleInstance  ; Needed since the hotkey is dynamically created.
  55.  
  56. Hotkey, %f_Hotkey%, f_DisplayMenu
  57. StringLeft, f_HotkeyFirstChar, f_Hotkey, 1
  58. if f_HotkeyFirstChar = ~  ; Show menu only for certain window types.
  59.     f_AlwaysShowMenu = n
  60. else
  61.     f_AlwaysShowMenu = y
  62.  
  63. ; Used to reliably determine whether script is compiled:
  64. SplitPath, A_ScriptName,,, f_FileExt
  65. if f_FileExt = Exe  ; Read the menu items from an external file.
  66.     f_FavoritesFile = %A_ScriptDir%\Favorites.ini
  67. else  ; Read the menu items directly from this script file.
  68.     f_FavoritesFile = %A_ScriptFullPath%
  69.  
  70. ;----Read the configuration file.
  71. f_AtStartingPos = n
  72. f_MenuItemCount = 0
  73. Loop, Read, %f_FavoritesFile%
  74. {
  75.     if f_FileExt <> Exe
  76.     {
  77.         ; Since the menu items are being read directly from this
  78.         ; script, skip over all lines until the starting line is
  79.         ; arrived at.
  80.         if f_AtStartingPos = n
  81.         {
  82.             IfInString, A_LoopReadLine, ITEMS IN FAVORITES MENU
  83.                 f_AtStartingPos = y
  84.             continue  ; Start a new loop iteration.
  85.         }
  86.         ; Otherwise, the closing comment symbol marks the end of the list.
  87.         if A_LoopReadLine = */
  88.             break  ; terminate the loop
  89.     }
  90.     ; Menu separator lines must also be counted to be compatible
  91.     ; with A_ThisMenuItemPos:
  92.     f_MenuItemCount++
  93.     if A_LoopReadLine =  ; Blank indicates a separator line.
  94.         Menu, Favorites, Add
  95.     else
  96.     {
  97.         StringSplit, f_line, A_LoopReadLine, `;
  98.         f_line1 = %f_line1%  ; Trim leading and trailing spaces.
  99.         f_line2 = %f_line2%  ; Trim leading and trailing spaces.
  100.         ; Resolve any references to variables within either field, and
  101.         ; create a new array element containing the path of this favorite:
  102.         Transform, f_path%f_MenuItemCount%, deref, %f_line2%
  103.         Transform, f_line1, deref, %f_line1%
  104.         Menu, Favorites, Add, %f_line1%, f_OpenFavorite
  105.     }
  106. }
  107. return  ;----End of auto-execute section.
  108.  
  109.  
  110. ;----Open the selected favorite
  111. f_OpenFavorite:
  112. ; Fetch the array element that corresponds to the selected menu item:
  113. StringTrimLeft, f_path, f_path%A_ThisMenuItemPos%, 0
  114. if f_path =
  115.     return
  116. if f_class = #32770    ; It's a dialog.
  117. {
  118.     if f_Edit1Pos <>   ; And it has an Edit1 control.
  119.     {
  120.         ; Activate the window so that if the user is middle-clicking
  121.         ; outside the dialog, subsequent clicks will also work:
  122.         WinActivate ahk_id %f_window_id%
  123.         ; Retrieve any filename that might already be in the field so
  124.         ; that it can be restored after the switch to the new folder:
  125.         ControlGetText, f_text, Edit1, ahk_id %f_window_id%
  126.         ControlSetText, Edit1, %f_path%, ahk_id %f_window_id%
  127.         ControlSend, Edit1, {Enter}, ahk_id %f_window_id%
  128.         Sleep, 100  ; It needs extra time on some dialogs or in some cases.
  129.         ControlSetText, Edit1, %f_text%, ahk_id %f_window_id%
  130.         return
  131.     }
  132.     ; else fall through to the bottom of the subroutine to take standard action.
  133. }
  134. else if f_class in ExploreWClass,CabinetWClass  ; In Explorer, switch folders.
  135. {
  136.     if f_Edit1Pos <>   ; And it has an Edit1 control.
  137.     {
  138.         ControlSetText, Edit1, %f_path%, ahk_id %f_window_id%
  139.         ; Tekl reported the following: "If I want to change to Folder L:\folder
  140.         ; then the addressbar shows http://www.L:\folder.com. To solve this,
  141.         ; I added a {right} before {Enter}":
  142.         ControlSend, Edit1, {Right}{Enter}, ahk_id %f_window_id%
  143.         return
  144.     }
  145.     ; else fall through to the bottom of the subroutine to take standard action.
  146. }
  147. else if f_class = ConsoleWindowClass ; In a console window, CD to that directory
  148. {
  149.     WinActivate, ahk_id %f_window_id% ; Because sometimes the mclick deactivates it.
  150.     SetKeyDelay, 0  ; This will be in effect only for the duration of this thread.
  151.     IfInString, f_path, :  ; It contains a drive letter
  152.     {
  153.         StringLeft, f_path_drive, f_path, 1
  154.         Send %f_path_drive%:{enter}
  155.     }
  156.     Send, cd %f_path%{Enter}
  157.     return
  158. }
  159. ; Since the above didn't return, one of the following is true:
  160. ; 1) It's an unsupported window type but f_AlwaysShowMenu is y (yes).
  161. ; 2) It's a supported type but it lacks an Edit1 control to facilitate the custom
  162. ;    action, so instead do the default action below.
  163. Run, Explorer %f_path%  ; Might work on more systems without double quotes.
  164. return
  165.  
  166.  
  167. ;----Display the menu
  168. f_DisplayMenu:
  169. ; These first few variables are set here and used by f_OpenFavorite:
  170. WinGet, f_window_id, ID, A
  171. WinGetClass, f_class, ahk_id %f_window_id%
  172. if f_class in #32770,ExploreWClass,CabinetWClass  ; Dialog or Explorer.
  173.     ControlGetPos, f_Edit1Pos,,,, Edit1, ahk_id %f_window_id%
  174. if f_AlwaysShowMenu = n  ; The menu should be shown only selectively.
  175. {
  176.     if f_class in #32770,ExploreWClass,CabinetWClass  ; Dialog or Explorer.
  177.     {
  178.         if f_Edit1Pos =  ; The control doesn't exist, so don't display the menu
  179.             return
  180.     }
  181.     else if f_class <> ConsoleWindowClass
  182.         return ; Since it's some other window type, don't display menu.
  183. }
  184. ; Otherwise, the menu should be presented for this type of window:
  185. Menu, Favorites, show
  186. return
  187.