Putting function Hotstring() at work

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

Putting function Hotstring() at work

11 Feb 2018, 15:06

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 (thanks, lexikos!). I thought it might be useful for other developers.

Code: Select all

#SingleInstance force

objString1 := Object("HotstringTrigger", "f1#", "HotstringOptions", "X", "Content", "String1 default options (except execute)")
objString2 := Object("HotstringTrigger", "f2#", "HotstringOptions", "XCB0", "Content", "String2 case-sensitive, do not backspace")
objString3 := Object("HotstringTrigger", "f3#", "HotstringOptions", "X*", "Content", "String3 no ending key")

objHotstrings := Object()
Loop, 3
	objHotstrings[":" . objString%A_Index%.HotstringOptions . ":" . objString%A_Index%.HotstringTrigger] := objString%A_Index%
	
for strHotstring in objHotstrings
{
	Hotstring(strHotstring, "ShowObject") ; requires v1.1.28+ https://autohotkey.com/docs/commands/Hotstring.htm
	strMessage .= strHotstring . "`n"
}

MsgBox, Hotstrings created:`n`n%strMessage%`nTo test these hotstrings, type "f1#" + space, "f2#" + space or  "f3#" (no space required)

return

ShowObject:

StringSplit, arrHotstring, A_ThisHotkey, :

MsgBox, % "You typed """ . arrHotstring3 . """ with options """ . arrHotstring2 . """.`n`nIt triggered the command """ . A_ThisLabel . """ displaying the content of objHotstrings[A_ThisHotkey].Content: """ . objHotstrings[A_ThisHotkey].Content . """"

return
In my app Quick Access Popup, this new function will allow me to associate hotstrings to favorites objects and make text snippets as well as other types of favorites (folders, documents, apps, etc.) launchable with an hotstring (in addition to menus and hotkeys).
: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
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Putting function Hotstring() at work

11 Feb 2018, 15:21

Nice, thanks for sharing, it is a great addition :thumbup:.

Cheers.
User avatar
JnLlnd
Posts: 490
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Putting function Hotstring() at work

11 Feb 2018, 19:40

On thing I should have mention. There is an issue with this quick and dirty example. If two hotstrings using the "C" (case-sensitive) option have a trigger with the same letters but different upper/lower case, only one of the two will be added to the objHotstrings object because objects keys are case insensitive. For example:

Code: Select all

objString4 := Object("HotstringTrigger", "f4#", ...)
objString5 := Object("HotstringTrigger", "F4#", ...)
In this example, "f4#" and "F4#" are considered the same key and the objString4 object will be added twice to objHotstrings (the 2nd overwriting the 1st one). To fix this, use objHotstrings := ComObjCreate("Scripting.Dictionary") instead of Object() because this Com object supports case sensitive keys.

The fixed script should be:

Code: Select all

#SingleInstance force

objString1 := Object("HotstringTrigger", "f1#", "HotstringOptions", "X", "Content", "String1 default options (except execute)")
objString2 := Object("HotstringTrigger", "f2#", "HotstringOptions", "XCB0", "Content", "String2 case-sensitive, do not backspace")
objString3 := Object("HotstringTrigger", "f3#", "HotstringOptions", "X*", "Content", "String3 no ending key")
objString4 := Object("HotstringTrigger", "f4#", "HotstringOptions", "XC*", "Content", "String4 lower key")
objString5 := Object("HotstringTrigger", "F4#", "HotstringOptions", "XC*", "Content", "String5 UPPER key")

objHotstrings := ComObjCreate("Scripting.Dictionary") ; instead of Object() to support case sensitive keys

Loop, 5
	objHotstrings.Add(":" . objString%A_Index%.HotstringOptions . ":" . objString%A_Index%.HotstringTrigger, objString%A_Index%)
	
for strHotstring in objHotstrings
{
	Hotstring(strHotstring, "ShowObject") ; requires v1.1.28+ https://autohotkey.com/docs/commands/Hotstring.htm
	strMessage .= strHotstring . "`n"
}

MsgBox, Hotstrings created:`n`n%strMessage%`nTo test these hotstrings, type "f1#" + space, "f2#" + space, "f3#" (no space required), "f4#" (lower case) or "F4#" (upper case). 

return
: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: kaka2 and 132 guests