Image Search script.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Muligambia
Posts: 1
Joined: 10 Oct 2022, 01:03

Image Search script.

Post by Muligambia » 10 Oct 2022, 01:09

Hello. I have this script that searches for one imagine when i'm clicking F buttons. But i'd want to click one button and to search for two images, not one, with some little delay after the first one, and it's possible to assign to different keys? Not only f keys? Thanks.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance Force
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetMouseDelay 0
CoordMode, Mouse, Screen

F1::ImageSearchClick("1.png")
F2::ImageSearchClick("2.png")
F3::ImageSearchClick("3.png")
F4::ImageSearchClick("4.png")
F5::ImageSearchClick("5.png")
F6::ImageSearchClick("6.png")
F7::ImageSearchClick("7.png")
F8::ImageSearchClick("8.png")
F9::ImageSearchClick("9.png")
F10::ImageSearchClick("10.png")
F11::ImageSearchClick("11.png")
F12::ImageSearchClick("12.png")

ImageSearchClick(image_filepath, options := "") { ;2021/10/13 by AHKDev
    ;Parse options
    static default_options := {variation: 0, findclick: true, click_offset: [0.5, 0.5], click_options: "", click_return: true, timeout: 0}
    if (!IsObject(options)) {
        options := {}
    }
    for default_key, default_value in default_options {
        if (!options.HasKey(default_key)) {
            options[default_key] := default_value
        }
    }
    
    ;Read image size
    SplitPath, image_filepath, filename, filedir
    oS := ComObjCreate("Shell.Application")
    oF := oS.namespace(filedir ? filedir : A_WorkingDir)
    oFn := oF.parseName(filedir ? filename : image_filepath)
    size := StrSplit(oFn.extendedProperty("Dimensions"), "x", " ?" chr(8234) chr(8236))
    
    ;Mark start for timeout
    start := A_TickCount
    loop {
        ImageSearch, img_x, img_y, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, % (options.variation ? "*" options.variation " " : "") A_WorkingDir "\" image_filepath
        ;tooltip %ErrorLevel% looking for %image_filepath%
        if (!ErrorLevel) {
            if (options.findclick) {
                if (options.click_return) {
                    MouseGetPos, m_x, m_y
                }
                BlockInput, MouseMove
                Click % Round(img_x + (size[1] * options.click_offset[1])) " " Round(img_y + (size[2] * options.click_offset[2])) (options.click_options ? " " options.click_options : "")
                if (options.click_return) {
                    MouseMove, % m_x, % m_y
                }
                BlockInput, MouseMoveOff
            }
            return {x: img_x, y: img_y}
        } else if (A_TickCount - start > options.timeout) {
            return false
        }
    }
}

User avatar
usnake
Posts: 9
Joined: 22 Sep 2022, 02:41

Re: Image Search script.

Post by usnake » 10 Oct 2022, 02:48

Maybe you can try findtext() written by feiyue

It's a powerful script that can completely replace ahk's native findimage

Post Reply

Return to “Ask for Help (v1)”