Create hotstrings from variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Create hotstrings from variables

09 Aug 2017, 14:10

Hi,

Is is possible to create hotstrings with the abbreviations and expanded value stored in variables? I need to store hotstrings in an ini file and retrieve/enable them from the ini file. I would need a command similar to the "Hotkey" command as illustrated in the example below. Without such a "Hotstring" command, is there another way to obtain the same result?

This is a working example. It will create an ini file named with with the script's filenname.

Code: Select all

#NoEnv
#SingleInstance force
#KeyHistory 0

objHotkeyCommands := Object()

StringReplace, strIniFilename, A_ScriptFullPath, .ahk, .ini

IniWrite, !q, %strIniFilename%, Global, Hotkey
IniWrite, DoThis, %strIniFilename%, Global, HotkeyCommand

IniWrite, qq, %strIniFilename%, Global, Hotstring
IniWrite, Some text, %strIniFilename%, Global, HotstringText

; at some other point in the script

IniRead, strHotkey, %strIniFilename%, Global, Hotkey
IniRead, strHotkeyCommand, %strIniFilename%, Global, HotkeyCommand
objHotkeyCommands.Insert(strHotkey, strHotkeyCommand)

IniRead, strHotstring, %strIniFilename%, Global, Hotstring
IniRead, strHotstringText, %strIniFilename%, Global, HotstringText

Hotkey, %strHotkey%, DoHotkey

; I know the "Hotstring" command below does not exit.
; But, if it would, it would do quite the same as the "Hotkey" command.
; Is there a way to obtain the same result?

; Hotstring, %strHotstring%, %strHotstringText%

; Here are a few alternatives that I put here for test
::ww::Some text ; this work
::%strHotstring%::%strHotstringText% ; "qq" does not work at all

return

DoHotkey:
gosub, % objHotkeyCommands[A_ThisHotkey]
return

DoThis:
MsgBox, strHotkey "%A_ThisHotkey%" executed
return

I've found this solution but it works only for uncompiled scripts. Mine is compiled.

I've also reviewed this solution but it seems quite invasive (it triggers every key of your keyboard).

Thanks.

EDIT (2018-02-11): This is now possible with v1.1.28+. See the Hotstring function and this demo.
Last edited by JnLlnd on 11 Feb 2018, 15:12, edited 2 times in total.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Create hotstrings from variables

09 Aug 2017, 15:02

JnLlnd wrote:I've also reviewed this solution but it seems quite invasive (it triggers every key of your keyboard).
Still, Dynamic Hotstrings was the first thing that came to mind. Creating a hotkey for every key is not so different from the native hotstring method. Both use a keyboard hook which triggers on each key event. Dynamic Hotstrings is just an additional layer on top of that hook. The downside is that it's cumbersome to implement regular hotkeys in the same script.
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Create hotstrings from variables

09 Aug 2017, 15:20

