WinMinimize PID after MsgBox - autohotkey

The following code opens notepad, then minimizes it. It works fine:
!^m::
Run, Notepad.exe,,USEERRORLEVEL MAX, PID
WinWait, ahk_pid %pid%
WinMinimize, ahk_pid %PID%
return
However if I add an MsgBox to display the PID before minimizing the notepad window, then the latter doesn't get minimized:
!^m::
Run, Notepad.exe,,USEERRORLEVEL MAX, PID
WinWait, ahk_pid %pid%
MsgBox OK %PID%
WinMinimize, ahk_pid %PID%
return
Why?
Sleeping didn't help:
!^m::
Run, Notepad.exe,,USEERRORLEVEL MAX, PID
WinWait, ahk_pid %pid%
MsgBox OK %PID%
Sleep, 2000 ; 2 seconds
WinMinimize, ahk_pid %PID%
return
I use Windows 7 SP1 x64 Ultimate and AHK 1.1.13.01 (October 11, 2013). I have the same issue with Version v1.0.48.05 and Version v1.1.15.02. All AHK versions I tried are 32-bit.
Edit:
!^b::WinMinimize, ahk_pid 9156 doesn't even work. (9156 being a valid PID)
Following MCL suggestion, I also unsuccessfully tried:
!^t::
PID = 11692
hwnd := WinExist("ahk_pid " pid)
WinMinimize, ahk_id %hwnd%
return

Use this code (it is tested and is working):
!^m::
Run, Notepad.exe,,MAX, PID
CheckWin:
IfWinExist, Untitled - Notepad
{
WinWait, Untitled - Notepad
MsgBox OK %PID%
WinMinimize, Untitled - Notepad
}
else
{
Goto, CheckWin
}
return
WinWait and WinMinimize commands cannot use Process ID (PID). In my code I am using WinTitle with these commands.
I am using IfWinExist command to be sure that notepad.exe is already launched and only after use WinWait and WinMinimize commands.

Related

Exit the script whenever i close spesific program || AutoHotKey

First, Here's the code
#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.
#SingleInstance Force
IfWinNotExist, ahk_exe VisualBoyAdvance.exe
{
Run, VisualBoyAdvance.exe, D:\Games\GAMEBOY ADVANCE (.gba)\GBA\emu
WinWait, ahk_exe VisualBoyAdvance.exe
WinActivate, ahk_exe VisualBoyAdvance.exe
return
}
WinActivate, ahk_exe VisualBoyAdvance.exe
sleep, 1000
MouseClick,,22,40
sleep,100
MouseClick,,116,257
sleep,100
MouseClick,,342,312
sleep,100
MouseClick,,179,16
return
Joy4::
MouseClick,,77,43
sleep, 100
MouseClick,,111,105
sleep, 100
MouseClick,,263,217
sleep, 100
MouseMove, 1380, 251
return
Joy1::
Send, {Shift Down}
Sleep, 100
Send, {F1}
Sleep, 100
Send, {Shift Up}
Return
Joy12::Send, {F1}
return
;LCtrl::
;ExitApp
It's basically open VisualBoyAdvance from the top of the recent list on VBA, i wanted to whenever i close the VBA, the script closed as well.. i've tried lot of method on the AutoHotKey Website, like :
Process, WaitClose, exwfile.exe
ExitApp
Return
Or
RunWait, calc
ExitApp
or
Run, C:\Program Files (x86)\Sonos\Sonos.exe
WinWait, ahk_exe Sonos.exe
SetTimer,Sonos,100
Sonos:
IfWinNotExist, Sonos
ExitApp
Return
but none of them works
please help me
thank you
Try using WinWaitClose followed by ExitApp:
// [...]
sleep,100
MouseClick,,179,16
WinWaitClose, ahk_exe VisualBoyAdvance.exe
ExitApp
Return
// [...]
IfWinNotExist, ahk_exe Sonos.exe

AHK Script using PostClick and PostMessage

