Popup Text Menu

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
marstech
Posts: 7
Joined: 21 Nov 2017, 23:47

Popup Text Menu

22 Nov 2017, 00:01

Trying to consolidate some of my Autohotkey functions. I have one now that changes case of a selection. Rather than have to press Shift-Ctrl-L for lowercase and Shift-Ctrl-U for upper case etc, I would like to just press one hotkey and then select from a simple text popup 1,2,3,4 to execute the desired task. I looked at some sample menu items. Sort of see how I can make a stand alone app with it, but I can't seem to incorporate it into my existing setup that is always running. Hope that makes sense. Thanks!
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Popup Text Menu

22 Nov 2017, 00:15

Have your hotkey call a function that has all of your "GUI" commands that define the window that you want to pop up.

For example:

Code: Select all

; Ctrl + Shift + C opens the case conversion menu
^+C::CaseConversionMenu()

; This function displays a menu for converting text to lowercase, uppercase or title case
CaseConversionMenu()
{
    ; Store the title of the window with the text to be changed so that it can be activated later
    WinGetActiveTitle, CurrentWindow

    ; Create and show the menu
    Gui, New, -MinimizeBox, Case Conversion Menu
    Gui, Add, Text, x10 y20 w160 h20 Center, Convert the selected text to...
    Gui, Add, Button, x10 y50 w160 h30 gLowercase, lowercase
    Gui, Add, Button, x10 y90 w160 h30 gUppercase, UPPERCASE
    Gui, Add, Button, x10 y130 w160 h30 gTitlecase, Title Case
    Gui, Show, h170 w180, Case Conversion Menu
    return

    Lowercase:
      Gosub, CopyText
      StringLower, clipboard, clipboard
      Gosub, PasteTest
    return

    Uppercase:
      Gosub, CopyText
      StringUpper, clipboard, clipboard
      Gosub, PasteTest
    return

    Titlecase:
      Gosub, CopyText
      StringUpper, clipboard, clipboard, T
      StringCaseSense, On
      clipboard := StrReplace(clipboard, " A ", " a ")
      clipboard := StrReplace(clipboard, " An ", " and ")
      clipboard := StrReplace(clipboard, " As ", " as ")
      clipboard := StrReplace(clipboard, " At ", " at ")
      clipboard := StrReplace(clipboard, " But ", " but ")
      clipboard := StrReplace(clipboard, " By ", " by ")
      clipboard := StrReplace(clipboard, " For ", " for ")
      clipboard := StrReplace(clipboard, " In ", " in ")
      clipboard := StrReplace(clipboard, " Of ", " of ")
      clipboard := StrReplace(clipboard, " Off ", " off ")
      clipboard := StrReplace(clipboard, " On ", " on ")
      clipboard := StrReplace(clipboard, " The ", " the ")
      clipboard := StrReplace(clipboard, " To ", " to ")
      clipboard := StrReplace(clipboard, " Up ", " up ")
      Gosub, PasteTest
    return

    ; Commands that all three above subroutines use prior to string conversion
    CopyText:
      Gui, Hide
      WinActivate, CurrentWindow
      clipboard = 
      Send, {ctrl down}{c}{ctrl up}
    return

    ; Paste command that all three above subroutines use
    PasteTest:
      Send, {ctrl down}{v}{ctrl up}
    return
}
For anyone wondering and wanting something like this, this is a functioning script to convert any selected text to lowercase, uppercase or title case. Select/highlight any text, press Ctrl+Shift+C and then press one of the three buttons on the pop-up menu. You can also press Ctrl+Shift+C first, then select the text, then go back to the menu and press a button.
Last edited by Osprey on 23 Nov 2017, 23:05, edited 2 times in total.
marstech
Posts: 7
Joined: 21 Nov 2017, 23:47

Re: Popup Text Menu

22 Nov 2017, 14:12

That what I'm trying to do. I like the clean look with 'Gui, -Caption' too. I thought I was missing something fundamental but it turns out I forgot to close an #If block from another script. Duh. Thanks so much for your post!
User avatar
davebrny
Posts: 85
Joined: 05 Dec 2016, 06:26

Re: Popup Text Menu

22 Nov 2017, 18:45

you could give ezMenu a try https://github.com/davebrny/ezMenu
its "ez" as in easy... but i will admit, its probably no where near 100% easy either :D its just easier compared to making menus and sub-menus from scratch

if you download the repo, there is an example script in the example folder that shows some of the different ways of using it.
it would look something like this:

Code: Select all

^+c::
menu_text = 
(
convert case
    upper case
    lower case
    title case
other commands
    toggle always on top
    open folder
        folder 1
        folder 2
    hibernate computer
)
ezMenu("consolidate", menu_text )
return


