Script restricting to one instance of a program + running in background

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bonkers01
Posts: 3
Joined: 18 Jan 2022, 21:35

Script restricting to one instance of a program + running in background

Post by Bonkers01 » 18 Jan 2022, 21:43

Hey i was struggling to create an Script. Im currently having 2 instances open of the same program but i do only wanna let the script target one of those 2 instances. I want this script to perform actions in this secondary Program-instance, while the targeted Instance is running in the background, but i guess its gonna happen if the target is restricted to the one instance anyway. Can anyone help with this issue?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Script restricting to one instance of a program + running in background

Post by swagfag » 18 Jan 2022, 23:46

i have no idea what ure saying

no code + vague description = no one cares about thread

Tensai
Posts: 29
Joined: 12 Dec 2019, 14:15

Re: Script restricting to one instance of a program + running in background

Post by Tensai » 18 Jan 2022, 23:50

I'm not sure what exactly you're asking. If you could provide sample code that would help.
However to uniquely target a specific app, you can use the windows unique hwnd.
Here is a small demo showing how to use unique Handles:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines -1
#SingleInstance Force

SetTitleMatchMode 2	; A window's title can contain WinTitle anywhere inside it to be a match.
Run notepad
Sleep 500
WinGet, Hwnd, ID , Notepad	; https://www.autohotkey.com/docs/commands/WinGet.htm#ID
Hwnd:="ahk_id " ID
; Alternatively...WinExist(WinTitle) also captures the unique Hwnd
Hwnd:="ahk_id " WinExist("Notepad")
WinActivate % Hwnd	; Hwnd is unique to this notepad instance.
ControlSend, Edit1, This Handle ( %Hwnd% ) is unique to this notepad instance.{Enter}, %Hwnd%
WinMinimize, % Hwnd
Run notepad,,,PID
Sleep 500
Hwnd2:="ahk_id " WinExist("ahk_pid " PID)
ControlSend, Edit1, This Handle ( %Hwnd2% ) is also unique to this specific notepad instance. {Enter}, %Hwnd2%

WinActivate % Hwnd
WinMove % Hwnd,,% (A_ScreenWidth//4),% (A_ScreenHeight//2) ,% (A_ScreenWidth//4), % (A_ScreenHeight//3)
WinActivate % Hwnd2
WinMove % Hwnd2,,% (A_ScreenWidth//2),% (A_ScreenHeight//2) ,% (A_ScreenWidth//4), % (A_ScreenHeight//3)

WinActivate % Hwnd
Send I {Enter}
WinActivate % Hwnd2
Send Can{Enter}
WinActivate % Hwnd
Send Easily {Enter}
WinActivate % Hwnd2
Send Switch {Enter}
WinActivate % Hwnd
Send Between {Enter}
WinActivate % Hwnd2
Send Each {Enter}
WinActivate % Hwnd
Send Instance {Enter}

ExitApp
Escape::ExitApp
Also if it's an app without a window (like another ahk instance) you can try setting

Code: Select all

DetectHiddenWindows On
Hope this helps! ;)

Bonkers01
Posts: 3
Joined: 18 Jan 2022, 21:35

Re: Script restricting to one instance of a program + running in background

Post by Bonkers01 » 19 Jan 2022, 06:13

Lets say it like that: I have 2 documents of Wordpad open. One is selected, one minimized. I want the AHK script to press buttons to the minimized one. Ive been trying out a bit with IfWinNotActive, but i got the feeling that not the right command for that, more for "performing actions if Wordpad is not open at all".

Code: Select all

*f3::
    toggle := !toggle
    if (toggle) {
        SetTimer, Spam_Space, 10
    } else {
        SetTimer, Spam_Space, Off
    }
return

Spam_Space:
	SendInput {Space}
return
That for now works for the selected window of Wordpad, but idk how to go on for checking for the unselected wordpad-window.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Script restricting to one instance of a program + running in background

Post by swagfag » 19 Jan 2022, 06:57

find the hwnds of both windows(use WinExist())
use ControlSend to send keys to the second window using the second window's hwnd

Bonkers01
Posts: 3
Joined: 18 Jan 2022, 21:35

Re: Script restricting to one instance of a program + running in background

Post by Bonkers01 » 19 Jan 2022, 07:48

Ok figured it out.
Thank you so much swagfag & Tensai for your help ^^

Post Reply

Return to “Ask for Help (v1)”