press no on external msgbox with yesno option

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Userdosfx
Posts: 13
Joined: 31 Aug 2019 14:13

press no on external msgbox with yesno option

#1 Post by Userdosfx » 17 Sep 2019 07:54

i want press no button on external msgbox and close it
the msgbox is window not process
i want close it with batch file code

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: press no on external msgbox with yesno option

#2 Post by ShadowThief » 17 Sep 2019 10:46

You can't use batch to interact with GUIs.

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: press no on external msgbox with yesno option

#3 Post by misol101 » 19 Sep 2019 04:52

If an external program is ok, you can try running cmdwiz from your script: viewtopic.php?t=7402

If the dialog box is visible and focused at the time you want to press it from the script, it's easy, just send an 'n' character (key codes at https://msdn.microsoft.com/en-us/librar ... 85%29.aspx):

Code: Select all

cmdwiz sendkey 0x4e p

If the window that owns the dialog is not on top, then we need to find it and show it before sending the key. Use "cmdwiz windowlist" for a list of windows and their processes. Let's say we found out that the process name was Photoshop.exe, we should be able to show the window/dialog with:

Code: Select all

cmdwiz showwindow top /n:Photoshop.exe
Unfortunately at this point the dialog box may not be focused/respond to key input. I have yet to find a reliable way to ensure this, but standard dialog boxes tend to pop up a the center of the screen, so a little hack to move the mouse to the center and click there might work. After this, we send the 'n' character:

Code: Select all

cmdwiz showwindow top /n:Photoshop.exe
cmdwiz getdisplaydim w
set /a X=%errorlevel%/2
cmdwiz getdisplaydim h
set /a Y=%errorlevel%/2-50
cmdwiz setmousecursorpos %X% %Y% l
cmdwiz sendkey 0x4e p

In case the dialog opens as a result of something you do earlier in the script, you may also have to include a delay before doing anything else to make sure the dialog has been opened. Use this for a 1 second delay:

Code: Select all

cmdwiz delay 1000

Szecska
Posts: 17
Joined: 15 Aug 2019 15:29
Location: Hungary

Re: press no on external msgbox with yesno option

#4 Post by Szecska » 28 Sep 2019 15:37

There is a way, but its not pure batch. It uses a temporary vbs script:

Code: Select all

@echo off
echo ret=Msgbox("Continue?",4,"Terminated.") 'msgbox(title,type,headline)   Add 16 to type to show it as an error, 32 to question, 48 to warning, and 64 to information >%TEMP%\yn.vbs 
echo wscript.echo ret >>%TEMP%\yn.vbs
rem The return code for the 'No' button is 7.
rem Now we need to query the code from the vbs.
for /f %%i in ('cscript /nologo %TEMP%\yn.vbs') do (
if %%i==7 (
taskkill /im program.exe
rem taskkill without the /f switch just simulates an ALT+F4 keypress on the process
)
)
del %TEMP%\yn.vbs
If I undestood your problem well, this will help

Post Reply