consolidate:
this_menu := strReplace(a_thisMenuItem, a_space, "_")  ; change "upper case" menu text to "upper_case"
; this_menu := strReplace(a_thisMenuItem, a_space, "")  ; or "upperCase"
if isLabel(this_menu)
     goSub, %this_menu%
else msgBox, you selected `n"%this_menu%"
return

upper_case:
msgBox, change to upper case
return

lower_case:
msgBox, change to lower case
return

when you select any item from the menu, say "upper case", it runs the "consolidate" label which then checks if the label "upper_case" exists and if it does then it runs that label.

to use ezMenu.ahk in your own script, you just have to #include the file or else copy ezMenu.ahk to your user library so you can use it in any of your scripts without having to use the include command each time
marstech
Posts: 7
Joined: 21 Nov 2017, 23:47

Re: Popup Text Menu

23 Nov 2017, 16:34

Have it working with ezMenu right. I tried the one Osprey posted and it works great. I prefer to not have buttons though. I like the way with text I can press the hotkey and then use up and down arrow to select it. I can't see to get the syntax correct for just a plain text menu like ezMenu does. If someone could provide a simple example for that it would be greatly appreciated. Thanks for the posts!
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Popup Text Menu

23 Nov 2017, 17:26

Just look at the "Gui, Add, Text" line in my menu above for how to do plain text. You can either write as much on that one line as you want and then increase the height (the number next to the 'h' in the options section of the line) or duplicate that line and then modify the new line's 'y' position to be about the first line's 'y' position + its 'h' (height) in order to have the new line of text be directly under the other.
marstech
Posts: 7
Joined: 21 Nov 2017, 23:47

Re: Popup Text Menu

23 Nov 2017, 21:51

I added the text, but I can't select menu items with the arrow keys. The ezMenu performs exactly like when I'm looking for, I just like to know to do it myself.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Popup Text Menu

23 Nov 2017, 22:13

Oh, sorry. I thought that you meant a GUI menu. You meant a Menu menu. Here's a simple example for you:

Code: Select all

Menu, MySubmenu, Add, SubItem1
Menu, MySubmenu, Add, SubItem2
Menu, MyMenu, Add, Item 1, :MySubmenu
Menu, MyMenu, Add, Item 2, Item2

^+c::Menu, MyMenu, Show

SubItem1:
  MsgBox, You clicked on SubItem1
return

SubItem2:
  MsgBox, You clicked on SubItem2
return

Item2:
  MsgBox, You clicked on Item2
return
The main things to remember are that your submenus have to be defined before your main menu and each of the subroutines listed at the end of the menu item declarations ("SubItem1", "SubItem2" and "Item2" in my example) has to exist.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Popup Text Menu

23 Nov 2017, 22:25

Here's a version of my Case Conversion Menu from above in a pop-up menu (rather than a window with buttons) format:

Code: Select all

; Ctrl + Shift + C opens the case conversion menu
^+C::CaseConversionMenu()

; This function displays a menu for converting text to lowercase, uppercase or title case
CaseConversionMenu()
{
    ; Store the title of the window with the text to be changed so that it can be activated later
    WinGetActiveTitle, CurrentWindow

    ; Create and show the menu
    Menu, CaseConversionMenu, Add, Title Case, Titlecase
    Menu, CaseConversionMenu, Add, lowercase, Lowercase
    Menu, CaseConversionMenu, Add, UPPERCASE, Uppercase
    Menu, CaseConversionMenu, Default, Title Case
    Menu, CaseConversionMenu, Show, % MouseX - 17, % MouseY - 12

    Lowercase:
      Gosub, CopyText
      StringLower, clipboard, clipboard
      Gosub, PasteTest
    return

    Uppercase:
      Gosub, CopyText
      StringUpper, clipboard, clipboard
      Gosub, PasteTest
    return

    Titlecase:
      Gosub, CopyText
      StringUpper, clipboard, clipboard, T
      StringCaseSense, On
      clipboard := StrReplace(clipboard, " A ", " a ")
      clipboard := StrReplace(clipboard, " An ", " and ")
      clipboard := StrReplace(clipboard, " As ", " as ")
      clipboard := StrReplace(clipboard, " At ", " at ")
      clipboard := StrReplace(clipboard, " But ", " but ")
      clipboard := StrReplace(clipboard, " By ", " by ")
      clipboard := StrReplace(clipboard, " For ", " for ")
      clipboard := StrReplace(clipboard, " In ", " in ")
      clipboard := StrReplace(clipboard, " Of ", " of ")
      clipboard := StrReplace(clipboard, " Off ", " off ")
      clipboard := StrReplace(clipboard, " On ", " on ")
      clipboard := StrReplace(clipboard, " The ", " the ")
      clipboard := StrReplace(clipboard, " To ", " to ")
      clipboard := StrReplace(clipboard, " Up ", " up ")
      Gosub, PasteTest
    return

    ; Commands that all three above subroutines use prior to string conversion
    CopyText:
      WinActivate, CurrentWindow
      clipboard = 
      Send, {ctrl down}{c}{ctrl up}
    return

    ; Paste command that all three above subroutines use
    PasteTest:
      Send, {ctrl down}{v}{ctrl up}
    return
}
The main drawback to this method is that you need to remember to select the text before opening the menu.
marstech
Posts: 7
Joined: 21 Nov 2017, 23:47

Re: Popup Text Menu

24 Nov 2017, 12:15

Yeah, that is a downside, but for me not having to take my hands off keys is better. I added Menu, CaseMenu, DeleteAll in the CopyText section. It was doubling up on the text for some reason. I really appreciate your help! I've wanted to clean up my scripts for a long time. Incorporating the menus saves me from having to remember as many hotkeys. :)

Code: Select all

; Ctrl + Shift + C opens the case conversion menu
^+C::CaseConversionMenu()

; This function displays a menu for converting text to lowercase, uppercase or title case
CaseConversionMenu()
{
	; Store the title of the window with the text to be changed so that it can be activated later
	WinGetActiveTitle, CurrentWindow

	; Create and show the menu
	Menu, CaseMenu, Add, Lowercase, Lowercase
	Menu, CaseMenu, Add, Uppercase, Uppercase
	Menu, CaseMenu, Add, Title Case, TitleCase
	Menu, CaseMenu, Show
	Return

	Lowercase:
		Gosub, CopyText
		StringLower, clipboard, clipboard
		Gosub, PasteTest
	Return

	Uppercase:
		Gosub, CopyText
		StringUpper, clipboard, clipboard
		Gosub, PasteTest
	Return

	Titlecase:
		Gosub, CopyText
		StringUpper, clipboard, clipboard, T
		Gosub, PasteTest
	Return

	; Commands that all three above subroutines use prior to string conversion
	CopyText:
		Menu, CaseMenu, DeleteAll
		WinActivate, CurrentWindow
		clipboard = 
		Send, {ctrl down}{c}{ctrl up}
	Return

	; Paste command that all three above subroutines use
		PasteTest:
		Send, {ctrl down}{v}{ctrl up}
	Return
}
User avatar
davebrny
Posts: 85
Joined: 05 Dec 2016, 06:26

Re: Popup Text Menu

24 Nov 2017, 12:49

marstech wrote:I've wanted to clean up my scripts for a long time. Incorporating the menus saves me from having to remember as many hotkeys. :)
ive been thinking of doing the same thing for a while now. there are times i make something new but i cant think of a logical hotkey combination to use for it, or if i do i usually forget what it is after a few weeks.
im thinking i might start making everything as labels first, and then have something that searches label names and then puts them into a menu, or maybe just have a gui where you could search/run them

anwya, in case you dont know, the other good thing about using the menus is that you can select items and submenus by typing the first letter, so its sort of like a 2 or 3 stage hotkey once you get the hang of it.
i use a menu for sending git commands that i open with alt+g and then i type c to open the "commit" submenu and then m to send the "git commit -m "message" item
so altogether its alt+gcm
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Popup Text Menu

24 Nov 2017, 12:58

I have various hotkeys for things. But for less common things, I use Win+F to bring up an InputBox for general things (mostly opening things), and Win+T to bring up an InputBox for text functions (that typically operates on the selected text). And I use short strings for each item. It's actually quite easy to remember a lot of short strings.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
marstech
Posts: 7
Joined: 21 Nov 2017, 23:47

Re: Popup Text Menu

25 Nov 2017, 16:35

That sounds good. If you haven't seen it you might want to look at http://keypirinha.com/. I setup a folder on a second drive for it with categories in subdirectories. Also some pretty powerful addons for it.
marstech
Posts: 7
Joined: 21 Nov 2017, 23:47

Re: Popup Text Menu

25 Nov 2017, 19:58

I have the script almost identical to the one I posted. Any reason the menu would be drawn when other scripts are triggered? To test I have an include to the CaseConversation.ahk and Test.ahk. In the test one I have just one hotkey (Ctrl-Shift+T) run an app. Before it runs the app though, I see the case conversation menu on the screen.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Popup Text Menu

25 Nov 2017, 21:35

Do you have a menu defined in the Test one? Perhaps instead of showing that menu when you press Ctrl+Shift+T, it's showing the one in the other script, which overwrote it. I just did a very simple test with a single hotkey in two different scripts and used an #include in one that pointed to the other and both hotkeys worked without executing the other.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Auntiejack56, Google [Bot] and 119 guests