hotstring

Get help with using AutoHotkey and its commands and hotkeys
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

hotstring

12 Nov 2019, 15:50

Welcome my dear colleagues
this code from here: https://www.autohotkey.com/docs/Hotstrings.htm#Helper

Code: Select all

#h::  ; Win+H hotkey
; Get the text currently selected. The clipboard is used instead of
; "ControlGet Selected" because it works in a greater variety of editors
; (namely word processors).  Save the current clipboard contents to be
; restored later. Although this handles only plain text, it seems better
; than nothing:
AutoTrim Off  ; Retain any leading and trailing whitespace on the clipboard.
ClipboardOld := ClipboardAll
Clipboard := ""  ; Must start off blank for detection to work.
Send ^c
ClipWait 1
if ErrorLevel  ; ClipWait timed out.
    return
; Replace CRLF and/or LF with `n for use in a "send-raw" hotstring:
; The same is done for any other characters that might otherwise
; be a problem in raw mode:
StringReplace, Hotstring, Clipboard, ``, ````, All  ; Do this replacement first to avoid interfering with the others below.
StringReplace, Hotstring, Hotstring, `r`n, ``r, All  ; Using `r works better than `n in MS Word, etc.
StringReplace, Hotstring, Hotstring, `n, ``r, All
StringReplace, Hotstring, Hotstring, %A_Tab%, ``t, All
StringReplace, Hotstring, Hotstring, `;, ```;, All
Clipboard := ClipboardOld  ; Restore previous contents of clipboard.
; This will move the InputBox's caret to a more friendly position:
SetTimer, MoveCaret, 10
; Show the InputBox, providing the default hotstring:
InputBox, Hotstring, New Hotstring, Type your abreviation at the indicated insertion point. You can also edit the replacement text if you wish.`n`nExample entry: :R:btw`::by the way,,,,,,,, :R:`::%Hotstring%
if ErrorLevel  ; The user pressed Cancel.
    return
if InStr(Hotstring, ":R`:::")
{
    MsgBox You didn't provide an abbreviation. The hotstring has not been added.
    return
}
; Otherwise, add the hotstring and reload the script:
FileAppend, `n%Hotstring%, %A_ScriptFullPath%  ; Put a `n at the beginning in case file lacks a blank line at its end.
Reload
Sleep 200 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
MsgBox, 4,, The hotstring just added appears to be improperly formatted.  Would you like to open the script for editing? Note that the bad hotstring is at the bottom of the script.
IfMsgBox, Yes, Edit
return

MoveCaret:
IfWinNotActive, New Hotstring
    return
; Otherwise, move the InputBox's insertion point to where the user will type the abbreviation.
Send {Home}{Right 3}
SetTimer, MoveCaret, Off
return
Could you add a function to the code to delete the Hotstring already entered.
This Current dialog box
Hotstring.PNG
Hotstring.PNG (10.97 KiB) Viewed 625 times
After the amendment I propose to be with three options

Insert Delete Cancel

Thanks in advance.
User avatar
SuperFoobar
Posts: 49
Joined: 23 Nov 2018, 15:14

Re: hotstring

12 Nov 2019, 17:58

I decided to take on this challenge, adding a delete function as well as the add function using a custom GUI solution.

gui.png
gui.png (8.64 KiB) Viewed 566 times

Clicking the delete button will bring up a secondary GUI which lets you choose from 1 of the hotstrings in the external Hotstrings.txt file, and allows you to delete one.

delete.PNG
delete.PNG (9.28 KiB) Viewed 564 times

All of the hotstrings are stored externally from the main script and then #Included in to prevent accidental deletion of the script as this script uses the FileRecycle command before using FileAppend

Code: Select all

#NoEnv
SendMode Input
#SingleInstance Force
SetBatchLines -1

;ExternalHotstring File used with #Include
HotstringFile :=  A_ScriptDir "\Hotstrings.txt"

;Main Hotstring GUI
Gui +ToolWindow +AlwaysOnTop +hwndHotstringGUI
Gui, Add, Text, , Enter a new Hotstring
Gui, Add, Edit, hwndHotstringEdit vHotstring w290
Gui, Add, Button, Section w90 gInsert, Insert
Gui, Add, Button, ys wp gDelete, Delete
Gui, Add, Button, ys wp gCancel, Cancel

;Delete Hotstring GUI
Gui, Delete: +ToolWindow
Gui, Delete:Add, Text, , Select a Hotstring to Delete
Gui, Delete:Add, ListBox, w200 r10 hwndListBox vSelHotstring
Gui, Delete:Add, Button, wp gDeleteHotstring, Delete

Return

;Insert a new Hotstring
Insert:
	Gui, Submit
	FileAppend, % "`n" Hotstring, % HotstringFile
	MsgBox % ErrorLevel ? "Hotstring Failed to Add" : "Hotstring added Successfully"
	Reload
Return

