Understanding Why My AutoHotkey Variable Isn't Being Assigned - autohotkey

I am trying to assign a string to a variable, and then have a hotkey send that variable's value.
I already have a functioning example, where I use f1 to send a text string with some of the automatic Autohotkey variables such as %A_MM% %A_DD% and %A_YYYY% for quick and easy datestamps:
f1::
Send, (WTC %A_MM%/%A_DD%/%A_YYYY% %A_Hour%:%A_Min%) :{space} ; press f1
Return
I tried writing this to test:
v_test := "testing string in v_test"
f2::
MsgBox, %v_test%
RETURN
But the message box come up blank. Why isn't the message box displaying "testing string in v_test"?

The variables were never initialized because the variable was not contained within the auto-execute section of the autohotkey script.
To fix this, I had to put the variable declaration at the top of the script, before any RETURNs, EXITs, hotkeys, or hotscripts. I also could have written the variable as GLOBAL, inside of a function, then called the function.

Related

HotKey multiple characters

Is it possible to define an AutoHotKey HotKey which require multiple characters to fire ?
Example: if I write the AHK script
^j::
Send, my test
return
The Hotkey Control j fires off the string "my test"
What if I wanted to require Control jBAS to fire off the script ...in this case the letters BAS following the Control j ? From the examples I have seen, HotKey does not seem to allow this ...I can accomplish this with a HotString ...problem with HotString is that you must hit Enter and then the original String is replaced with the new HotString definition.
Heres an option:
^j::
Input, matchVal, C L3,, BAS
if (matchVal=="BAS") {
Send, mytest
}
Return
Check out https://www.autohotkey.com/docs/commands/Input.htm for further data and options.

Autohotkey: How to correctly write text, enclosed within brackets, to text files?

I need Autohotkey (AHK) to insert text, enclosed within brackets, into my text files. However, my AHK code results in erroneous text input depending on using the code on text files opened in Notepad3 or opened in Emacs. My use of brackets are obviously wrong relative to the AHK syntax.
My minimum working example:
::ttt::
; define variables
myString = abcdf
myFile = C:\Users\myUser\notes\myFile.tex
; write variables into current file
SendInput, <<%myString%>> {enter}
SendInput, [[%myFile%]]
return
The results I need should look like this:
<< abcde>>
[[C:\Users\myUser\notes\myFile.tex]]
Emacs When using this script on a text file opened with Emacs, the result looks like this:
<< abcde>>
[[C:\Users\myUser\notes\myFile.tex]]]
The first line written the way I need it, while the second line has got an extra "]" added in the end.
Notepad3 When using this script on a text file opened with Notepad3, the result looks like this:
<< abcde>>
[[C:\Users\myUser\notes\myFile.tex]]< /abcde>
The first line is written the way I need it, while the second line has got a variant of the first line added in the end, though with an "/" added.
How can I modify my code so the text input will look correct both in Notpad3 and in Emacs?
Is the space in << abcde>> necessary? The definition of your variable doesn't seem to reflect that.
Nonetheless, the issue likely stems from the fact that these keys are being inputted instead of sending the text directly. I find that it's usually best to send strings as text if you wish to avoid other hotkeys from potentially disturbing your output. Try this adjustment:
SendInput {text}<< %myString%>>
SendInput {enter}
SendInput {text}[[%myFile%]]

User to change a variable in a compiled macro

I have a variable with a value stored in an AutoHotKey macro. I want the user to be able to change the variable whenever they want and to have the macro use the new value stored in the variable when the macro is launched in the future. Is it possible to change a variable in a running macro and have the macro use the new variable value the next time the macro is launched? Alternatively, is there a way for the user to change a variable in a compiled macro? I'm also wondering about how to the user interface would be like for the user to be able to change the variable when the user chooses to change the variable?
Sure, you can store the variable in a text file. This will be the most simple and reliable method to change a variable and store it permanently.
Here is an example. Create a file "config.txt" in the same directory and put the text there. Pressing F1 will reload the text file into variable v.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
fname := "config.txt" ; define file name (must be in the same directory)
loadvar(fname) ; load file
return ; end main
; loadvar function
loadvar(fname){
global v
FileRead, v, %fname%
tooltip %v%
}
; reload variable
F1::
loadvar(fname)
return

Store key modifier as variable

Is there way to use variable for key modifier, for example:
var = +
%var%c:: do something ; Equal to Shift-C
... Well, StackOverflow trying to force me to write something more about the task. But I'm really don't know what to add to what have been already said. Also I've already read AHK forums, but can't find answer there.
Dynamic hotkeys are defined using the Hotkey command.
From the AHK website on hotkeys: https://autohotkey.com/docs/Hotkeys.htm
By means of the Hotkey command, hotkeys can be created dynamically
while the script is running. The Hotkey command can also modify,
disable, or enable the script's existing hotkeys individually.
This faq page on dynamic variables provides something close to what you are asking for:
https://autohotkey.com/board/topic/97097-faq-variables-dynamic-variables-literal-strings-and-stuff-like-that/
keys = abcdefghijklmnopqrstuvwxyz
StringSplit, keys, keys
Loop, %keys0%
Hotkey, % keys%A_Index%, keydown
return
keydown:
ToolTip, %A_ThisHotkey% was pressed
I've verified that the following works as expected:
var = +c
Hotkey, %var%, keydown
return
keydown:
ToolTip, %A_ThisHotkey% was pressed

How to wrap currently selected text in ``

How can write a hotkey (which works in any editor) to wrap currently selected text in ``
e.g double click "text", it becomes selected, then by pressing key for single `` it
is converted to text
Here is mix of pseudo code and actual code. Text within <> is what I'm not sure of
^<COMMAND FOR PRESSING ` KEY>::
KeyWait Control
<STORE CURRENTLY SELECTED TEXT IN MEMORY>
SendInput `<STORED SELECTED TEXT>'
return
Your approach is pretty good already. Try this:
$`::
clp_tmp := ClipboardAll
send ^c
currently_selected := Clipboard
stringReplace, currently_selected, currently_selected, `r,, All
Clipboard := clp_tmp
sendraw ``%A_Space%
sendraw %currently_selected%
sendraw ``%A_Space%
return
$ is needed because otherwise, sendraw `` would re-trigger this hotkey. The built-in variable clipboard / clipboardAll contains windows' clipboard. Also, you don't need any keywaits. ahk manages concurring modifiers from hotkey triggers by itself. I also suggest using sendraw which will not treat # as the win-button, + as Shift and so on.
script inserts new lines between each line if multiple lines are selected
Weird. When using msgBox, %currently_selected% instead of any send command, the line breaks are displayed correctly... there is obviously some strange formatting going on, I fixed it by simply removing all Carriage Returns (CR) (`r) from the string which does not change the selected text at all.
The given solution works for my keyboard which is German. This might be different for other keyboard layouts. For me, the %A_Space% in sendraw ``%A_Space% (at least in the second one) is needed, because if you state sendraw `` (a literal space character in the end), AHK will ignore it. You could also put all three sends in one line like
sendraw `` %currently_selected%``%A_Space%
Another solution might be
sendInput ````{BS}%currently_selected%````{BS}
or just simply
sendRaw `%currently_selected%`
Finally: If you wanted to make everything easier, make use of the #EscapeChar command and change the delimiter from ` to \ or sth. similar.