I'm trying to write a simple AHK script to type a few characters and make a few clicks at specific coordinates in a background window, but I can't seem to get it to work, and I'm not finding a lot of information on how to properly use PostClick.
I was able to get this to work using Click when the window is active. Also tried using ControlClick, which seemed to work, except the click was happening on the location where the mouse was rather than the coordinates provided (and also only when the window was activated again).
Can I please have some help / advice on how to get this to work?
Here's my current script:
DetectHiddenWindows On
S:: ;Character to start the loop
pid = 19164 ;Application PID
ControlGet, clickVar, Hwnd , , ahk_pid %pid% ;Specify control for which program
BreakLoop = 0
Loop, 3 {
if (BreakLoop = 1)
break
Sleep 1000
PostClick(clickVar,1055,525)
{
ControlSend,, 3, ahk_pid %pid%
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, , %lParam%, , ahk_pid %pid% ;WM_LBUTTONDOWN
PostMessage, 0x202, , %lParam%, , ahk_pid %pid% ;WM_LBUTTONUP
}
Sleep 1500
}
E::
BreakLoop = 1
return
Thanks ahead of time for the help.
I am going to assume you are attempting to use this function.
I am unsure how this function works, but I think what you want is something like:
DetectHiddenWindows On
S:: ;Character to start the loop
pid = 19164 ;Application PID
ControlGet, clickVar, Hwnd , , ahk_pid %pid% ;Specify control for which program
WinGetTitle, clickTitle, ahk_pid %pid%
WinGetClass, clickClass, ahk_pid %pid%
BreakLoop = 0
Loop, 3
{
if (BreakLoop = 1)
break
Sleep 1000
PostClick(1055,525,clickClass, clickTitle)
ControlSend,, 3, ahk_pid %pid%
Sleep 1500
}
E::
BreakLoop = 1
return
PostClick(x, y, class, title)
{
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, 1, %lParam%, %class%, %title% ;WM_LBUTTONDOWN
PostMessage, 0x202, 0, %lParam%, %class%, %title% ;WM_LBUTTONUP
}
You need to place the function somewhere in the same file or at least accessible by the current file. You can do #include <script name here>.ahk at the top of your file and place that function as a new file in the same directory if you want.
The function you are trying to use takes in an x and a y for where to click in that window. Then it also takes in a class and title in order to know which window to actually use. Hope this works or helps you in some way.

Wait for previous process to finish before starting new one

I need to find a way in AHK to wait for a program to finish, before starting a new one.
Basically, I have a script that opens an application and inputs some parameters. The application then spends an unknown amount of time processing the input data.
Unfortunately, at the moment the ahk script ends before the application has finished processing, at which point the same ahk script is run again and does not work / interrupts the previous processing.
edit: (the ahk .exe is called using subprocess calls in Python)
is there a way or any methods to help with this?
For reference, the script:
#NoEnv
CoordMode, Mouse, Window
SendInput Mode Input
#SingleInstance Force
SetTitleMatchMode 2
#WinActivateForce
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1
if 0 < 2 ; The left side of a non-expression if-statement is always the name of a variable.
{
MsgBox, This script requires 2 incoming parameters but it only received %0%.
ExitApp
}
IfWinNotExist, ahk_exe photoscan.exe
{
Run, "C:\Program Files\Agisoft\PhotoScan Pro\photoscan.exe"
}
sleep, 200
WinActivate, ahk_exe photoscan.exe
sleep,5
WinMaximize, ahk_exe photoscan.exe
;Macro5:
Click, 476, 438, 0
SendInput {LControl Down}
SendInput {r}
Click, -56, 157, 0
WinActivate, Run Python Script ahk_class QWidget
sleep, 400
SendInput {LControl Up}
SendInput {LControl Down}
SendInput {a}
SendInput {LControl Up}
sleep, 400
SendInput {Backspace}
SendInput %1% ; 1st argument is the photoScan API scriptimages folder directory
SendInput {Tab}
SendInput {Tab}
sleep, 400
SendInput {LControl Down}
SendInput {a} ; 2nd argument is additional args (in our case, the projectName)
SendInput {LControl Up}
SendInput {Backspace}
SendInput %2% ; 2nd argument is the images folder directory & name of output log, model and texture
Sleep, 703
SendInput {Enter}
Click, 476, 438, 0
Return
You have:
IfWinNotExist, ahk_exe photoscan.exe
{
Run, "C:\Program Files\Agisoft\PhotoScan Pro\photoscan.exe"
}
sleep, 200
Which is set to start/launch the application if it is not running. Then a sleep to allow two tenths of a second for it to load (which is probably too small).
Instead of just a ‘sleep’ you have to ‘WinWait’ or ‘WinWaitActive’, found at this link:
https://autohotkey.com/docs/commands/WinWaitActive.htm
Like this sample:
Run, "C:\Program Files\Agisoft\PhotoScan Pro\photoscan.exe"
WinWaitActive, ahk_exe photoscan.exe, , 2
if ErrorLevel
{
MsgBox, WinWait timed out.
return
}
else
WinMinimize ; minimize the window found by WinWaitActive.
You may also have to use the window inspector to get the true name of the window/application/process name.

