How do I update the auto hot keys config once saved from a script? - autohotkey

I have a shortcut to open the current running script in a text editor and then save it and close it. Upon closing, how do I update ahk to use the new updated settings?
This is what I have:
IfWinExist, test.ahk ahk_exe notepad.exe
{
WinActivate
Sleep, 100
Send, ^s
Sleep, 200
WinClose
}
else
MsgBox, window not found
app1Open=false
Reload

If you want to restart the script itself (that is, from its own process), use the Reload command.
Otherwise, if you want to restart the script from another process, you have to close it and then run the script again. In this case, you can either kill the script's process and run it again (not nice!). Or you can tell the script to gracefully Reload, for example in one of these two ways:
Inter-process communication: OnMessage() for the receiving process and PostMessage/SendMessage in the sending process
Defining a simple hotkey in your receiving script and sending that hotkey from another process

As MCL mentioned, use Reload. There is no need to close the script. Here is an example:
^!#r:: ; Save and Reload the script with Alt+Ctrl+Win+r
IfWinExist, SciTE4AutoHotKey
{
WinActivate
Sleep, 100
Send, ^s
Sleep, 300
}
Reload
Return

Related

Lost focus in browser when using clipboard for search query [autohotkey]

I am using a simple script to run a google/wikipedia/etc search using a hotkey, unfortunately after the search result appears in a new tab, I have to click because the tab is not on focus, although the browser windows is on focus. I tried to add a WinActivate but it's not working. This script used to work as expected before a new OS installation. Why is this script making lose focus on the browser?
Here's the script
^+g::
{
Send, ^c
sleep 200
Run, https://www.google.com/search?hl=en&q=%Clipboard%
sleep 50
WinActivate, ahk_exe waterfox.exe
}
Return
I don't know why, but it looks like increasing the delay between the Run and the WinActivate seems to fix it.
^+g::
{
Send, ^c
sleep 200
Run, https://www.google.com/search?hl=en&q=%Clipboard%
sleep 500 ;Up from 50, you might be able to fine-tune this number based on your computer's speed
WinActivate, ahk_exe waterfox.exe
}

AutoHotKey - Run a program once send several times

I am pretty new to AutoHotkey, but I managed it to start my desired program and send an Enter-Key to it, but the probleme here is, the program should only start once and if started it should only receive that enter key, when I press the key stroke again and again it should only send that enter key.
And the program should should stay in the background and not focus after it receives the enter key.
My Code:
#n::
Run F:\V..c.exe
Send {enter}
return
Detect if the process exists and start the program minimized, then wait for its window to appear.
#n::
process, exist, PROGRAM.EXE
if (errorlevel = 0) {
run, d:\program.exe, , min
winwait, ahk_class PROGRAM_WINDOW_CLASS
}
controlSend, , {Enter}, ahk_class PROGRAM_WINDOW_CLASS
;or use the line below
;controlSend, ahk_parent, {Enter}, ahk_class PROGRAM_WINDOW_CLASS
return
Replace PROGRAM.EXE with the executable name of your program and PROGRAM_WINDOW_CLASS with the window class as seen in the Autohotkey Window Spy utility available in Start menu or in the folder of the Autohotkey (AU3_Spy.exe) or in the right click menu of the Autohotkey tray icon.
Instead of running the program minimized it's also possible to use SW_SHOWNOACTIVATE flag of shellExecute, so you can replace the run, d:\program,, min with this:
dllCall("shell32\ShellExecute", uint,0, uint,0, str,"d:\program.exe", uint,0, uint,0
,uint,SW_SHOWNOACTIVATE:=4)
you could try:
hasran := false
#n::
if (!hasran) {
Run F:\V..c.exe
hasran := true
}
Send {enter}
return
It wont check if that windows exists, but it will only run the program once. then you can navigate to that program and it will only hit the enter key. (if that program is not gui I dont think you can send key events to it)

Writing script for Autohotkey for invoking 'Shift+F5' keystrokes

