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 

filemenus v0.2

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Fri Jun 24, 2005 1:18 pm    Post subject: filemenus v0.2 Reply with quote

A slightly more polished version to emulate filemenus. Error checking is still minimal, but this version can send keys and run programs or websites etc. Comments and suggestions are welcome; perhaps this can be coaxed into doing more than just sending keys and running things.

WARNING! TO RUN THIS YOU HAVE TO MAKE SURE TWO THINGS:

1. The file with the menu definitions MUST contain real tabs.
2. Versions prior to 1.0.36 of AHK have a subtle bug that makes this script unusable. Grab the current version if there's a problem

The code reads a file called Filemenu.txt, whose format is described further down. This file contains the menu definition(s); you can define as many menus as you want (or rather as you have hotkeys). Don't forget to separate menuname and hotkey with a real tab; the same's true for menu entries and command code and parameter: no tabs, no fun. (I know that's a rather poor choice but it makes the parsing of the menu file very straightforward. And it looks not half bad in an editor.)

If you use the Filemenu.txt as shown further down, have a window active that accepts more than one line of input before you press #1 and pick a menu option.

@Chris: I think you'll see why I wanted {wait} and {setkeydelay} as Send parameters: this would make it possible to use them in these menus. But I think I can figure out something in the sources as they're pretty well documented.

Code:
;Filemenu.ahk

k=0
Loop, Read, Filemenu.txt
{
  szLine=%A_LoopReadLine%
  If (szLine="") ; empty
    Continue
  If (InStr(szLine,";")=1) ; comment
    Continue
  If (InStr(szLine,"menu:")=1) ; start of new menu definition?
  {
    StringTrimLeft, szName, szLine, 5 ; get rid of menu:
    szMenus_%k%=%szName% ; store menu name and activation key in array szMenus_?, used later in ActivateMenu
    StringSplit, szEntry, szName, %A_Tab%
    szName=%szEntry1% ; stores the base name of the array for menu entries
    Hotkey %szEntry2%, ActivateMenu ; all filemenus go to ActivateMenu
    j=1 ; stores the index into the array for menu entries, 1 because we've got a new menu
    k:=k+1
  }
  Else {
    ; a menu entry has 3 parts: what shows up in the menu, the command, and the paramenter
    szEntry3=
    StringSplit, szEntry, szLine, %A_Tab%
    If (szEntry1="-") ; separator
      Menu, %szName%, Add
    Else {
      Menu, %szName%, Add, %szEntry1%, FilemenuHandler ; add option to menu in szName
      ; store the command
      szCmd%szName%_%j%=%szEntry2%
      ; store the parameter
      If (szEntry3="") ; if empty use menu option itself
        szParm%szName%_%j%=%szEntry1%
      Else
           szParm%szName%_%j%=%szEntry3%
    }
    j:=j+1
  }
}

Exit

; Handles a menu choice
FilemenuHandler:
  szCmd:=szCmd%A_ThisMenu%_%A_ThisMenuItemPos% ; get command
  szParm:=szParm%A_ThisMenu%_%A_ThisMenuItemPos% ; get parameter
  If (szCmd="S")
    Send, %szParm%
   Else If (szCmd="R")
    Run, %szParm%
  Return

; Activates the filemenu linked to the incoming hotkey
ActivateMenu:
  i=0
  loop
  {
    If (i=k)
        Break
    StringSplit, szEntry, szMenus_%i%, %A_Tab%
    If (szEntry2=A_ThisHotkey)
    {
      Menu, %szEntry1%, Show
      Break
    }
    i:=i+1
  }
  Return


Here is Filemenu.txt (the forum software eats the tabs, alas):
Code:
; comment

; A menu definition starts with "menu:" followed by the name (which is used
; internally and should contain no special characters) and the hotkey used
; to activate this specific menu.
; Important! Name and hotkey *have* to be separated  by a literal tab (sorry
; for the poor choice but this makes the parsing code trivial)
menu: TestSend   #1

; Next are the actual menu entries. Three parts, once again all separated by
; real tabs. The first part is the menu entry as it will appear when the menu
; is shown (& for shortcut is ok).
; The second part is a command code. Currently there only two codes: S and R,
; but more easily be defined.
; The third part is the parameter, the meat of the thing. If the command code
; is S, this string will be send to the active window via a Send command.
; If the command code is R, this string will be given to the Run command.
; If this is empty the menu entry will be used instead.
test&1   S   test1 is sent
; special strings like {enter} or {esc} are ok in the command parameter
test&2   S   here is test2{enter}and a new line
; separator
-
test&3   S   here is test3{tab}and more
test&4   S   here is test4{BS 13}and by now it's getting boring
; empty parameter sends test5
test5   S

menu: TestRun   #2
; web site
&Go Google   R   http://www.google.com
http://www.google.com   R
; ditto with parameters
&Search for AHK   R   http://www.google.com/search?q=AutoHotkey
; a program
&Notepad   R   notepad.exe
; Explorer (or whatever handles directories)
e&Xplore c:\   R   c:\


Here is an identical version, without the comments. If you let your editor replace all instances of [[TAB]] with real tabs it should be a usable filemenu:
Code:
menu: TestSend[[TAB]]#1
test&1[[TAB]]S[[TAB]]test1 is sent
test&2[[TAB]]S[[TAB]]here is test2{enter}and a new line
-
test&3[[TAB]]S[[TAB]]here is test3{tab}and more
test&4[[TAB]]S[[TAB]]here is test4{BS 13}and by now it's getting boring
test5[[TAB]]S

menu: TestRun[[TAB]]#2
&Go Google[[TAB]]R[[TAB]]http://www.google.com
http://www.google.com[[TAB]]R
&Search for AHK[[TAB]]R[[TAB]]http://www.google.com/search?q=AutoHotkey
&Notepad[[TAB]]R[[TAB]]notepad.exe
e&Xplore c:\[[TAB]]R[[TAB]]c:\


Last edited by thomasl on Wed Jul 06, 2005 3:56 pm; edited 1 time in total
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Jul 05, 2005 6:04 pm    Post subject: Reply with quote

I cannot make it work?

Error on Line 17
Not a valid hotkey

But I copied and pasted from the script provided?

Anybody have any ideas of how to fix?
Back to top
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Wed Jul 06, 2005 9:26 am    Post subject: Reply with quote

Hm... it works here, for a good few weeks now, without a glitch Smile

Did you make sure that the menu definitions are valid? If you forget to use real tabs between the elements (as described in the file) you will get this message.

Take an editor that does not convert tabs to spaces and type the following two lines:

menu: TestMenu[[TAB]]#1
test[[TAB]]S[[TAB]]test

but make sure you replace each of the three [[TAB]] with a real tab. Save the file and test again with Winkey+1 as hotkey. If it works, fine;if not, mea culpa. I'll have a look then.

Perhaps I should change the script so that it doesn't use tabs as separators.
Back to top
View user's profile Send private message
enrica



Joined: 21 Mar 2005
Posts: 114
Location: Bahia, Brasil

PostPosted: Wed Jul 06, 2005 1:18 pm    Post subject: Nice script! Reply with quote

Hi thomasl, your script is very useful, the part with R run perfectly, but part S not at all, I belive because editor is not active, (I try with 4 different editor)
now I try to insert an "WinActivate, NoteTab Light," but show error.
How can solve?

Code:

FilemenuHandler:
  szCmd:=szCmd%A_ThisMenu%_%A_ThisMenuItemPos% ; get command
  szParm:=szParm%A_ThisMenu%_%A_ThisMenuItemPos% ; get parameter
  If (szCmd="S")

WinActivate, NoteTab Light,

    Send, %szParm%
   Else If (szCmd="R")
    Run, %szParm%
  Return
ActivateMenu:


Thank you, ciao!
_________________
65.6E.72.69.63.61. (My hovercraft is full of eels)
Back to top
View user's profile Send private message
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Wed Jul 06, 2005 1:27 pm    Post subject: Reply with quote

If R works then the whole thing should work because the logic is the same for S and R.

What S does is just send a string to the foreground window (ie. the window that was active before the menu option was selected).

I use this feature not only in an editor but also to do logins on the web, select directories in open/save dialogs etc. And it works perfectly.
Back to top
View user's profile Send private message
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Wed Jul 06, 2005 1:33 pm    Post subject: Reply with quote

Sorry: I also forgot to mention that there is a subtle bug in AHK in versions prior to 1.0.35.10 (I think).

This bug means that the foregorund window is not properly reactivated. Just download the newest version, install and all should be ok. Smile
Back to top
View user's profile Send private message
enrica



Joined: 21 Mar 2005
Posts: 114
Location: Bahia, Brasil

PostPosted: Wed Jul 06, 2005 2:37 pm    Post subject: Yes, now work! Reply with quote

Yes, now work!
Is only version problem.
Thank you, ciao!
_________________
65.6E.72.69.63.61. (My hovercraft is full of eels)
Back to top
View user's profile Send private message
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Wed Jul 06, 2005 4:00 pm    Post subject: Re: Yes, now work! Reply with quote

enrica wrote:
YIs only version problem.


Thanks for bringing this to my attention. I have updated the original post.
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
Page 1 of 1

 
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