Combo Posted February 4, 2022 Posted February 4, 2022 (edited) Hi, this should be simple but i'm not sure what am i doing wrong. I need to run a program, if the correct title doesn't show up, press esc multiple times to exit the program. When i run this, the exe wont stay on focus. Is it windows 11 issue? Run("C:\resetter\adjust.exe") While 1 If WinExists("programtitle") Then ExitLoop Else Send("{ESC 3}") EndIf Sleep(100) WEnd . . script continues Edit, what i mean is when i run the script, the "adjust.exe" starts, but it runs in the background. It doesn't do that and starts normally when i use this code Run("C:\resetter\adjust.exe") WinWaitActive("programtitle") Edited February 4, 2022 by Combo
SOLVE-SMART Posted February 4, 2022 Posted February 4, 2022 (edited) Hi @Combo, it's a bit of confusing to me. What do you exactly want to and how it should behave? 1 hour ago, Combo said: Edit, what i mean is when i run the script, the "adjust.exe" starts, but it runs in the background. It doesn't do that and starts normally when i use this code Do you want to wait for program exists or should it be displayed or not or should it be have the specific title? Please a bit more context, because there are plenty of ways to achieve "a" assumed varaint. Best regards Sven ________________Stay innovative! Edited February 4, 2022 by SOLVE-SMART ==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon)
Combo Posted February 4, 2022 Author Posted February 4, 2022 (edited) 7 minutes ago, SOLVE-SMART said: Hi @Combo, it's a bit of confusing to me. What do you exactly want to and how it should behave? Do you want to wait for existing program or should it be displayed or not or should it be have the specific title? Best regards Sven ________________Stay innovative! So : 1. run adjust.exe 2. In case if window title "programtitle" doesn't show up (e.g other window like error,etc show up instead,) press esc I'm doing this for other windows inside adjust.exe, just made a simplified example above to explain my purpose Edited February 4, 2022 by Combo
SOLVE-SMART Posted February 4, 2022 Posted February 4, 2022 (edited) Hi again, I understood that your first problem is the sending escape 3 times to a windows: Send("{ESC 3}") What if you bring the window which should receive the escape, to the front (gives focus to) like: For $i = 1 To 3 Step 1 WinActivate($sProgramTitle) Send("{ESC}") Next Is that helpful or still not what you want? Best regards Sven ________________Stay innovative! Edited February 4, 2022 by SOLVE-SMART ==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon)
Combo Posted February 4, 2022 Author Posted February 4, 2022 2 minutes ago, SOLVE-SMART said: Hi again, I understood that your first problem is the sending escape 3 times to a windows: Send("{ESC 3}") What if you bring the window which should receive the escape to the front (gives focus to) like: For $i = 1 To 3 Step 1 WinActivate($sProgramTitle) Send("{ESC 3}") Next Is that helpful or still not what you want? Best regards Sven ________________Stay innovative! I just tested it and that's not what i mean i think. But thanks to your explanation, i understand why it went to background in the first place now, it's because i send escape command. Your code behaves the same way unfortunately, so what's the correct way if i want to do this? 1. wait until the correct window title show up 2. if other window title like error / etc shows up, press esc to close that window and abort script. There are multiple times this could happen in the exe that i want to automate because it's a printer checking program.
SOLVE-SMART Posted February 4, 2022 Posted February 4, 2022 (edited) Hi @Combo, you could use AdlibRegister to poll each second (configurable) for your other, maybe unexpected, windows and close them by "{ESC}" or "WinClose()". expandcollapse popup; opt -------------------------------------------------------------------------- Opt('MustDeclareVars', 1) Opt('WinTitleMatchMode', 2) ; declaration ------------------------------------------------------------------ Global $sAdjsutExe = 'C:\resetter\adjust.exe' Global $sExpectedTitle = 'yourProgramTitle' Global Const $iTimeoutInSeconds = 2 ; processing ------------------------------------------------------------------- Run($sAdjsutExe) AdlibRegister('_closeWindows', 1000) While True If WinWaitActive($sExpectedTitle, '', $iTimeoutInSeconds) Then ExitLoop EndIf _closeWindows() Sleep(250) WEnd AdlibUnRegister('_closeWindows') MsgBox('', '', 'Script continues') ; functions -------------------------------------------------------------------- Func _closeWindows() _closeWindow('yourErrorWindowTitle') _closeWindow('yourOtherWindowTitle') _closeWindow('justAnotherOne') EndFunc Func _closeWindow($sWindowTitle) If WinExists($sWindowTitle) Then WinActivate($sExpectedTitle) Send('{ESC}') EndIf EndFunc It would be even better when you check for window handle or class (title/hWnd/class) instead of the title. But this depends on the windows which pop up. The AdlibUnRegister at line 29 is just a good practise but not mandatory. I hope this will help or even solve your problem!? Best regards Sven ________________Stay innovative! Edited February 4, 2022 by SOLVE-SMART ==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon)
Combo Posted February 4, 2022 Author Posted February 4, 2022 Thanks, i will try to integrate it to my scripts. There are 3-4 different window titles, so i guess i will need to call this 3-4 times as well. About the functions, i'd assume i need to list the possible error windows? Would it be possible to send escape key if the incorrect window title shows up? You know, whitelist instead of blacklist
SOLVE-SMART Posted February 4, 2022 Posted February 4, 2022 Hi @Combo, 3 minutes ago, Combo said: There are 3-4 different window titles, so i guess i will need to call this 3-4 times as well. No you just add the different windows to the _closeWindows() function. The AdlibRegister will do the polling (call this function each 1000 ms). 5 minutes ago, Combo said: About the functions, i'd assume i need to list the possible error windows? Yes or they all share a common "class" name which could be used in WinExists, WinActivate, WinClose etc. 6 minutes ago, Combo said: Would it be possible to send escape key if the incorrect window title shows up? You know, whitelist instead of blacklist Yes in case they share a common "class" name (like mentioned above), but this windows/popups/dialog should have a unique class name. Otherwise it could be, that you will close other windows by accident which also have this class name. I am talking about this information provided by the InfoTool: Best regards Sven ________________Stay innovative! ==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon)
Combo Posted February 4, 2022 Author Posted February 4, 2022 (edited) Hmm maybe it's better if i post the whole code, by 3-4 window i was referring to the correct titles that will be used, and there will be some actions after each of window detection : expandcollapse popupRun("C:\resetter\adjust.exe") WinWaitActive("model = (not selected) | port = Auto selection | AdjProg Ver.1.0.0") #1 Send("{TAB 2}") Sleep (2000) Send("{ENTER}") WinWaitActive ("Adjustment Program") #2 Send("{TAB}") Sleep (2000) Send("{DOWN 4}") Sleep (2000) Send("{TAB}") Sleep (2000) send("!{DOWN}") Sleep (1000) Send("{DOWN}") Sleep (2000) Send("{ENTER}") Sleep (2000) Send("{ENTER}") WinWaitActive ("model = L350 | port = USB001 | AdjProg Ver.1.0.0") #3 Send("{TAB 3}") Sleep (2000) Send("{DOWN 24}") Sleep (2000) Send("{ENTER}") WinWaitActive ("model = L350 | port = USB001 | AdjProg Ver.1.0.0") #4 Send("{TAB}") Sleep (2000) Send("{ENTER}") Sleep (5000) Send("{TAB}") Sleep (5000) WinWaitActive ("Save as") #5 Send(@MON & "-" & @MDAY & "-" & @YEAR) Sleep (2000) Send("{ENTER}") Send("{ESC 4}") #close whole program I need to place the close window function that i mentioned on my first post on #1 until #5. The tricky part is, #3 and #4 have the same window title name. (But when executed above code works cause #4 window will be the focus after #3, and #3 will be in the background) Hence i probably need to whitelist the correct name instead of blacklist the possible errors. I will see if i can find the class of window #3 and #4 then with infotool Edit : well infotool says both #3 and #4 has the same class Edited February 4, 2022 by Combo
SOLVE-SMART Posted February 4, 2022 Posted February 4, 2022 (edited) Hi again, maybe another way: Is the "adjust.exe" also a AutoIt Program or an external one? Can you please share the information (screenshots) that you get when you spy on the five windows/popups? Also the summary texts of the window info tool: This would help to think about proper decisions. I recommend to automate the program by other mechanisms like ControlCommand or UIA Automation, but this depends on your AutoIt skill level to be honest. Doing this "press this N times, press that N times" approach could lead to an unrobust behavior. But could be okay if you need your solution asap instead of expand your experience about UIA. Best regards Sven ________________Stay innovative! Edited February 4, 2022 by SOLVE-SMART ==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon)
Combo Posted February 4, 2022 Author Posted February 4, 2022 Ok, here you go, it's already ordered from window 1-4, window 5 is just regular windows' save as page ( i can't access it right now cause i'm not connected to the printer) https://imgur.com/a/a506QT2 It's not an autoit program, i have tried using pywinauto but i got stuck in identifying the 4th window. Actually i found autoit the easiest tool for automating this, the above code is already working properly. i just need a failsafe window cancelling function in case if printer is not detected, hdd corrupted and exe not detected, etc.
ad777 Posted February 4, 2022 Posted February 4, 2022 @Combo this how you can get handle and wintitle of same window: For $i = 1 To winlist()[0][0] $win = WinList("model = L350 | port = USB001 | AdjProg Ver.1.0.0")[$i][0] $win_h = WinList(@win)[$i][1] MsgBox(64,"Window:"&$i,$win&@CRLF&$win_h) Next none
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now