autohotkey Restricting a script to only run a designated program - autohotkey

I use Teraterm for my terminal program. My issue is I cannot make my script run only when I am in the teraterm program. Is there a way to make it pop into teraterm if I am in a different app?
The script works great please share it to anyone who uses teraterm.
We always use the same server ip that shows up in the window title it contains 10.155.3.8. That text is always in the title.
How do I make it execute only in teraterm. I know this is an incredibly simple question but I have spend days looking around any help would be greatly appreciated.
If you have any basic tutorial sites I would greatly appreciate it.
I am a programming neophyte.
::ttwa:: ; change teraterm window name to current device or state.
SetTitleMatchMode, 2 ;// allow partial window title matches
#IfWinActive, 156.99.121.173, 156.99.121.173
send !e
sleep 10
send s
send {enter 100}
sleep 100
Send {click 3}
send !s
sleep 10
Send w
sleep 10
send %clipboard%
sleep 100
;send {backspace}
sleep 10
send {enter}
send !e s {enter}
send {enter 10}
Clipboard :=
return

There are several methods:
assign a hotkey which you would press to initiate the script:
^+F1::
.... send stuff
....
return
wait for the teraterm window to appear (WinWait) or become active (WinWaitActive):
winwait, teraterm ; change to the actual window qualifications
.... send stuff
....
return
run teraterm from your script, so you'll run the script icon instead of running teraterm directly:
run teraterm ; change to the actual path
winwait, teraterm ; change to the actual window qualifications
.... send stuff
....
return

Well. To me it looks like you have the answer already in your script.
From the Docs here: #IfWinActive
Creates context-sensitive hotkeys and hotstrings. Such hotkeys perform
a different action (or none at all) depending on the type of window
that is active or exists.
You simply have your script executing that requirement, out of order in which in needs to be.
SetTitleMatchMode, 2
#ifWinActive, 156.99.121.173, 156.99.121.173 ;Assuming this is correct
::ttwa:: ; change teraterm window name to current device or state.
send !e
sleep 10
send s
send {enter 100}
sleep 100
Send {click 3}
send !s
sleep 10
Send w
sleep 10
send %clipboard%
sleep 100
;send {backspace}
sleep 10
send {enter}
send !e s {enter}
send {enter 10}
Clipboard :=
return
As for recommendations on furthering your understanding of AutoHotkey, I strongly suggest starting with the official Tutorial.

Related

Autohotkey loop down key/folder-files

I want to add +1 to {Down} after starting the next loop
F8::
stop := 0
Loop, 13
{
Run, "C:\Users\USER\Desktop\docs"
Send {Home}
Sleep 1000
Send {Down} ;Here i want to add +1 after loop start again
Sleep 2000
Send {Enter}
Sleep 2000
Send !{F11}
Sleep 2000
Send {F5}
Sleep 2000
Send !{f4}
Sleep 2000
}until Stop
return
F9::Stop := 1
If someone has a different solution how to open files one by one and perform a given action and add it in a loop, please give me some suggestions.
OK i know the answer
Send, {Down %A_index%}
What i want to do. So I want to Open folder with some texts files (MS Word) and open first one, klick macro (alt+f11) then start macro (f5) and klick (alt + f4) to close macro window and next (alt+f4) to close word window then klick twice enter to accept save changes, then start loop again with next document (loop time = x documents in folder). But im done right now ;p
Anyway if u have any other idea how do things like this in future u can tell ;p
PS Add+1 dostn work ;p

Making AHK script in Notepad ++

Im trying to make an ahk script to try and simplify a one time use script. Pretty much im running over 1000 commands in a game one after one another .Currently I have something like this. I have all the commands in a single text file just not with any ahk coding.
.waypointadd 1 100234 40 -469
.waypointadd 2 99549 34 5
.waypointadd 3 100615 37 -160
.waypointadd 4 100817 27 -457
.waypointadd 5 100503.5 10.5 -647.5
.waypointadd 6 100494.5 10.5 -625.5
This goes on for a while. Im new to using expressions and such and am pretty much trying to make it to press enter, type the command, then press enter, then go to the next one. I obviously cant do this manually. I have tried using some basic replace expressions and stuff but not really sure how to do this.
In the end i would want it to look like this
send {enter}
send (command 1)
send {enter}
send {enter}
send (command 2)
send {enter}
you could bind it to a key like....
1::
loop, 1 {
send {enter}
send (command 1)
send {enter}
send {enter}
send (command 2)
send {enter}
}
or make a function when you a press a key. lmk if this helps or not
doSomething() {
send {enter}
send (command 1)
send {enter}
send {enter}
send (command 2)
send {enter}
}
1::
doSomething()
You could store all the commands in your clipboard (CTRL+C them) and then loop through all of them:
Loop, Parse, Commands, `n, `r ;split by linefeed, ignore carriage return
{
SendInput, % A_LoopField "{Enter 2}"
Sleep, 1000 ;however long you need
}
Loads of ways to get the commands into your script, I just went with loading them from your clipboard, should be pretty easy convenient to just copy the block of commands you want and then starting the script.
Then there's a parsing loop.
And then SendInput is used to send the current command follow by two presses of Enter.
Alternatively, if your game supports pasting from clipboard, it would be nice to just load your clipboard with whatever you want to send, and then sending a CTRL+V.
If the code with SendInput is going too fast, you can try switching over to normal Send and maybe even using SetKeyDelay to add even more delay between the keypresses.

What in this script is causing my window to minimize?

