Jump to content

Followed tutorial to save file in notepad worked but unable to select Don't Save


molitar
 Share

Go to solution Solved by benners,

Recommended Posts

So here is the tutorial I used.
 

Run("notepad.exe")

WinWaitActive("Untitled - Notepad")

Send("Cool IT Help Tutorial")

WinClose("*Untitled - Notepad")

WinWaitActive("Notepad", "Save")

Send("{ENTER}")

WinWaitActive("Save As", "Text")

Send("C:\Users\4477724\Desktop\AutoIT\MyFirstFiletxt")

WinWaitActive("Save As", "Save")

Send("{ENTER}")

WinWaitActive("MyFirstFile.txt - Notepad")

WinClose("MyFirstFile.txt - Notepad")

So I tried to change the line that says WinWaitActive("Notepad", "Save") to the one below.

WinWaitActive("Notepad", "Don't Save")

Than I tried to use the text from the Window Info instead still did not work.

WinWaitActive("Notepad", "Do&n't Save")

So it appears that the ONLY reason the save is working is because that is already the default button and that line of code is not actually selecting the button at all.  So how do I actually properly select the button? 

Link to comment
Share on other sites

when you close the notepad, then display the message Do you want to save changes to Untitled? with 3 buttons save, Don't save, cancel

when you press alt then you see     Save , Don 't Save

This mean when you press (send)  s = Save ,    when you press (send)  n = Don 't Save

 

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("Cool IT Help Tutorial")
WinClose("*Untitled - Notepad")
WinWaitActive("Notepad", "Save")

Sleep(2000)
Send("n")

 

I know that I know nothing

Link to comment
Share on other sites

OK found this works.
 

WinWaitActive("Notepad", "Save")

ControlClick("Notepad", "", "Do&n't Save")

Now is it possible to get the result of which button was pressed in this window?  The windowinfo below.

Quote

>>>> Window <<<<
Title:    Notepad
Class:    #32770
Position:    769, 418
Size:    366, 141
Style:    0x96C80284
ExStyle:    0x00010101
Handle:    0x0000000000150AC4

>>>> Control <<<<
Class:    Button
Instance:    2
ClassnameNN:    Button2
Name:    
Advanced (Class):    [CLASS:Button; INSTANCE:2]
ID:    
Text:    Do&n't Save
Position:    170, 70
Size:    92, 23
ControlClick Coords:    48, 14
Style:    0x50000000
ExStyle:    0x00000000
Handle:    0x00000000002D0AE0

>>>> Mouse <<<<
Position:    995, 533
Cursor ID:    0
Color:    0xE1E1E1

>>>> StatusBar <<<<

>>>> ToolsBar <<<<

>>>> Visible Text <<<<
&Save
Do&n't Save
Cancel


>>>> Hidden Text <<<<

 

I can't figure out how to get result of which button was pressed and than show in a message box.  If I can get it to show in message box than I can use it for if statements.

Link to comment
Share on other sites

The result of pressing the "Dont Save" button would be for Notepad to close. Either check the return from ControlClick or use ProcessExists to check if it is still running. An example is in the help file

#include <MsgBoxConstants.au3>

If ProcessExists("notepad.exe") Then ; Check if the Notepad process is running.
        MsgBox($MB_SYSTEMMODAL, "", "Notepad is running")
Else
        MsgBox($MB_SYSTEMMODAL, "", "Notepad is not running")
EndIf

 

Edited by benners
Link to comment
Share on other sites

OK that works for ControlClick but is it possible to get the value of button pressed the other way around.  Detect what button is clicked on by the user?

Now I have a problem with this code I have to keep using the Stop Script as it does not properly terminate it seems.

#include <MsgBoxConstants.au3>

Run("notepad.exe")

WinWaitActive("Untitled - Notepad")

Send("Cool IT Help Tutorial")

WinClose("*Untitled - Notepad")

WinWaitActive("Notepad", "Save")

; variable for name of button
$id = "&Save"

$res = ControlClick("Notepad", "", $id)
If $res Then
    MsgBox(0, "Notepad", "The " & $id & " button was pressed", 3)
Else
    ;error
EndIf

If WinWaitActive("Save As", "Text") Then
    Send("C:\Users\xxxxxx\Desktop\AutoIT\MyFirstFiletxt")
    WinWaitActive("Save As", "Save")
    Send("{ENTER}")
    WinWaitActive("MyFirstFile.txt - Notepad")
    WinClose("MyFirstFile.txt - Notepad")
EndIf

Why does this script hang and not close correctly?

Link to comment
Share on other sites

  • Solution

 

The script hangs because of the WinWaitActive after you send the enter. To detect which button is pressed, here's my dirty way. There's probably a better way by hooking to the mouse but that's above me.

#include <GuiButton.au3>

Run("notepad.exe")

WinWaitActive("Untitled - Notepad")

Send("Cool IT Help Tutorial")

