Wait for previous process to finish before starting new one - subprocess

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.

Related

Autohotkey, problems redefining keypress down state of mousekey

I am using the LButton (mouse left) in a keybind as a prefix key. I got it to work, problem is I now need to redefine the LButton as whatever it was in it's natural state...in Autohotkey's terms.
I read this: https://www.autohotkey.com/docs/commands/GetKeyState.htm.
And cameup with the following code, but it's not working at all the way I thought it would. Simply put, you can use $LButton::Send {Click Left} to emulate the basic mouse click. The problem is when you hold the button/key down, nothing happens. I thought the code to emulate, or define the 'pressed down' behaviour would be readily available, but what I've found isn't working.
$LButton::
if (GetKeyState("LButton", "P"))
Send, {Click Left Down} ;tried variants with {Click Left} etc alrdy
else
Send, {Click Left Up}
return
For person in comments:
LButton & ~RButton::
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
$LButton:: ;no idea what this shud be
SendInput {Click Left}
;Send, {Click Left Down}
;KeyWait, LButton
;Send, {Click Left Up}
return
RButton::
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
Not sure if I understood this correctly, but you want holding LButton, then clicking the RButton to run this code:
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
And just clicking RButton should run that code as well?
And if LButton is just pressed normally (not in combination with RButton) it should function as normal?
Well this would be it:
~LButton & RButton::
RButton::
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep, 130
Send, 1
return
Basically just making use of the ~ modifier.
Although I can't speak for Send, 1{Click Right}{Click Left}{Click Right}{MButton}.
I don't know what it's supposed to do, but maybe/hopefully it's doing the right thing.
Pressing w or something that you define should do the job
w::
Send, {Click down}
Send, {Blind}1{Click Right}{Click}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
Anyone looking for something similar could try in this direction
#SingleInstance force
#warn
r::Reload
x::ExitApp
w::
Send, {Click down}
Send, {Blind}1{Click Right}{Click}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
just Pressing w should do the job
Alternately
, the below one will do the same
LButton::Send, {Click down}
RButton::
if GetKeyState("LButton", "P")
{
Send, {Blind}1{Click Right}{Click}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
}
else
Send, {Click Right}
return

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

I cant seem to run a AHK script ONLY when I insert the sd card

I have been looking into this for a few days ♫ And i still haven't found what I'm looking for♫ The problem is that the script runs when I insert the sd card AND when I take it out. Any suggestions to how I could get it to just run on insert?
Code:
#Persistent
OnMessage(0x219, "notify_change")
notify_change(wParam, lParam, msg, hwnd)
{
send, <#{right}
sleep, 100
MouseClick, left, 726, 615
send, ^a
sleep, 100
send, ^c
sleep, 100
GroupAdd, ExplorerGroup,,ShellView
GroupActivate ExplorerGroup
send, ^v
return
}

script won't run a section of code

I have this small script that is supposed to open up an app and type in some letters. However, for some reason the script won't run a section of code. It gets up to winMaximize and then stops. But if the app is already open then the section DOES run?? Not sure if I'm doing anything wrong, ideas?
#NoEnv
CoordMode, Mouse, Window
SendMode 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
{
RunWait, "C:\Program Files\Agisoft\PhotoScan Pro\photoscan.exe"
}
WinActivate ; Automatically uses the window found above.
sleep,5
WinMaximize
;Macro5:
Click, 499, 426, 0
Click, 497, 427, 0
Click, 496, 427, 0
Click, 493, 428, 0
Click, 492, 429, 0
Click, 487, 431, 0
Click, 485, 433, 0
Click, 482, 435, 0
Click, 481, 435, 0
Click, 480, 435, 0
Click, 479, 436, 0
Click, 478, 436, 0
Click, 477, 437, 0
Click, 477, 438, 0
Click, 476, 438, 0
Send, {LControl Down}
Send, {r}
Click, -56, 157, 0
WinActivate, Run Python Script ahk_class QWidget
Send, {LControl Up}
Send, {LControl Down}
Send, {LControl Up}
Send, {LControl Down}
Send, {a}
Send, {LControl Up}
Send, {Backspace}
Send, {%1%} ; 1st argument is the images folder directory
Send, {Tab}
Send, {Tab}
Send, {LControl Down}
Send, {%2%} ; 2nd argument is additional args (in our case, the projectName)
Send, {LControl Up}
Send, {Backspace}
Send, {b}
Sleep, 703
Send, {Enter}
Click, 476, 438, 0
Return
By ommitting any wintitle parameters, you are calling winactivate and winmaximize on the "The Last Found Window". I think winMaximize simply doesn't execute because there was just no found window (it is called "the last found window", and not "the window which was found last, or maybe it wasn't").
Afaican see, your stack trace is
>
> IfWinNotExist, ahk_exe photoscan.exe ; does not exist. -> last found window: NONE
> RunWait, "C:\Program Files\Agisoft\PhotoScan Pro\photoscan.exe"
> ; still: last found window: NONE
> WinActivate / WinMaximize ; called on NONE
Not sure why winActivate terminates though. Either way, if you add ahk_exe photoscan.exe to winactivate and winmaximize, you should be fine

