Passing window title to function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
klayman
Posts: 2
Joined: 22 May 2018, 01:42

Passing window title to function

22 May 2018, 02:04

Hello,

I'm trying to write an universal function for multiple similar tasks (run-or-raise an app). Here is an example of working code:

Code: Select all

SetTitleMatchMode, 2

#o::
AppsKey & o::
if WinActive( "Opera" )
    {
    ; cycle through open windows
    WinGetClass, CurrentActive, A
    WinGet, Instances, Count, ahk_class %CurrentActive%
    If Instances > 1
        {
        WinSet, Bottom,, A
        }
    WinActivate, ahk_class %CurrentActive%
    }
else
    {
    ; switch to the window or launch a new instance
    if WinExist( "Opera" )
        {
        WinActivate
        }
    else
        {
        Run, "C:\Program Files\Opera\launcher.exe" --disable-update
        }
    }
return
However, when I move this code to a function and pass the app name and binary path as arguments, it stops working (it launches new instances even if the app is already running):

Code: Select all

SetTitleMatchMode, 2

run_or_raise( name, exe )
{
    global CurrentActive
    global Instances
    if WinActive( "%name%" )
        {
        ; cycle through open windows
        WinGetClass, CurrentActive, A
        WinGet, Instances, Count, ahk_class %CurrentActive%
        If Instances > 1
            WinSet, Bottom,, A
        WinActivate, ahk_class %CurrentActive%
        }
    else
        {
        ; switch to the window or launch a new instance
        if WinExist( "%name%" )
            {
            WinActivate
            }
        else
            Run, %exe%
        }
    return
}

#o::
AppsKey & o::
    run_or_raise( "Opera", """C:\Program Files\Opera\launcher.exe"" --disable-update" )
return
What's wrong with my function?
Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Passing window title to function

22 May 2018, 02:46

Hallo,
only a try:
Replace:

Code: Select all

WinActive( "%name%" )
WinExist( "%name%" )
with

Code: Select all

WinActive(name)
WinExist(name)
klayman
Posts: 2
Joined: 22 May 2018, 01:42

Re: Passing window title to function

22 May 2018, 02:53

Exactly! Thank you very much.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AHK_user, Google [Bot], sharonhuston and 222 guests