Page 1 of 1

Image search + sound

Posted: 03 Aug 2021, 20:12
by ivar85
Hi guys!

Could you help me with a small script that will search for a image and play a sound if it's found?


Start button (any button)
Search for a image (could be two or three images)
If found
Play sound file
Loop sound
When image disappear then it stops playing that sound.

Hope someone can help.
Thanks!

Re: Image search + sound

Posted: 03 Aug 2021, 21:08
by mikeyww

Code: Select all

image = ....png
sound = ....wav
F3::
WinGetPos,,, width, height, A
Loop {
 Sleep, 200
 ImageSearch, x,, 0, 0, width, height, %image%
 SoundPlay, % ErrorLevel ? "" : sound, Wait
} Until (x = "")
Return

Re: Image search + sound

Posted: 04 Aug 2021, 04:04
by ivar85
Thank you !
Does AHK work that way I can just have any image in the AHK folder that ends in with a .png and this scripts search for it ?

Right now this script cant find any test image I make.
Could you help me a little more here ?

Code: Select all

image = ....png
sound = ....wav
F3::
WinGetPos,,, width, height, A
Loop {
 Sleep, 200
 ImageSearch, x,, 0, 0, width, height, %image%
 SoundPlay, % ErrorLevel ? "" : sound, Wait
} Until (x = "")
Return
[Mod edit: [code][/code] tags added.]

This is how my folder looks right now:
image.png
(27.82 KiB) Downloaded 108 times

Re: Image search + sound

Posted: 04 Aug 2021, 05:45
by mikeyww
You can loop through various images, but start with just one, so that you can get the script working. Change the first two lines: provide a full path to an image & sound file. The script searches with coordinates relative to the active window (can be changed), so ensure that the target window is active before you trigger the hotkey.

Re: Image search + sound

Posted: 04 Aug 2021, 14:25
by ivar85
That works thank you.

I get it working when I am browsing in chrome... It finds the image and makes the sound.

But I am trying to do it from a computer game and it seems that it´s not finding any pictures there.
Do you know why that could be ?

Re: Image search + sound

Posted: 04 Aug 2021, 14:52
by ivar85
Should I add *50 in it if the shades are not exactly the same ?

Where would I add it ?

edit***
This actually works !
So I have another question.
When the program finds the image... It start´s playing the sound.
So far so good.
Then when the picture disappears it´s like the script does not reload or something ?
I have to manually press F3 again to turn it on.
Is it possible to keep going without my input to refresh it ?
And also.. Would it be possible to search the whole screen even if the window is not active ?

Re: Image search + sound

Posted: 04 Aug 2021, 18:51
by mikeyww
You can search the screen if you change the second coordinate to A_ScreenWidth, A_ScreenHeight and change the CoordMode to screen. You can enclose any routine inside a Loop to execute it repeatedly.