;Delete a Hotstring
Delete:
	Gui, Submit
	Loop, Read, % HotstringFile
		ListBoxItems .= A_LoopReadLine "|"
	
	GuiControl,, % ListBox,
	GuiControl,, % ListBox, % ListBoxItems
	
	Gui Delete:Show,, Delete Hotstring
Return

DeleteHotstring:
	Gui Delete:Submit, Nohide
	If !SelHotstring
		MsgBox, , ERROR, No hotstring selected
	
	Loop, Read, % HotstringFile
		If !InStr(A_LoopReadLine, SelHotstring) AND !RegExMatch(A_LoopReadLine, "^\s^")
			Hotstrings .= A_LoopReadLine "`n" ;Make a list of every hotstring that is not the one we want to delete
	
	FileRecycle % HotstringFile ;Delete the original file
	Sleep 100 
	FileAppend, % Hotstrings, % HotstringFile ;Append our hotstrings
	MsgBox % (ErrorLevel ? "Failed to Delete Hotstring" : "Hotstring Deleted Successfully")
	Reload
Return

Cancel:
	Gui, Hide
Return

#h::
	AutoTrim Off
	ClipboardOld := ClipboardAll
	Clipboard := ""  
	Send ^c
	ClipWait 1
	If ErrorLevel
	    Return

	StringReplace, Hotstring, Clipboard, ``, ````, All
	StringReplace, Hotstring, Hotstring, `r`n, ``r, All
	StringReplace, Hotstring, Hotstring, `n, ``r, All
	StringReplace, Hotstring, Hotstring, %A_Tab%, ``t, All
	StringReplace, Hotstring, Hotstring, `;, ```;, All

	Clipboard := ClipboardOld 
		
	GuiControl,, % HotstringEdit, % "::R::" Hotstring
	Gui, Show, , Add Hotstring
	SendMessage, 0xB1, 2, 2,, % "ahk_id " HotstringEdit ;Move the carret to the 2nd position
Return

;===============================
; HOTSTRINGS
;===============================

#Include *i Hotstrings.txt


Let me know what you think!
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: hotstring

13 Nov 2019, 09:46

Welcome SuperFoobar :bravo:
Outstanding effort led to a great result
Creating a Hotstrings.txt file made it easy to enter a large number of words
And make deletion and addition very easy

But when converting to EXE instead of AHK
It doesn't work ..
I reiterate my thanks and appreciation
User avatar
SuperFoobar
Posts: 49
Joined: 23 Nov 2018, 15:14

Re: hotstring

13 Nov 2019, 11:38

Thank you!

Although my code won't work when compiled, your original code you had will also not work when compiled due to the nature of having to append a file!
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: hotstring

13 Nov 2019, 13:30

Hi SuperFoobar

Did you mean appending the Hotstrings.txt file?
How to solve this problem please?
User avatar
SuperFoobar
Posts: 49
Joined: 23 Nov 2018, 15:14

Re: hotstring

13 Nov 2019, 17:18

Appending to a TXT while compiled is no issue, it's just that the EXE can't make use of it because #include does not exist in compiled files

#Include statements do not work with compiled files simply because they do not exist in compiled files. At the time of compilation it takes all the #Include files and adds it to the ahk before compiling, meaning, whatever was in Hotstrings.txt at the time of compilation is what will be in the final compiled exe and cannot be changed anymore.

A workaround would be to append the hotstrings to an AHK file and then run that AHK file, but this defeats the purpose of compiling in the first place.

Like I said, this issue is also present in your original code too, this is a deep rooted structural issue in the concept itself, not just in my solution.
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: hotstring

14 Nov 2019, 08:37

Thanks for this detailed explanation, but if the file is Hotstring.ini
Not Hotstrings.txt, is there a problem when converting to exe?
User avatar
SuperFoobar
Posts: 49
Joined: 23 Nov 2018, 15:14

Re: hotstring

14 Nov 2019, 10:17

asad41163 wrote:
14 Nov 2019, 08:37
Thanks for this detailed explanation, but if the file is Hotstring.ini
Not Hotstrings.txt, is there a problem when converting to exe?
The file extension does not change any of the above facts
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: hotstring

14 Nov 2019, 17:55

I don't mean just changing the file extension..
but I mean using the write and read feature of an .ini file, instead of including a .txt file.
User avatar
SuperFoobar
Posts: 49
Joined: 23 Nov 2018, 15:14

Re: hotstring

18 Nov 2019, 14:05

asad41163 wrote:
14 Nov 2019, 17:55
I don't mean just changing the file extension..
but I mean using the write and read feature of an .ini file, instead of including a .txt file.
If you want to use file-read to define hotkeys then the structure of the application needs to change such that data is read from a file and then looped into a Hotstring command

Return to “Ask For Help”

Who is online

Users browsing this forum: AHK_user, Alexzemm, Google [Bot], jNizM, Scryptic, sdkyron and 228 guests