AutoHotkey Community

It is currently May 27th, 2012, 12:20 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Folder Menu Function
PostPosted: May 27th, 2005, 2:35 am 
Offline

Joined: July 2nd, 2004, 11:53 pm
Posts: 207
This here's a function for building menus from paths. I know there's a script on the forum already, but this works better as a function.

Code:
FolderMenu(path, menu)
{   
    global
    local f_FolderList, nextmenu, f_FileList, f_LenFileName, f_MenuItemName, varname, MenuName
    If menu =
   MenuName := String2Hex(path)
    else
   MenuName := menu
    Loop, %path%\*, 2
   f_FolderList = %f_FolderList%`n%A_LoopFileFullPath%
    Sort, f_FolderList
    Loop, parse, f_FolderList, `n
    {
   If A_LoopField =
       continue
   f_MenuItemName := FileShortName(A_LoopField)
   nextmenu := FolderMenu(A_LoopField,"")
   varname := MenuName . "path" . String2Hex(f_MenuItemName)
   %varname% = %A_LoopField%
   Menu, %MenuName%, add, %f_MenuItemName%, :%nextmenu%
    }
    Loop, %path%\*, 0
   f_FileList = %f_FileList%`n%A_LoopFileFullPath%
    Sort, f_FileList
    Loop, parse, f_FileList, `n
    {
   If A_LoopField =
       continue
   f_MenuItemName := FileShortName(A_LoopField)
   varname := MenuName . "path" . String2Hex(f_MenuItemName)
   %varname% = %A_LoopField%
   Menu, %MenuName%, add, %f_MenuItemName%, FolderMenuHandler
    }
    return MenuName
}

FolderMenuHandler:
    If menu_RButton = true
    {
   Msgbox, Right Click
   menu_RButton = false
    }
    varname := A_ThisMenu . "path" . String2Hex(A_ThisMenuItem)
    cmd := %varname%
    If cmd =
   return
    else
   Run, %cmd%
return

FileShortName(path)
{
    StringSplit, tokens, path, \/
    last := tokens%tokens0%
    StringSplit, half, last, .
    If half1 =
   return last
    else
   return half1
}

String2Hex(x)                 ; Convert a string to a huge hex number starting with X
{
   StringLen Len, x
   format = %A_FormatInteger%
   SetFormat Integer, H
   hex = X
   Loop %Len%
   {
      Transform y, ASC, %x%   ; ASCII code of 1st char, 15 < y < 256
      StringTrimLeft y, y, 2  ; Remove leading 0x
      hex = %hex%%y%
      StringTrimLeft x, x, 1  ; Remove 1st char
   }
   SetFormat Integer, %format%
   Return hex
}


I snagged the hex function from the list library by Laszlo Hars.

This function makes it really easy to insert paths onto your existing menus.
It recurses all subdirectories as well.
The first parameter is the path to build the menu from.
The second parameter can be the name of a menu to add the contents of the path to. This parameter can be blank, in which case the function generates a name. In all cases the function returns the menu name it used.

Here's an example of how to use it:

Code:
FolderMenu("C:\command\shortcuts\programs","TopMenu")
Menu, TopMenu, add, Opus, menu_opus
name := FolderMenu("C:\command\docs","")
Menu, TopMenu, add, Docs, :%name%


This produces a menu that looks like this:
Code:
Games >
Media >
Net >
Prog >
Tools >
Opus
Docs >


As you can see, the contents of the program directory (all folders) are added right on the menu, followed by a normal menu item, followed by the contents of the docs directory in a submenu. Very simple to use, yes? Selecting one of the items on the file menus will run it.

I'm currently trying to make right click open the context menu, but I'm not even going to bother with the c code I need for that until I get the right click part working, and I'm not sure ahk can manage it right now.

_________________
<enormous animated gif>


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2005, 2:02 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Thanks for posting it. It's good to have a self-contained menu-builder ready to plug in to existing scripts.

savage wrote:
I'm currently trying to make right click open the context menu, but I'm not even going to bother with the c code I need for that until I get the right click part working, and I'm not sure ahk can manage it right now.
Some improvements for GUI context menus are planned for the future. It will probably be either built-in context menus or the ability to define a GuiClick subroutine that catches all clicks done inside the GUI window.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2005, 5:13 pm 
Offline

Joined: July 2nd, 2004, 11:53 pm
Posts: 207
By context menu, I meant the explorer context menu for the file. I've got some code for showing it I pulled from a litestep module. It seems to be totally self contained and independent code, so it should be a simple matter to throw into a dll.

_________________
<enormous animated gif>


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 28th, 2005, 1:15 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
That sounds great. I've often wanted a better, faster Search Companion window that still has the full Explorer context menu.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 28th, 2005, 5:38 pm 
Offline

Joined: September 2nd, 2004, 1:08 am
Posts: 124
Location: Sunnyvale
Thanks very much for this savage! It was easy for me to replace my popup-menu script with your better functions.

Here is my script with my minor addition of file extensions filtering. This script will pop-up a menu where ever the mouse cursor is. Note that I use XButton2 to popup the menu in this example, but for a regular mouse, I would use LAlt & RButton::.

Code:
; List only files with these extensions
myext = .lnk,.exe,.ahk,.cmd,.bat