I have to run few applications like browser and ticketing tool. But, I need to continuously press shift+F5 keystrokes for reloading the pages of my browser and ticketing tool.
Can I use any script through autohotkey for invoking 'Shift+F5' keys repeatedly for every 20 seconds regular interval?
Can anybody please help me with writing the script for autohotkey for invoking 'Shift+F5' keystrokes?
I assume that you want to be able to turn this feature on/off
#SingleInstance Force
#installKeybdHook
#Persistent
#F5:: ; [Win]+[F5] to start timer
SetTimer, RefreshPage, 20000
TrayTip, Refresh, Started, 1
ToolTip, Refresh Active
Return
+#F5:: ; [Shift]+[Win]+[F5] to stop timer
SetTimer, RefreshPage, Off
TrayTip, Refresh, Stopped, 1
ToolTip
Return
RefreshPage:
Send, +{F5}
Return

Hotkey to restart autohotkey script?

Say I have an autohotkey script C:\path\to\my\script running. Is there a way to define a hotkey that re-starts it?
In order to prevent duplicate instances, I normally do not re-launch a script but use the build-in function Reload. I launch this with Ctrl+Win+Alt+R and use Ctrl+Win+Alt+E to edit the main AHK script.
^#!r::Reload
Actually, my script looks like this:
^#!r::
Send, ^s ; To save a changed script
Sleep, 300 ; give it time to save the script
Reload
Return
^!#e::Edit
As a matter of fact, all the way at the top of my script I have this to give me a visual and audio indication that the script was restarted:
#SingleInstance Force
#installKeybdHook
#Persistent
Menu, Tray, Icon , Shell32.dll, 25, 1
TrayTip, AutoHotKey, Started, 1
SoundBeep, 300, 150
Return
Make a hotkey that runs a script, which in this case is the same script and then exit.
somehotkey::
Run, C:\path\to\my\script.ahk
ExitApp
return
I found this to be the safest option of them all, because it takes care that the correct script is reloaded when you have multiple scripts running simultaneously, which was a recurring issue for me. The combination of the following also ensures that only one instance of a script will ever run at a time. The ScriptFullPath variable includes the name of the script.
#SingleInstance Force ;put this at the top of the script
^r::run, %A_ScriptFullPath%

How can I make the script run automatically, without a hotkey being defined?

I want to create a "PolyEdit" script.
Below is the script.
!M::
IfWinActive, ahk_class TMainForm
{
sleep 2000
Send Now is the time
Return
}
The purpose of the script is to:
Send keystrokes and mouse clicks to my default program for opening text files.
That default program is called "PolyEdit".
But I need the script to run without a hotkey being defined.
Right now, in it's present form, with a hotkey defined, it runs just fine.
My question is:
How can I make the script run automatically, without a hotkey being defined?
As Armin already wrote, use #Persistent. Also, If you want to create hotkeys that are only active when a specific application is in focus you can do the following: In this case the script will no longer execute on startup, only when you press the hotkey though...
#Persistent
#SingleInstance
#IfWinActive, ahk_class TMainForm
!M::
sleep 2000
Send Now is the time
Return
!n::SoundBeep, 500, 500
!o::MsgBox, OK
#IfWinActive
This way all 3 (dummy) hotkeys will only be active when your application is in focus! You can define the same hotkeys for another application, just repeat the code but use the ID if the other application in the #IfWinActive, ahk_class TMainForm line.
If you want to send a message every 2 seconds when your application is active do the following:
#Persistent
#SingleInstance
SetTimerMatchMode, CheckApp, 2000
Return
CheckApp:
IfWinActive, ahk_class TMainForm
{
Send, Now is the time
}
Return
If you want to execute a script every time you (re)activate (put in focus) your application (so not every two seconds) then use the following:
#Persistent
#installKeybdHook
#SingleInstance
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return
ShellMessage( wParam )
{
If (wParam = 4)
{
IfWinActive ahk_class TMainForm
{
Send, Now is the time
}
}
}
Return
Take a look at #Persistent which will keep script running.
If this directive is present anywhere in the script, that script will stay running after the auto-execute section (top part of the script) completes