Bart
Bart

Reputation: 1

How do I prevent AHK from opening a duplicate window?

In AutoHotKey, I've created a script that starts on bootup, which calls the "Productivity.ahk"-script whenever I press ctrl+p. "Productivity.ahk" opens windows with Todoist and Toggl for me. This code works perfectly.

; Toggl
Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome_proxy.exe"  --profile-directory=Default --app-id=emhlbipdbnglohkhcmimglnngjlfoehc

; Todoist
Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome_proxy.exe"  --profile-directory=Default --app-id=geflgmhlcjjamienpgojogcbpjddhcme


WinWait, Toggl
WinRestore,
WinMove, , , 803, 0, 1126, 1080

WinWait, Todoist
WinRestore,
WinMove, , , -7, 0, 826, 1080

return

What I would like to do, though, is to have the script first check if there might be an older instance of those windows open already. I've tried to achieve this with the "if" and "else" expressions, but it doesn't work. It still opens new instances either way. Is there someone out there who has a clue on why it doesn't behave the way I expected?

; Todoist
if WinExist, Todoist
{
    WinWait, Todoist
    WinRestore,
    WinMove, , , -7, 0, 826, 1080
}
else 
{
    Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome_proxy.exe"  --profile-directory=Default --app-id=geflgmhlcjjamienpgojogcbpjddhcme
    WinWait, Todoist
    WinRestore,
    WinMove, , , -7, 0, 826, 1080
}


; Toggl
if WinExist, Toggl
{
    WinWait, Toggl
    WinRestore,
    WinMove, , , 803, 0, 1126, 1080
}
else 
{
    Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome_proxy.exe"  --profile-directory=Default --app-id=emhlbipdbnglohkhcmimglnngjlfoehc
    WinWait, Toggl
    WinRestore,
    WinMove, , , 803, 0, 1126, 1080
}

return

Upvotes: 0

Views: 566

Answers (2)

wetin
wetin

Reputation: 438

I agree here with Smile, it seems the Window Title is likely not correctly specified. At least it doesn't look right to me. You're launching Chrome and calling for a window title named "Toggl" and "Todoist"

The window is likely going to be in the format: Browser tab name - Site title - Google Chrome so for this StackOverflow page, the URL looks like: autohotkey - How do I prevent AHK from opening a duplicate window? - Stack Overflow - Google Chrome

Right click the script in the system tray and click > window spy. Then select the program you wish to detect. It will populate with the information you need. Window Spy

Upvotes: 0

Smile
Smile

Reputation: 29

If you want to use old syntax of WinExist, don't add space after if

Correct like this: ifWinExist, Todoist.

Also check if you are using a correct window name, I suggest you to use Windows Spy to get the title of that window or to verify you are using the correct title.

Windows Spy utility should be found in installation directory of AutoHotkey.

Upvotes: 0

Related Questions