WinExist - WinText, How to use this parameter? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
CraigM
Posts: 17
Joined: 21 Sep 2017, 13:07

WinExist - WinText, How to use this parameter?

Post by CraigM » 20 May 2022, 08:43

I have been unable to get the WinText parameter of the WinExist function to work and have not been able to find a working example for either V1 or V2.

The working V2 code I have included, opens Outlook if it is not open or activates the Outlook window if it is already running (on the email screen). Then it resizes the Window and centers, CenterWindow(ctrwin), the Outlook Window accounting for the Task Bar area.

The use of WinText seems like a good choice which would prevent, multiple instances of Outlook being started when the running instance of Outlook is on the calendar or contacts screen.

The code below does work, but I must define the full Window Title in the variable MyEmail. It would be easier to simply say is Outlook running? Which may be harder than I think.

Bug: The current code opens a new instance of Outlook if the current instance is on the calendar or contact screens.

What I would like to do is something like:

if !WinExist(MyEmail, "Outlook")

Which I think should say is does a Window with "Outlook" in its title exist? I can't seem to get this to work.

I attempted

if !WinExist(Instr(MyEmail, "Outlook")) without success.

By using WinText it seems that I could eliminate the MyEmail variable entirely.

Thanks, CraigM

Code: Select all

^o::            ; ^ Ctrl o
{
;Check to see if Outlook is running or start it and wait for it to load,
;then resize if and center it afterwhich increase the size of the text
;in the preview reading pane.

MyEmail := "Inbox - [email protected] - Outlook"

if !WinExist(MyEmail)
    {
        Run "C:\Program Files\Microsoft Office\root\Office16\Outlook.exe"
        WinWaitActive MyEmail    ;, "Outlook"
    }
Else
    {
    If Instr(MyEmail, "Outlook")         ;Make sure it is Outlook
        {   
            WinActivate                       ;Use the window found by WinExist.
        }
    }

WinWaitActive MyEmail
WinMove , , 2830, 1750          ;Resize the Outlook window before centering it
CenterWindow(MyEmail)        ;Center the Outlook Window

;Move the Outlook cursor and magnify the reading pane.
WinGetPos &X, &Y, &WinWidth, &WinHeight, MyEmail
MouseClick "Left", WinWidth - 251, WinHeight - 41, 7      ;Ensure the the reading pane is at normal zoom
MouseClick "Left", WinWidth - 250, WinHeight - 41, 3      ;Zoom in three clicks
}
; End of ^o - open and center the Outlook window

;CenterWindow (ctrwin) Centers the passed window on the screen,
;less the area taken up by the Task Bar
CenterWindow(ctrwin)
{
    MonitorGetWorkArea , &WLeft, &WTop, &WRight, &WBottom  ;Actual Area - Task Bar
    WinGetPos , , &WinWidth, &WinHeight, ctrwin
    WinMove ((WRight - WinWidth)/2), ((WBottom - WinHeight)/2)
}

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: WinExist - WinText, How to use this parameter?

Post by boiler » 20 May 2022, 08:55

CraigM wrote: What I would like to do is something like:

if !WinExist(MyEmail, "Outlook")

Which I think should say is does a Window with "Outlook" in its title exist?

If that’s what you want it to say, you wouldn’t put a “not” (!) in front of it. And you can’t just shift parameters around. The WinTitle parameter is the first parameter. You would use SetTitleMatchMode to allow it to match anywhere in the title.

Code: Select all

SetTitleMatchMode 2
if WinExist("Outlook")

However, that’s not a great approach to ensuring Outlook is running because it will match any window that may happen to have Outlook in its title, which can be a browser window viewing a page about Outlook or something like that. This is a much better approach:

Code: Select all

if WinExist("ahk_exe OUTLOOK.EXE")

CraigM
Posts: 17
Joined: 21 Sep 2017, 13:07

Re: WinExist - WinText, How to use this parameter?

Post by CraigM » 20 May 2022, 09:37

SetTitleMatchMode 2 is set.

The statement If WinExist("Outlook") is problematic as Outlook is not found when it is on any other screen (calendar, contacts, notes).

I used the NOT (!) function as I was having trouble nesting If...Else statements and it seemed more logical.

I originally had and If{....} If {....} Else{....} and I could not figure out how to get the first If to belong to the Else. I found it more straight forward to Use the NOT (!) condition. It also made the code shorter and easier to read.

Thanks for your help. CraigM

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: WinExist - WinText, How to use this parameter?

Post by boiler » 20 May 2022, 11:57

CraigM wrote:
20 May 2022, 09:37
The statement If WinExist("Outlook") is problematic as Outlook is not found when it is on any other screen (calendar, contacts, notes).
What do you mean by "is not found when..."? It sounds like you are saying the opposite of what I said, which is it would be found if that word appears in the title of other windows. That's why I said to use this instead:

Code: Select all

if WinExist("ahk_exe OUTLOOK.EXE")

CraigM
Posts: 17
Joined: 21 Sep 2017, 13:07

Re: WinExist - WinText, How to use this parameter?  Topic is solved