Typing on two windows at the same time

I want to be able to select both window 1 and window 2 and be able to type on both windows at the same time.
Whatever I type in window 1 will be typed in window 2 as well, for example: "hey everyone".
Being able to type on both windows at the same time, having both selected for typing. I have 2 notepad windows open. I want to type on the the first notepad window I opened, and what ever I type on window 1 will also be typed on window 2.
Sadly, I can't put up a image for you can see what I'm trying to say
WinGet, windowid, List, <Name of Window goes here>
#IfWinActive, <Name of Window goes here>
Space:: ; jump
KeyWait, Space, D
ControlSend,, {Space} , ahk_id %windowid1%
ControlSend,, {Space} , ahk_id %windowid2%
ControlSend,, {Space} , ahk_id %windowid3%
ControlSend,, {Space} , ahk_id %windowid4%
ControlSend,, {Space} , ahk_id %windowid5%
Return
I'm not sure if I even understand what you're trying to do... but have you tried using Input?
loop {
input, char, I L1 V M
ControlSend,, %char%, ahk_id %windowid1%
}
For example: Catching every single character sent in any %processID% window by Input and sending it to all remaining %processID% windows afterwards:
processID := "notepad.exe"
loop {
input, char, I L1 V M
winGet, active_win_ID, ID, A
winGet, active_win_exe, ProcessName, A
winGet, windowsToBeSentTo_IDs, List, ahk_exe %processID%
if active_win_exe != %processID%
continue
loop, %windowsToBeSentTo_IDs% {
_id := windowsToBeSentTo_IDs%a_index%
if _id != %active_win_ID%
controlSend,, %char%, ahk_id %_id%
}
}

How do I return from inside an if/else block in Autohotkey?

Below is the code snippet I used. If you are doing something in a browser, it should detect that you are currently in a browser and copy the URL to the clipboard. If you are not in a browser, then since there is no URL there is no question of doing that.
Regardless what browser I am in, the first MessageBox is always outputting the value 0. However, the MessageBox inside the function never gets activated, that is "No browser detected" message is never displayed. The value therefore can't be zero, yet when I am in notepad the if function still gets processed.
if (Retrieve_Browser_Type <> 0)
{
Send, {F6}
Send, ^{ins}
URL = %clipboard%
MsgBox, % Retrieve_Browser_type
MsgBox, URL copied: %URL%
}
Retrieve_Browser_Type() {
IfWinActive, ahk_class MozillaWindowClass return 1
IfWinActive, ahk_class Maxthon3Cls_MainFrm return 2
IfWinActive, ahk_class IEFrame return 3
IfWinActive, ahk_class Chrome_WidgetWin_1 return 4
MsgBox, No browser detected
return 0
}
It was a syntax mistake that thee compiler didn't detect.
Instead of:
IfWinActive, ahk_class MozillaWindowClass return 1
You ought to write:
IfWinActive, ahk_class MozillaWindowClass
return 1