Exit the script whenever i close spesific program || AutoHotKey - 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

Related

open program from windows Tray

I made a script that simply opens or activate specific app.
it works great but I have one issue, when the app minimized to windows tray.
the Hotkey activate the Else statement part so it creates another instance of the same app.
!a::
If WinExist("ahk_exe Orzeszek Timer.exe")
{
WinActivate, ahk_exe Orzeszek Timer.exe
}
Else {
Run, "D:\Portable\PortableApps\Orzeszek Timer\Orzeszek Timer.exe"
}
Return
I need help with script to trigger the IF statement part in case the app appears in system tray.
Much Thanks in advance)
Try this
!a::
Process, Exist, Orzeszek Timer.exe
If (Errorlevel != 0) ; is running
{
WinGet, WinState, MinMax, ahk_exe Orzeszek Timer.exe
If (WinState = "") ; is minimized to tray
SendInput, #bo{Enter} ; Win+b activates the tray, o marks the icon of Orzeszek Timer
else
WinActivate, ahk_exe Orzeszek Timer.exe
}
else ; is NOT running
Run, "D:\Portable\PortableApps\Orzeszek Timer\Orzeszek Timer.exe"
return
If the program has its own hotkey to restore the window, use that hotkey instead of #bo{Enter}.
EDIT:
If SendInput, #bo{Enter} is too fast to restore the program, add a sleep between the keys to send:
...
If (WinState = "") ; is minimized to tray
{
SendInput, #b ; Win+b activates the tray
; WinWaitActive, ahk_class Shell_TrayWnd
Sleep, 300
SendInput, o ; o marks the icon of Orzeszek Timer. Try first of all manually which letter marks the icon
Sleep, 300
SendInput, {Enter}
}
...
EDIT 2:
I downloaded that small portable app and this is working on my system:
!a::
Process, Exist, Orzeszek Timer.exe
If (Errorlevel != 0) ; is running
{
WinGet, WinState, MinMax, ahk_exe Orzeszek Timer.exe
If (WinState = "") ; is minimized to tray
; SendInput, #bot{Enter} ; OR:
SendInput, #b{Enter}{Up}ot{Enter}
}
else ; is NOT running
Run, "D:\Portable\PortableApps\Orzeszek Timer\Orzeszek Timer.exe"
WinWait, Orzeszek Timer,, 10
If (!ErrorLevel)
{
WinActivate, Orzeszek Timer
WinWaitActive Orzeszek Timer,, 5
If (!ErrorLevel)
MouseMove, 150, 80, 0
}
return
EDIT 3:
If you don't have the option "Always show all icons in the notification area" in the Settings enabled, try replacing
SendInput, #bot{Enter}
with
SendInput, #b{Enter}{Up}ot{Enter}
user3419297 much thanks for your script it works great. I Modified it a little to be open the app.
!a::
Process, Exist, Orzeszek Timer.exe
If (Errorlevel != 0) ; is running
{
WinGet, WinState, MinMax, ahk_exe Orzeszek Timer.exe
If (WinState = "") {
SendInput, #bo{Right}{Enter}
Sleep, 500
CoordMode, Mouse, Window
MouseMove, 150, 80, 0
}
else
WinActivate, ahk_exe Orzeszek Timer.exe
}
else ; is NOT running
Run, "D:\Portable\PortableApps\Orzeszek Timer\Orzeszek Timer.exe"
return
only one minor disadvantage the script depend on app been place first
in windows tray list.
of course I could always redefine {Right x} placement but is there more efficient way to find automatically app placement in tray?
finally works! the only missing part in script was {Up} key as pointed on edit 3 for system tray to become searchable. so now I had no problem to find the exact key. in my case it was
SendInput, #b{Enter}{Up}tt
Sleep, 100
SendInput, {Enter}
I Really don't know how to thank you. you helped me alot )

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
}

Autohotkey If WinActive

Here is the script I am using to use the key 1 to send 1 if active window is 3ds max or else go back in explorer and browser. My code only works if the window is a newly opened unsaved 3ds max file. How do I get it to work with any 3ds max file? I know the ahk_class of max is 3DSMAX.
1::
if WinActive("Untitled - Autodesk 3ds Max 2018")
{
Send, 1
}
Else
{
Send, {Alt Down}{Right Down}{Right Up}{Alt Up}
}
Return
The official documentation says this
if WinActive("ahk_class Notepad") or WinActive("ahk_class" . ClassName)
but neither this works
if WinActive("ahk_class 3DSMAX") or WinActive("ahk_class" . ClassName)
Nor this works
if WinActive("ahk_class 3DSMAX")
Nor this works
WinActive("ahk_class" . 3DSMAX)
I cannot use
#IfwinActive ahk_class 3DSMAX
even though it works because my script is using intercept to use the macro keys on Razer Naga Hex, to send separately from my keyboard keys and it will get very complicated once I start adding all of the software I use.
Here is the full script
#HotkeyInterval 20000 ; This is the default value (milliseconds).
#MaxHotkeysPerInterval 2000
#SingleInstance force
#Persistent
#if (getKeyState("F22", "P"))
F22::return
;=========================================
1::
if WinActive("Untitled - Autodesk 3ds Max 2018")
{
Send, 1
}
Else
{
send, {Alt Down}{Right Down}{Right Up}{Alt Up}
}
return
;=========================================
2::
send, {Ctrl Down}z{Ctrl Up}
return
;========================================
3::
send, {Ctrl Down}y{Ctrl Up}
return
;=========================================
4::
send, {Alt Down}{Left Down}{Left Up}{Alt Up}
return
;=========================================
5::
return
;=========================================
6::
return
;==========================================
7::
return
;==========================================
#if
return

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.

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