Post by CraigM » 20 May 2022, 12:25

You are AWESUM, Thanks for all of your guidance.

The if WinExist("ahk_exe OUTLOOK.EXE") suggestion works great. I ended up NOTing (!) it for clarity sake. It seems bullet proof. It opens and centers Outlook no matter what pane (Contacts, Notes) you were last using. I never saw ahk_exe before, Thanks for that tip.

I added a sleep to allow Outlook to start loading which gave WinWaitActive "Outlook" something to wait for.

The CenterWindow function I changed the parameter to be optional. If no parameter is passed the active window is centered. This eliminated the function getting stuck until the Outlook email pane was active. That confused me a long time.

I hope others can benefit from this.

I learned a bunch here, THANKS for your kind inputs! CraigM

Code: Select all

;The script and function below work.  CraigM

^o::            ; ^ Ctrl o
{
;Check to see if Outlook is running or start it and wait for it to load,
;then resize and center it.  Then increase the size of the text in the
;preview reading pane.

if !WinExist("ahk_exe OUTLOOK.EXE")
    {
        Run "C:\Program Files\Microsoft Office\root\Office16\Outlook.exe"
        Sleep 4000                              ;Wait for Outlook
    }
Else
    {  
            WinActivate WinExist("ahk_exe OUTLOOK.EXE")
    }

WinWaitActive "Outlook"                         ;Wait for Outlook to load
CenterWindow()

;Move the Outlook cursor and magnify the reading pane.
WinGetPos &X, &Y, &WinWidth, &WinHeight, "A"
MouseClick "Left", WinWidth - 251, WinHeight - 41, 7    ;Set normal zoom level
MouseClick "Left", WinWidth - 250, WinHeight - 41, 3    ;Zoom in three times
}
;End of ^ o script


;CenterWindow Centers the Active window on the screen,
;less the area taken up by the Task Bar.  The Window
;to be centered is the Window passed in.  If nothing
;is passed in the Active window is Centered.

CenterWindow(ctrwin :=0)
{
    If ctrwin = 0
    {
        ctrwin := "A"   
    }
    MonitorGetWorkArea , &WLeft, &WTop, &WRight, &WBottom  ;Actual Area - Task Bar
    WinGetPos , , &WinWidth, &WinHeight, ctrwin
    WinMove ((WRight - WinWidth)/2), ((WBottom - WinHeight)/2)
}
;End of CenterWindow function

CraigM
Posts: 17
Joined: 21 Sep 2017, 13:07

Re: WinExist - WinText, How to use this parameter?

Post by CraigM » 20 May 2022, 12:38

boiler wrote:
20 May 2022, 11:57
CraigM wrote:
20 May 2022, 09:37
The statement If WinExist("Outlook") is problematic as Outlook is not found when it is on any other screen (calendar, contacts, notes).
What do you mean by "is not found when..."? It sounds like you are saying the opposite of what I said, which is it would be found if that word appears in the title of other windows. That's why I said to use this instead:

Code: Select all

if WinExist("ahk_exe OUTLOOK.EXE")
Why I used Not (!) syntax is because it made the If...Else statement work and to me it reads clearer.

I first tried not NOTing if you will and I ened up with an If...If...Else construct that I could not figure out how to make the Else belong to the first If. NOTing the statement eliminated the second If statement and seemed more condensed. It probably runs a pico second faster too.

Your guidance is always right on so I sincerely appreciate your suggestions.

Thanks, CraigM

smogmanus
Posts: 44
Joined: 27 Jul 2015, 12:44

Re: WinExist - WinText, How to use this parameter?

Post by smogmanus » 25 Sep 2023, 19:56

This format worked on vlc.exe you can try it on Outlook

Code: Select all

;~ DetectHiddenWindows On
SetTitleMatchMode, 2 ; Allow partial matching of window titles
^!a:: ; Use Ctrl+J to trigger the action (you can change this hotkey)

	If (!WinExist("ahk_exe vlc.exe"))    ; This format is the only one that worked for vlc.exe
	;~ if !WinExist("ahk_class Qt5QWindowIcon") or ("ahk_exe vlc.exe") or WinExist("ahk_class VLC media player")
        ;~ MsgBox, VLC not running.
		run, explorer.exe f:\dropbox\math

else

If (WinExist("ahk_class Qt5QWindowIcon") or ("ahk_exe vlc.exe") or WinExist("ahk_class VLC media player"))
{
    ;~ WinShow
    ; Activate VLC window
   WinActivate  ;("ahk_class QWidget") or WinExist("ahk_exe vlc.exe") or WinExist("ahk_class VLC media player")
    ; Send the keystrokes "6" and "]"
    sleep 1000
    send, =
	;~ This loop speeds up vlc.exe playback to 2.00x
    loop, 10
        {
        ;SetKeyDelay, [ Delay, PressDuration]
        sleep 50
    Sendinput, ]
        }


}
;~ DetectHiddenWindows Off
return
[Mod edit: [code][/code] tags added. Please use them yourself when posting code!]

Post Reply

Return to “Ask for Help (v2)”