The goal of this simple script is to detect an idle interval when a specific program is in focus and then send a simple keystroke when that idle interval has passed. I'm running this script on 4 PC's and I'm getting unexpected results. Some PC's minimize the window when the script runs. Other PC's run it as expected. The script is identical on each PC.
I'm invoking this script by right clicking on the script (that is, not running a compiled exe version of it). Running it as administrator seems to achieve better results on some clients, on one it makes no difference and minimizes the window.
As stated, on some PC's the script works as intended. There are no error messages, it just causes my window to minimize. Nothing in that code, to my newb eyes, should cause the window to minimize.
#Persistent
SetTimer, Timer_check,3000
Timer_check:
if WinActive("ahk_exe gta5.exe")
{
if (A_TimeIdlePhysical > 31301 && WinActive("ahk_exe gta5.exe")) {
Gosub, keepActive
ToolTip, We're currently idle TimeIdle is %A_TimeIdle% and TimeIdlePhysical is %A_TimeIdlePhysical%
sleep 1000
ToolTip
}
if (A_TimeIdle < 31301) {
ToolTip
}
}
return
keepActive: ; keep active sub.
if WinActive("ahk_exe gta5.exe")
{
Send, {` down} ; Press the ` key to keep us active. It holds the key for 0.2 seconds.
Sleep 200
Send, {` up}
}
return```
You're trying to send the accent/backtick, which is default escape character in AHK (`). To fix this, send a different character or escape the escape character, like so:
Send, {`` down}
Sleep 200
Send, {`` up}
Without it being escaped, it just sends the down- and up-keys. The WinKey+down combination minimizes a non-maximized window and that may somehow be related to why you're seeing the game window occasionally minimized.
https://www.autohotkey.com/docs/commands/_EscapeChar.htm
Edit: Added script for testing
#Persistent
SetTimer, Timer_check, 3000
Timer_check:
If WinActive("ahk_exe gta5.exe") {
If (A_TimeIdlePhysical > 31301 && WinActive("ahk_exe gta5.exe")) {
Send , z
ToolTip, We're currently idle TimeIdle is %A_TimeIdle% and TimeIdlePhysical is %A_TimeIdlePhysical%
sleep 1000
ToolTip
}
If (A_TimeIdle < 31301)
ToolTip
}
Return

How do I add a delay between SendInput commands in AutoHotkey?

I have an AutoHotkey script using SendInput which sends MouseClick commands too quickly for my program to handle. My script will send a MouseClick to focus an input field, then start typing before the field finishes focusing.
I've tried using SetKeyDelay to make my script run a bit slower, but this doesn't work with SendInput.
Note: SetKeyDelay is not obeyed by SendInput; there is no delay between keystrokes in that mode. This same is true for Send when SendMode Input is in effect.
Documentation for SetKeyDelay
My current workaround is to use sleep commands after each input, but this is less than ideal.
SendMode Input
F1::
MouseClick, left, 61, 50 ; select title field
sleep 100 ; artificial delay to prevent misfocused inputs
SendInput %user_input%{Enter} ; enter job title
sleep 100 ; artificial delay
MouseClick, left, 67, 408 ; select job
sleep 100 ; artificial delay
Return
Ideally I would like a more elegant solution for adding a delay between each SendInput command without manually using a sleep command each time.
How can I add a delay between SendInput commands in AutoHotkey without repeatedly using sleep?
Try using SendPlay instead of SendInput.
This sends text and mouse clicks with a 100ms delay following each click
user_input := "hello world"
SetMouseDelay 100, Play
SendPlay {Click 61,50}%user_input%{enter}{click 67,408}
From the documentation for SendPlay.
SendPlay
Note: SendPlay may have no effect at all if UAC is enabled, even if the script is running as an administrator. For more information, refer to the FAQ.
Like SendInput, SendPlay's keystrokes do not get interspersed with keystrokes typed by the user. Thus, if the user happens to type something during a SendPlay, those keystrokes are postponed until afterward.
Although SendPlay is considerably slower than SendInput, it is usually faster than the traditional SendEvent mode (even when KeyDelay is -1).
SendPlay does not use the standard settings of SetKeyDelay and SetMouseDelay. Instead, it defaults to no delay at all, which can be changed as shown in the following examples:
SetKeyDelay, 0, 10, Play ; Note that both 0 and -1 are the same in SendPlay mode.
SetMouseDelay, 10, Play

AutoHotkey. How to make a script trigger every time certain window is active?

good people! I'm struggling with a question here.
I have the following script:
#If WinActive("ahk_class Chrome_WidgetWin_0") || WinActive("ahk_class Chrome_WidgetWin_1") || WinActive("ahk_class MozillaWindowClass") || WinActive("ahk_class IEFrame")
WinWaitActive, my_window_name_here - Opera, , 2
Sleep 1000
#NoEnv
SendMode Input
SetWorkingDir, %A_ScriptDir%
#SingleInstance force
Send some-text
Send {TAB}
Send some-text
Send {TAB}
Send some-text
return
It triggers when a certain window in a browser gets active. The problem is, it does it only ONCE. But I need it to trigger EVERY TIME that window/tab gets active. Is that somehow possible?
A quick googling reveals http://www.autohotkey.com/board/topic/85660-run-scripts-when-window-opensbecomes-active/ . Its essense is:
You have to check in a continuous loop:
There is no way to >run< a script when a program opens/is active.
The reason is that there is "nobody home" to say 'wake up'.
Instead, you can have a script that is running all the time, and one of its functions is to watch for your target window(s) and then call a subroutine to "do whatever".
The "WinTrigger" script linked to further in the thread uses #Persistent and SetTimer which is essentially the same approach but is neater.