Manipulate a webpage to include a watermark

I work with a web application that works only on Mozilla Firefox.
A particular task generates a page to print a letter to be mailed.
After printed the letter, my colleagues should stamp the brand of post office and manually write the send date on which the letter is sent.
Is there any way to manipulate the display of the page to include a line in HTML with the stamp image before printing?
Any ideas?
Thank you very much.
Is there any reason it needs to be done via autohotkey? If you have access to the code, you could just add a print stylesheet that overlays the stamp image.
SmashingMagazine has a fantastic guide to print stylesheets:
In essence, all you have to do is add this to your existing stylesheet:
#media print {
//css to add the stamp image overlay here
}
Thanks for feedback.
I figured it out.
That's my code:
#NoEnv
#Persistent
#SingleInstance force
SendMode Input
SetWorkingDir %A_ScriptDir%
Gui, Add, Button, gCorreios, Correios
Gui, Show
Return
Correios:
Sleep 100
Send, {ALT DOWN}{ESC}{ALT UP}
Send, {CTRLDOWN}f{CTRLUP}
Sleep 10
Send, Postado{ESC}
Sleep 10
Send, {SHIFTDOWN}{END}{SHIFTUP}{CTRLDOWN}c{CTRLUP}
ClipWait
Sleep 10
DataDaPostagem=%Clipboard%
ClipBoard =
Sleep 10
StringReplace, DataDaPostagem, DataDaPostagem, postado,, All
StringReplace, DataDaPostagem, DataDaPostagem, em,, All
StringReplace, DataDaPostagem, DataDaPostagem, :,, All
StringReplace, DataDaPostagem, DataDaPostagem, `,,, All
StringReplace, DataDaPostagem, DataDaPostagem, %A_SPACE%,, All
Sleep 10
Send, {SHIFT DOWN}{F4}{SHIFT UP}
WinWait, Scratchpad,
IfWinNotActive, Scratchpad, , WinActivate, Scratchpad,
WinWaitActive, Scratchpad,
Send, {CTRLDOWN}a{CTRLUP}{DEL}
Send, var correios=document.createElement("div");{ENTER}
Send, correios.id="correios";{ENTER}
Send, correios.style="background:url(http://i62.tinypic.com/169hxls.png) no-repeat; display:block; height:37px; padding:135px 0 0 0; position:absolute; right:0; text-align:center; top:0; width:171px; z-index:-1";{ENTER}
Send, correios.innerHTML="
Send, %DataDaPostagem%
Send, ";{ENTER}
Send, document.getElementsByTagName("body")[0].appendChild(correios);{ENTER}
Send, {CTRLDOWN}r{CTRLUP}
Sleep 10
Send, {CTRLDOWN}w{CTRLUP}
WinWait, Alterações não salvas,
IfWinNotActive, Alterações não salvas, , WinActivate, Alterações não salvas,
WinWaitActive, Alterações não salvas,
Send, d
Sleep 10
return
~^s::
IfWinActive, %A_ScriptName%
{
SplashTextOn,,,Updated script,
Sleep, 200
SplashTextOff
Reload
}
return