WinClose("*Untitled - Notepad")

WinWaitActive("Notepad", "Save")

Local $h_SaveDlg = WinGetHandle("[ACTIVE]")
Local $h_SaveBtn = ControlGetHandle($h_SaveDlg, "", "[CLASS:Button; INSTANCE:1]")
Local $h_DontSaveBtn = ControlGetHandle($h_SaveDlg, "", "[CLASS:Button; INSTANCE:2]")
Local $h_CancelBtn = ControlGetHandle($h_SaveDlg, "", "[CLASS:Button; INSTANCE:3]")

While WinExists($h_SaveDlg)
     If _GUICtrlButton_GetState($h_SaveBtn) = 620 then
         consolewrite('Save: ' & @crlf)
         ExitLoop
     EndIf

     If _GUICtrlButton_GetState($h_DontSaveBtn) = 620 then
         consolewrite('Don''t Save: ' & @crlf)
         ExitLoop
    EndIf

     If _GUICtrlButton_GetState($h_CancelBtn) = 620 then
         consolewrite('Cancel: ' & @crlf)
         ExitLoop
    EndIf
WEnd

 

Link to comment
Share on other sites

Here another approach using handle and class as much as possible.

I also showed the way to click on the text button, instead of CLASSNN.

#include <Constants.au3>
#include <GuiButton.au3>

Run("notepad.exe")
Local $hWnd = WinWaitActive("[CLASS:Notepad]")
ConsoleWrite($hWnd & @CRLF)

ControlSend($hWnd, "", "Edit1", "Cool IT Help Tutorial")

WinClose($hWnd)
Local $hSave = WinWaitActive("[CLASS:#32770]")
ConsoleWrite($hSave & @CRLF)

ControlClick($hSave, "", "[TEXT:Ne pas en&registrer]")
While WinExists($hSave)
  Sleep(50)
WEnd
MsgBox($MB_OK, "Info", "Notepad was properly closed without saving")

ps. my OS is french, so you need to modify the text of the button accordingly.

Link to comment
Share on other sites

Or something like this:

AutoItSetOption('WinTitleMatchMode', 2)

$sSavePath = @ScriptDir & '\MyDoc.txt'

Run('notepad.exe')
WinWait('Notepad')
WinActivate('Notepad')
Send('Cool IT Help Tutorial')
Send('^s')
WinWait('Save as')
ControlSetText('Save as', '', 'Edit1', $sSavePath)
ControlClick('Save as', '', 'Button2')
WinClose('Notepad')

 

21 minutes ago, Nine said:

Here another approach using handle and class as much as possible.

ControlSend($hWnd, "", "Edit1", "Cool IT Help Tutorial")

 

On Win 11 it's RichEditD2DPT1. I am not sure if this is also true for Win 10. For all others Edit it's the correct class name.

When the words fail... music speaks.

Link to comment
Share on other sites

18 hours ago, benners said:

 

The script hangs because of the WinWaitActive after you send the enter. To detect which button is pressed, here's my dirty way. There's probably a better way by hooking to the mouse but that's above me.

#include <GuiButton.au3>

Run("notepad.exe")

WinWaitActive("Untitled - Notepad")

Send("Cool IT Help Tutorial")

WinClose("*Untitled - Notepad")

WinWaitActive("Notepad", "Save")

Local $h_SaveDlg = WinGetHandle("[ACTIVE]")
Local $h_SaveBtn = ControlGetHandle($h_SaveDlg, "", "[CLASS:Button; INSTANCE:1]")
Local $h_DontSaveBtn = ControlGetHandle($h_SaveDlg, "", "[CLASS:Button; INSTANCE:2]")
Local $h_CancelBtn = ControlGetHandle($h_SaveDlg, "", "[CLASS:Button; INSTANCE:3]")

While WinExists($h_SaveDlg)
     If _GUICtrlButton_GetState($h_SaveBtn) = 620 then
         consolewrite('Save: ' & @crlf)
         ExitLoop
     EndIf

     If _GUICtrlButton_GetState($h_DontSaveBtn) = 620 then
         consolewrite('Don''t Save: ' & @crlf)
         ExitLoop
    EndIf

     If _GUICtrlButton_GetState($h_CancelBtn) = 620 then
         consolewrite('Cancel: ' & @crlf)
         ExitLoop
    EndIf
WEnd

 

Thanks this worked great.  The code is clean and easy to understand and I have values for all 3 buttons. The only question I have is where did you get the value 620 to know that is what is returned.

Link to comment
Share on other sites

I initially tried the return values from _GUICtrlButton_GetState, mainly $BST_PUSHED. This returns 4 but it did not fire the If statements. I then wrote the value of _GUICtrlButton_GetState to the console and when the button was pushed, the value was 620. I tried using that and it triggered everytime.

I'm unsure why it returns that number, could be a few states added together like visible, focused etc. None of the values stated in the help file add up to that.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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