AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

File Open Favorites
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
savage



Joined: 02 Jul 2004
Posts: 206

PostPosted: Mon Aug 16, 2004 2:47 am    Post subject: File Open Favorites Reply with quote

Do you have a bunch of folders that you're frequently opening or saving things from? The XP file dialogs have My Documents, My Computer, etc, but they're not usually the ones you want. KDE has a sort of universal favorites feature. This script is my attempt to duplicate that effect. It builds a menu of names and paths from favorites.ini (stored in the same directory as the script) the name and the path are seperated by a semicolon like so:
Code:

-------- ; SEP
command ; C:\command
My Music ; C:\Documents and Settings\Ken Allen\My Documents\My Music


A seperator can be inserted by replacing the path with SEP.

Now, whenever you are in a standard xp file dialog, you can middle click and a menu you will appear. Pick an item and the dialog will jump to that folder - the current file name is preserved. You can also put a full filename as the path and the dialog will automagically open or save to that filename. There's no warning for this though, so I'm not sure I'd recommend it Wink.

If it seems a little cludgy, it's because I had to struggle to figure out a way to simulate getting the contents of a variable designated in another variable.

I imagine it wouldn't be very difficult to make it work in other dialogs or older versions of windows, just check the control and window class in the spy and add it in Smile.

Code:

