Brief Notepad automation demonstration

Many, many thanks to @kzaverdinos for the tip near the bottom of this thread, which I needed to send modifier keys.

Here’s the code:

Import                                  "C:\Users\admin\Documents\Robin\Notepad2.appmask"
                                        # Import appmask
System.RunApplication                   ApplicationPath:  'C:\Windows\Notepad.exe' \
                                        CommandLineArguments:'' \
                                        WorkingDirectory:'' \
                                        WindowStyle:System.ProcessWindowStyle.Normal \
                                        ProcessId=> ProcessId
                                        # Run application

UiAutomation.GetForegroundWindow        WindowTitle=> WindowTitle \
                                        WindowInstance=> NotepadInstance
                                        # For safety's sake, get the foreground window
UiAutomation.FocusWindowByInstanceOrHandle \
                                        WindowInstance: NotepadInstance
                                        # This may duplicate what was done above
UiAutomation.FocusTextField             TextField: Notepad2.Notepad.MainScreen.MainEdit
                                        # Focus the 'Edit' field
MouseAndKeyboard.SendKeys               TextToSend:   'This is a test of changing font type and style.' \
                                        DelayBetweenKeystrokes:0
                                        # Send a message to the viewer
wait 2
                                        # Wait before formatting
MouseAndKeyboard.SendKeys               TextToSend:   '{Alt}{O}{F}' \
                                        DelayBetweenKeystrokes:0
                                        # Thanks @kzaverdinos!!!!
                                        # Send hotkeys for formatting menu
UIAutomation.FocusTextField             TextField: Notepad2.Notepad.FontScreen.Font
                                        # Focus font type edit field
UIAutomation.PopulateTextField          TextField: Notepad2.Notepad.FontScreen.Font \ 
                                        Text: 'Comic Sans MS'
                                        # Select a horrid typeface :)
UIAutomation.FocusTextField             TextField: Notepad2.Notepad.FontScreen.FontStyle
                                        # Focuse the style edit field
UIAutomation.PopulateTextField          TextField: Notepad2.Notepad.FontScreen.FontStyle \ 
                                        Text: 'Bold Oblique'
                                        # Make it italic
UIAutomation.FocusTextField             TextField: Notepad2.Notepad.FontScreen.FontSizeEdit
                                        # Focus the font size edit field
UIAutomation.PopulateTextField          TextField: Notepad2.Notepad.FontScreen.FontSizeEdit \ 
                                        Text: '48'
                                        # Make it huge
UiAutomation.Click                      Element:  Notepad2.Notepad.FontScreen.Button_OK \
                                        ClickType:UIAutomation.ClickType.LeftClick \
                                        MousePositionRelativeToElement:UIAutomation.RectangleEdgePoint.MiddleCenter \
                                        OffsetX:0 \
                                        OffsetY:0
                                        # Click OK
set MyMessage to """I will now reset to my default settings, then exit."""

Display.ShowMessageWithTimeout          Title:'Nota bene . . .' \
                                        Message:MyMessage \
                                        Icon:Display.Icon.Information \
                                        Buttons:Display.Buttons.OK \
                                        DefaultButton:Display.DefaultButton.Button1 \
                                        IsTopMost:False \
                                        Timeout:2 \
                                        ButtonPressed=> ButtonPressed

wait 1

MouseAndKeyboard.SendKeys               TextToSend:   '{Alt}{O}{F}' \
                                        DelayBetweenKeystrokes:0

UIAutomation.FocusTextField             TextField: Notepad2.Notepad.FontScreen.Font
                                        
UIAutomation.PopulateTextField          TextField: Notepad2.Notepad.FontScreen.Font \ 
                                        Text: 'Symbola'
                                        
UIAutomation.FocusTextField             TextField: Notepad2.Notepad.FontScreen.FontStyle
                                        
UIAutomation.PopulateTextField          TextField: Notepad2.Notepad.FontScreen.FontStyle \ 
                                        Text: 'Regular'
                                        
UIAutomation.FocusTextField             TextField: Notepad2.Notepad.FontScreen.FontSizeEdit
                                        # Focus the font size edit field
UIAutomation.PopulateTextField          TextField: Notepad2.Notepad.FontScreen.FontSizeEdit \ 
                                        Text: '10'
                                        
UiAutomation.Click                      Element:  Notepad2.Notepad.FontScreen.Button_OK \
                                        ClickType:UIAutomation.ClickType.LeftClick \
                                        MousePositionRelativeToElement:UIAutomation.RectangleEdgePoint.MiddleCenter \
                                        OffsetX:0 \
                                        OffsetY:0

UiAutomation.FocusTextField             TextField: Notepad2.Notepad.MainScreen.MainEdit
                                        # Focus the 'Edit' field
MouseAndKeyboard.SendKeys               TextToSend:   '{Ctrl}{A}{Ctrl}{X}' \
                                        DelayBetweenKeystrokes:0

wait 2

MouseAndKeyboard.SendKeys               TextToSend:   '{Alt}{F}x' \
                                        DelayBetweenKeystrokes:10

And here’s an animated GIF of the result:

MyTest3

In the AppMask, I used a separate Screen for the main application window, another for the Font dialog. The controls are easy to add from there for font face, style, size.

Regards,
burque505

2 Likes

Another great guide @burque505!
Looking forward to the next!

Best regards,
James


James Papadimitriou
Tech Evangelist
My Github

1 Like