George
George

Reputation: 4674

How to pass window title to user function in AutoHotKey

I want to pass the window title into a function I wrote in AutoHotKey, is window title WinTitle a string? I have 4 window titles, and I need to pass them to the same function.

Extract(my_window_title) {
; Wake and select the correct window to be in focus
WinWait, my_window_title, 
IfWinNotActive, my_window_title, , WinActivate, my_window_title, 
WinWaitActive, my_window_title,
; ... do a bunch of things
}

I call the function like this

title1 = "Some title"
Extract(title1) 

and I also tried putting % in all the variables

Upvotes: 0

Views: 1827

Answers (2)

Forivin
Forivin

Reputation: 15488

Yes WinTitle is basically a string. Check out your Autohotkey-folder, there should be a file called "AU3_Spy.exe". Use it to find the window titles.

And as Elliot DeNolf already mentioned, you made some mistakes with variables. You should also take another look at the syntax of IfWInNotActive. This should work:

Extract(my_window_title) {
    ; Wake and select the correct window to be in focus
    WinWait, %my_window_title%
    IfWinNotActive, %my_window_title%
    {
        WinActivate, %my_window_title%
        WinWaitActive, %my_window_title%
    }
    msgbox, %my_window_title%
    ; ... do a bunch of things
}

title1 = MyWindowTitle
Extract(title1) ;functions always expect variables, no percent-signs here

Upvotes: 2

Elliot DeNolf
Elliot DeNolf

Reputation: 2999

There are a few things that look like they are causing an issue in your script.

When assigning a string value and using =, quotes are not needed. If you assign the value using :=, then you need the quotes. These 2 lines are equivalent:

    title1 := "Some Title"
    title1 = Some Title

Once these values are called via a function ie. Extract(title1), % symbols must be used (as you mentioned at the end of your question). This can be called in 2 ways:

    WinActivate, %my_window_title%
    WinActivate, % my_window_title

If the title is invalid, your script will wait indefinitely on WinWait and WinWaitActive. I would recommend using a timeout value and then checking ErrorLevel to see if it was successful or not.

Upvotes: 1

Related Questions