;----Build The Menu (Autoexecute)
Loop, Read, %a_scriptdir%\favorites.ini
{
   StringSplit, line, A_LoopReadLine, `;
   paths%a_index% = %line2%
   IfNotEqual, line2, SEP
   {
      Menu, Favorites, Add, %a_index% %line1%, OpenFavorite   
   }
   else
   {
    Menu, Favorites, Add, %line1%, OpenFavorite
   }
}

;----Open the favorite
OpenFavorite:
      StringSplit, item, A_ThisMenuItem, %A_Space%
      StringTrimLeft, path, paths%item1%, 0
      IfNotEqual, path, SEP
      {
         ControlGetText, text, Edit1, ahk_id %win%
         ControlSetText, Edit1, %path%, ahk_id %win%
         ControlSend, Edit1, {Enter}, ahk_id %win%
         ControlSetText, Edit1, %text%, ahk_id %win%
      }
return

;----Raise the menu
~Mbutton::
   MouseGetPos, x, y, win
   WinGetClass, class, ahk_id %win%
   IfEqual, class, #32770
   {
      Menu, Favorites, show
   }
return


As usual, suggestions, complaints, and cupcakes are welcome!

Chris, the more I work with AHK, the more I think it's the single most useful program for windows!

PS Do you think I should fix this up for the showcase?
Back to top
View user's profile Send private message AIM Address
Rajat



Joined: 28 Mar 2004
Posts: 1717

PostPosted: Mon Aug 16, 2004 10:29 am    Post subject: Reply with quote

i found it nice! Smile

i think i'll modify it to not to show numbers.
_________________
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10464

PostPosted: Mon Aug 16, 2004 3:44 pm    Post subject: Reply with quote

It's a beautiful script, and a great enhancement to the OS, especially for apps that display the old style of file-save/open dialogs (the ones that lack a favorites toolbar).

Per Rajat's suggestion, I've made a change to the script to allow the number of each item to be omitted from the menu. However, this requires that you download the latest updated installer, since it uses the new built-in variable A_ThisMenuItemPos.

I also made a few other minor changes in preparation for putting it on the showcase after v1.0.18 is released:

1) It uses the active window rather than the one the mouse is hovering over. Although this seems like an improvement (since it allows the user to bring up the menu when clicking outside of the dialog), I'll change it back if you think the other way has some advantage I didn't notice.

2) It uses a self-contained configuration section when running as a non-compiled script (thanks to Rajat for the idea). Otherwise, it uses the same external INI file as before.

3) It uses blank lines in the configuration to create true menu separator lines.

4) The config section can optionally refer to environment variables or script variables.

Code:
; 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
; 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%
return


;----Display the menu
~Mbutton::
WinGet, f_window_id, ID, A
WinGetClass, class, ahk_id %f_window_id%
if class = #32770
   Menu, Favorites, show
return


Savage, If you would like to be credited as someone other than "savage", let me know. Also, since it's your script you can certainly feel free to make further changes to it for inclusion in the published version.

Edit: removed test line "FileSelectFile", fixed typo, added "Sleep 100" to make it more reliable.


Last edited by Chris on Mon Aug 16, 2004 10:22 pm; edited 3 times in total
Back to top
View user's profile Send private message Send e-mail
Rajat



Joined: 28 Mar 2004
Posts: 1717

PostPosted: Mon Aug 16, 2004 4:50 pm    Post subject: Reply with quote

aaaargh! just when i bonked my head and found a way to remove those menu nos. u make it so eazy!! Mad

nice addition! Smile

Code:

;----Build The Menu (Autoexecute)

AutoTrim, Off

Loop, Read, %a_scriptdir%\favorites.ini
{
   IfInString, A_LoopReadLine, `;
   {
      StringSplit, line, A_LoopReadLine, `;

      paths%a_index% = %line2%
      M%a_index% = %line1%

      Menu, Favorites, Add, %line1%, OpenFavorite   
   }

   Menu, Favorites, Add

   IfInString, A_LoopReadLine, $


}

Exit


;----Open the favorite
OpenFavorite:
      ;StringSplit, item, A_ThisMenuItem, %A_Space%
      Loop
      {
         IfEqual, M%a_index%,, Break
         IfEqual, M%a_index%, %A_ThisMenuItem%, StringTrimLeft, path, paths%a_index%, 0
         IfEqual, M%a_index%, %A_ThisMenuItem%, Break
      }
     
      IfNotEqual, path, SEP
      {
         ControlGetText, text, Edit1, ahk_id %win%
         ControlSetText, Edit1, %path%, ahk_id %win%
         ControlSend, Edit1, {Enter}, ahk_id %win%
         ControlSetText, Edit1, %text%, ahk_id %win%
      }
return

;----Raise the menu
~Mbutton::
   MouseGetPos, x, y, win
   WinGetClass, class, ahk_id %win%
   IfEqual, class, #32770, Menu, Favorites, show
return

_________________
Back to top
View user's profile Send private message
savage



Joined: 02 Jul 2004
Posts: 206

PostPosted: Mon Aug 16, 2004 5:15 pm    Post subject: Reply with quote

LOL, that new variable certainly makes it easier! Thanks chris, that certainly takes most of the cludgery out.
Back to top
View user's profile Send private message AIM Address
Rajat



Joined: 28 Mar 2004
Posts: 1717

PostPosted: Mon Aug 16, 2004 7:39 pm    Post subject: Reply with quote

i've made my own variant of this adding things that i'd been thinking of doing for a long time. i'd an old script without menu cmd that i'd been thinking of updating.

now my menu shows a list of folders and if the window under mouse is a file open window then it opens the selected folder there and if not then simply opens the selected folder. also a submenu contains my most frequently used commands, ofcourse with friendly names on menu.
_________________
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10464

PostPosted: Mon Aug 16, 2004 8:17 pm    Post subject: Reply with quote

Sounds nice. This favorites script is also something I'm going to try to use, further overloading an already stressed out MButton.
Back to top
View user's profile Send private message Send e-mail
savage



Joined: 02 Jul 2004
Posts: 206

PostPosted: Mon Aug 16, 2004 10:37 pm    Post subject: Reply with quote

You should get a five button mouse. You'll never go back.

That's sweet idea Rajat, methinks this menu is going on one of my side buttons now.

I noticed you added a prefix to all of the variables in your version (which I'm using now, btw), are you planning on putting it in the script showcase? That would be nice, I think this is a good one for it.
Back to top
View user's profile Send private message AIM Address
Rajat



Joined: 28 Mar 2004
Posts: 1717

PostPosted: Mon Aug 16, 2004 10:58 pm    Post subject: Reply with quote

should i post my script here?
_________________
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10464

PostPosted: Tue Aug 17, 2004 1:58 am    Post subject: Reply with quote

Quote:
are you planning on putting it in the script showcase? That would be nice, I think this is a good one for it.

Yes, but probably after v1.0.18 is released, since the script requires that new built-in variable.

I mentioned a few other things above that you might have missed due to the post being longish.
Back to top
View user's profile Send private message Send e-mail
savage



Joined: 02 Jul 2004
Posts: 206

PostPosted: Tue Aug 17, 2004 4:14 am    Post subject: Reply with quote

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.

Code:
; 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 Razz . Removed.
Back to top
View user's profile Send private message AIM Address
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10464

PostPosted: Tue Aug 17, 2004 12:15 pm    Post subject: Reply with quote

Nice enhancement. In the final version, I will have a few other changes to the above:

Handles ExploreWClass (double pane Explorer) the same way as CabinetWClass (single pane view).

Has double quotes around this parameter (might be safer?): Run, Explorer "%f_path%"

A note that you must have View > Toolbars > Address Bar enabled in Windows Explorer for the Favorites menu to work correctly there. Better yet, maybe it should be enhanced to fall back to the default behavior of opening a new window if the Edit1 control doesn't exist.

It could probably use some further enhancement due to the fact that #32770 is the class name of all Windows dialogs -- and perhaps all modal dialogs in general. For example, if there is no Edit1 control in a #32770 window, it probably isn't a file-open/save dialog and the script should probably default to opening a new Explorer window.
Back to top
View user's profile Send private message Send e-mail
savage



Joined: 02 Jul 2004
Posts: 206

PostPosted: Tue Aug 17, 2004 1:58 pm    Post subject: Reply with quote

I didn't realize that a double pane explorer was another class or I would have added that myself.

I also didn't realize that that was the class for all dialogs :p.

For some reason, putting quotes around the path doesn't seem to work for me, it always opens up c:\, but when I take the quotes off it works. Maybe it's just me.

How do you check to see if a control exists?
Back to top
View user's profile Send private message AIM Address
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10464

PostPosted: Tue Aug 17, 2004 2:47 pm    Post subject: Reply with quote

Quote:
I didn't realize that a double pane explorer was another class or I would have added that myself.

I just happened to notice it was that way on XP. It might vary depending on OS type.

Quote:
For some reason, putting quotes around the path doesn't seem to work for me, it always opens up c:\, but when I take the quotes off it works. Maybe it's just me.

It works on mine, but to be safer I restored it to the way you had it.

Quote:
How do you check to see if a control exists?

Probably the best way is with ControlGetPos:
Code:
ControlGetPos, x,,,, Edit1, A
if x =
     MsgBox The control does not exist.
Back to top
View user's profile Send private message Send e-mail
Rajat



Joined: 28 Mar 2004
Posts: 1717

PostPosted: Tue Aug 17, 2004 4:22 pm    Post subject: Reply with quote

Quote:
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.

boy! now it has a shitload of goodies : )
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group