Jump to content

Recommended Posts

Posted (edited)

Hello, 

It seems I'm lousy at RegEx. I'm getting text from menus using

$name = _GUICtrlMenu_GetItemText($menu, $c)

Some menu text looks like, New Ctrl+N, Open Ctrl+O etc. with one space between the menu text and the shortcut letters.

Some menu text looks like, Save As..., Save Copy As..., no shortcut letters.

I want to get all the text up to the shortcuts e.g. Ctrl+N or Ctrl+O, I just need New, Open etc.

For the life of me I can not figure out how to get just back the menu text without the shortcut letters

I know people are reluctant to write code but I'm failing and I've read the documentation, I just clearly don't understand.

I have tried 

$matches = StringRegExp($name, "(.*)(\s)", 1)

Can someone please offer some help?

Thank you.

Edited by CygnusX1

Cygnus

Posted
local $aArr = ["New Ctrl+N" , "Open Ctrl+O" , "Save As...", "Save Copy As..."]


For $stuff in $aArr
   $sRex = StringRegExpReplace($stuff , "(\s.*\+.\z)" , "")
   consolewrite($sRex & @CR)
Next

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted (edited)

mine probably should have a + instead of an asterisk as there probably wouldn't be a case where there was no left side of the key combination, but I suck at regex so it's best to just wait for the good answers to show up :)

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted (edited)

I don't think it is possible to write a pattern that can make:

New Ctrl+N
Save As
Circle C

equal to:

New
Save As
Circle

The best option you have is to use a Regex like the one Iamtheky suggests to clean the one with the "+" sign, and to hardcode something like:

StringReplace("Source string and something else", "Source string")

 

Edited by j0kky
Posted (edited)

Are there any tabs between the menu item and the shortcut? Can you give us a return value from _GUICtrlMenu_GetItemText?

Edited by GMK
_GUICtrlMenu_GetItemText
Posted

Here are some examples of returned text. It is Swedish by the way.

Arkiv
&Ny    Ctrl+N
Ny
&Öppna...    Ctrl+O
Öppna...
&Skapa komponent...    G
Zoom &Fönster    Ctrl+Shift+W

I don't think there are tabs between the text. When I copy the text into a text editor and view spaces, tabs, LF etc. I see a dot for a space.

Cygnus

Posted (edited)

so if it has plus signs or is something preceded by multiple spaces, it disappears?

 

local $aArr = ["Arkiv" , "&Ny    Ctrl+N" , "Ny" , "&Oppna...    Ctrl+O" , "Oppna..." , "&Skapa komponent...    G" , "Zoom &Fonster    Ctrl+Shift+W "]


For $stuff in $aArr
   $sRex = StringRegExpReplace($stuff , "(\s.+\+.+?\z)|(\s\s\s\s.*\z)" , "")
   consolewrite($sRex & @CR)
Next

*I altered your foreign characters so as to reflect correctly in the console

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted (edited)

This works for me:

#include <GuiMenu.au3>

Example()

Func Example()
    Local $hWnd, $hMain, $hFile, $iCount

    ; Open Notepad
    Run("notepad.exe")
    WinWaitActive("[CLASS:Notepad]")
    $hWnd = WinGetHandle("[CLASS:Notepad]")
    $hMain = _GUICtrlMenu_GetMenu($hWnd)
    $hFile = _GUICtrlMenu_GetItemSubMenu($hMain, 0)
    $iCount = _GUICtrlMenu_GetItemCount($hFile)
    
    ; Get item text
    For $iItem = 0 To $iCount
        Writeln("Item text: " & StringRegExpReplace(_GUICtrlMenu_GetItemText($hFile, $iItem), '(?:\h{2,}|\t)\S+', ''))
    Next
EndFunc   ;==>Example

; Write a line of text to Notepad
Func Writeln($sText)
    ControlSend("[CLASS:Notepad]", "", "Edit1", $sText & @CRLF)
EndFunc   ;==>Writeln

 

Edited by GMK
Forgot to declare $iCount
Posted

Bravo GMK! 

I really appreciate your help. I'm going to dig into what the RegEx is doing so I can understand the mechanics of it and how it works. 

Thanks again!

Cygnus

Posted

jg doesn't rex that ass, he wrecks that ass!

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...