Nextron wrote:The downside is that it's cumbersome to implement regular hotkeys in the same script.
Hum. Interesting. The issue is that my script is already pretty complex to maintain regarding hotkeys. It supports hotkeys from variables (configurable in the GUI and stored in the app's ini file) and conditional hotkeys with #If (the function called for the hotkey may vary depend on the class of the windows on which it was executed).
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
Guest

Re: Create hotstrings from variables

09 Aug 2017, 16:51

First there is nothing wrong with redistributing a (renamed) autohotkey.exe for the sole purpose of hotstrings (the INI generates a ahk script, your program starts the renamed autohotkey and presto bob's your uncle - a good example of this technique is Catena by Normand Lamoureux http://normandlamoureux.com/catena/
(the catana exe is just a renamed autohotkey.exe (basic version) - so simply launch it as a plugin)

Second, just you could roll your own using a loop + input, lifted from lintalist (simplified) - perhaps I cut a few corners but you get the idea, check the Input examples in the Docs as well

Code: Select all

#SingleInstance, force
SetBatchLines, -1

abr:={"one":"hello there","two":"this is nice","three":"isn't it a lovely day"}

typed:=""
TriggerKeys=Tab,Space

TerminatingCharacters={Alt}{LWin}{RWin}{enter}{space}{esc}{tab}{Home}{End}{PgUp}{PgDn}{Up}{Down}{Left}{Right}{F1}{F2}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12} 

Loop
	{
	 Input, TypedChar, L1 V I, {BS}%TerminatingCharacters%
	 CheckTyped(TypedChar,ErrorLevel)
	}

CheckTyped(TypedChar,EndKey)
	{
	 Global TriggerKeys,abr,typed
	 If (EndKey = "EndKey:Backspace")
		{
		 StringTrimRight, Typed, Typed, 1
		 Return
		}
	 If (EndKey <> "Max")
		{
			StringReplace, EndKey, EndKey, Endkey:,,All
		 If EndKey in %TriggerKeys%
			{
			 If (Typed = "")
				Return
			 if abr.HasKey(typed)
				{
				 Loop % StrLen(typed) + 1
					Send {bs}
				 SendRaw % abr[typed]
				 Send {space}
				}
			 typed=
			}
		 }
		Else
			typed .= TypedChar
	}	

; for switching windows with mouse, clear typed stack
~*Lbutton::
Typed=
Return

~*MButton::
Typed=
Return

~*RButton::
Typed=
Return
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Create hotstrings from variables

09 Aug 2017, 21:53

Guest wrote:First there is nothing wrong with redistributing a (renamed) autohotkey.exe for the sole purpose of hotstrings (the INI generates a ahk script, your program starts the renamed autohotkey and presto bob's your uncle - a good example of this technique is Catena by Normand Lamoureux http://normandlamoureux.com/catena/
(the catana exe is just a renamed autohotkey.exe (basic version) - so simply launch it as a plugin)
Yes, this is a simple solution. My script would need a way to close the external script when exiting. Possibly using WinClose in OnExit.
Guest wrote:Second, just you could roll your own using a loop + input, lifted from lintalist (simplified) - perhaps I cut a few corners but you get the idea, check the Input examples in the Docs as well
Interesting too. In this example, the Tab in Alt+Tab is also triggering the hotstring expansion. But this is close to a working solution.

Thanks.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
Guest

Re: Create hotstrings from variables

10 Aug 2017, 03:59

Just deleted / forgot to copy some lines no doubt. You also need to take into account window switching, start typing in one window, switch to another window and continue to type could trigger it. But you'll figure it out :-0
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Create hotstrings from variables

10 Aug 2017, 10:06

JnLlnd wrote:
Guest wrote:First there is nothing wrong with redistributing a (renamed) autohotkey.exe for the sole purpose of hotstrings (the INI generates a ahk script, your program starts the renamed autohotkey and presto bob's your uncle - a good example of this technique is Catena by Normand Lamoureux http://normandlamoureux.com/catena/
(the catana exe is just a renamed autohotkey.exe (basic version) - so simply launch it as a plugin)
Yes, this is a simple solution. My script would need a way to close the external script when exiting. Possibly using WinClose in OnExit.
I'd have the script itself take care of closing itself if it's parent ceases to exist. If the parent process crashes, you can end up with multiple child processes.
You can then use ExecScript() to spawn a process without writing your dynamically generated hotstring script to hard disk. Then include the following to have it end itself when needed:

Code: Select all

Script:="
(
SetTimer,ParentDetect,5000
ParentDetect:
	Process,Exist," DllCall("GetCurrentProcessId") "
	If !ErrorLevel
		ExitApp
)"  
script.="`n::yh::Your hotstrings"
ExecScript(Script)

ExecScript(Script, Wait:=true)
{
    shell := ComObjCreate("WScript.Shell")
    exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
    exec.StdIn.Write(script)
    exec.StdIn.Close()
    if Wait
        return exec.StdOut.ReadAll()
}
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Create hotstrings from variables

10 Aug 2017, 19:00

Nextron wrote:I'd have the script itself take care of closing itself if it's parent ceases to exist. If the parent process crashes, you can end up with multiple child processes.
You can then use ExecScript() to spawn a process without writing your dynamically generated hotstring script to hard disk
Yes. Good suggestion. In the following line, I guess "AutoHotkey.exe" would have to be replaced with the new name of the exe file (as suggested earlier).
exec := shell.Exec("MyAutoHotkey.exe /ErrorStdOut *")
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Create hotstrings from variables

10 Feb 2018, 23:32

FYI, Lexikos just released v1.1.28 today that includes the Hotstring function that is the perfect solution to the request in my original post.

Thank you Lexikos!
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Create hotstrings from variables

11 Feb 2018, 15:14

I wrote this small "demo" script to test and show the capabilities of the new Hostrings introduced yesterday by lexikos in v1.1.28 with the "X" (execute) option. I thought it might be useful for other developers.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Joey5 and 190 guests