; Use some other tray icon for this
;Menu, Tray, Icon, %A_WinDir%\system32\Shell32.dll, 174

; Folders with or without sub-directories containing programs
FolderMenu("C:\toolbar\tlb\tbars\QL","tray")
name := FolderMenu("C:\cmdfiles\pstools","")
Menu, tray, add, Pstools, :%name%
return

XButton2::
menu, tray, show
return

FolderMenu(path, menu)
{   
    global
    local f_FolderList, nextmenu, f_FileList, f_LenFileName, f_MenuItemName, varname, MenuName
    If menu =
       MenuName := String2Hex(path)
    else
       MenuName := menu

    Loop, %path%\*, 2
       f_FolderList = %f_FolderList%`n%A_LoopFileFullPath%
    Sort, f_FolderList
    Loop, parse, f_FolderList, `n
    {
        If A_LoopField =
           continue
        f_MenuItemName := FileShortName(A_LoopField)
        nextmenu := FolderMenu(A_LoopField,"")
        varname := MenuName . "path" . String2Hex(f_MenuItemName)
        %varname% = %A_LoopField%
        Menu, %MenuName%, add, %f_MenuItemName%, :%nextmenu%
    }
    Loop, %path%\*, 0
       f_FileList = %f_FileList%`n%A_LoopFileFullPath%
   
    Sort, f_FileList
    Loop, parse, f_FileList, `n
    {
       If A_LoopField =
          continue
       
       Filename=%A_LoopField%

       If Filename contains %myext%
       {

          f_MenuItemName := FileShortName(A_LoopField)
          varname := MenuName . "path" . String2Hex(f_MenuItemName)
          %varname% = %A_LoopField%
          Menu, %MenuName%, add, %f_MenuItemName%, FolderMenuHandler
       }
    }
    return MenuName
}

FolderMenuHandler:
    If menu_RButton = true
    {
       Msgbox, Right Click
       menu_RButton = false
    }
   
    varname := A_ThisMenu . "path" . String2Hex(A_ThisMenuItem)
    cmd := %varname%
    If cmd =
       return
    else
       Run, %cmd%
return

FileShortName(path)
{
    StringSplit, tokens, path, \/
    last := tokens%tokens0%
    StringSplit, half, last, .
    If half1 =
       return last
    else
       return half1
}

String2Hex(x)                 ; Convert a string to a huge hex number starting with X
{
   StringLen Len, x
   format = %A_FormatInteger%
   SetFormat Integer, H
   hex = X
   Loop %Len%
   {
      Transform y, ASC, %x%   ; ASCII code of 1st char, 15 < y < 256
      StringTrimLeft y, y, 2  ; Remove leading 0x
      hex = %hex%%y%
      StringTrimLeft x, x, 1  ; Remove 1st char
   }
   SetFormat Integer, %format%
   Return hex
}


_________________
I am he of whom he speaks!


Report this post
Top
 Profile  
Reply with quote  
 Post subject: string to hex: why?
PostPosted: June 4th, 2006, 1:47 pm 
I am learning ahk by studying some of your scripts. I hope you don't mind my asking a question.

What is purpose of converting string to hex in your scripts for foldermenu?

Thanks, Sri


Report this post
Top
  
Reply with quote  
 Post subject: Re: string to hex: why?
PostPosted: June 4th, 2006, 6:01 pm 
Offline

Joined: March 28th, 2004, 3:53 pm
Posts: 1870
its probably used to turn all special characters into 0-9-A-F... so that they don't error -out the script. i've used that in some scripts so i know this is a good idea.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2006, 6:50 am 
I take the script of Atomhrt Sat May 28, 2005 5:38 pm and as auto-exec part:
Code:
FolderMenu("C:\","tray")
name := FolderMenu("C:\tmp","")
Menu, tray, add, tmp, :%name%
return

This gives
Quote:
Error: Submenu does not exist. The current thread will exit.
Am I doing something wrong? Where is my mistake?
AHK 1.0.44.04


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 23rd, 2008, 8:18 pm 
Offline

Joined: March 12th, 2007, 5:25 am
Posts: 14
Thanks for the work savage! I look forward to future updates to this AHK script.

I've been using ShortPopUp for some time now. It's an awesome little app, but it's no longer being maintained. I've been trying remove the need for having tons of small apps by figuring out how to use AutoHotKey for everything. I'm close to achieving this, but ShortPopUp continues to find uses.

Perhaps you could take a look at how it operates, in the very least to get feature/enhancement ideas for your AHK script.

_________________
-Thracx

"Man wants to know, and when he ceases to do so, he is no longer a man."
-Fridtjof Nansen


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2008, 4:12 am 
Wow! ShortPopUp is very good!

It will be great if we can make an AHK script with same functionallity.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2008, 5:39 am 
Offline

Joined: March 12th, 2007, 5:25 am
Posts: 14
Anonymous wrote:
It will be great if we can make an AHK script with same functionallity.


That's what I'm saying! Hopefully our posts will encourage more work being put into the FolderMenu script!

_________________
-Thracx

"Man wants to know, and when he ceases to do so, he is no longer a man."
-Fridtjof Nansen


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, Yahoo [Bot] and 7 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group