How can I declare a variable within a hotstring in AutoHotKey? - autohotkey

I'm trying to create a script that will take a name and add it to a pre-written block of text.
Essentially, I want to write "emailDave" and have the name Dave inserted into a string of text that is then sent. I'm just not sure how to modify a hotstring this way.
I'm currently using a method that asks for the name using an InputBox and inserts the name into the text. That works just fine on the Desktop, but I'm using Windows 8 and for some horrible reason, InputBox won't show up in-App (i.e. outside of Desktop mode).
I know there's got to be a way to use the text I input "email vs emailDave" to affect the variable instead of taking me on this goose chase with InputBox.
That said, if anyone knows a workaround for displaying InputBox in Windows 8 apps (particularly Mail), that would be more than helpful.
Current script that runs fine on Desktop but won't work in-App:
::email::
InputBox, thename, Enter the name, What is the name
SendInput Hi %thename%,{enter}{enter}Sample text.{enter}{enter}Thanks,{enter}Zach
Return
Is there any way to make something like this work?
::email{%thename%}::
SendInput Hi %thename%,{enter}{enter}Sample text.{enter}{enter}Thanks,{enter}Zach
Return

How about his:
:?*:email::
Input, thename, v,{Enter}{Space}
If (thename = "")
{
SendInput, {Bs}email `
Return
}
StringLen,MyLen, thename
MyLen++
SendInput {BackSpace %MyLen%}Hi +%thename%,{Enter 2}Sample text.{Enter 2}Thanks,{Enter}Zach
Return
By adding a + in front of the name string the first letter will be capitalized.
Input: "emailrobert{Enter}" or "emailRobert{Enter}" both give:
Hi Robert,
Sample text.
Thanks,
Zach
and email{Space} will give email{Space}.

If you really would like to avoid using an InputBox, I have a more complicated solution for you. You can use a library called RegEx Powered Dynamic HotStrings.
Save the file at that link into your lib folder inside the folder containing AutoHotkey.exe (create if necessary).
In this example, you type emailJohn followed by a Space.
#Include lib\DynamicHotstrings.ahk
hotstrings("email(\w+)\s", "email")
Return
email:
SendInput, Hi %$1%,{Enter 2}Sample text.{Enter 2}Thanks,{Enter}Zach
return

::email::
inputbox, name
msgbox, %name%
return
take "dave" out of the hotstring
After you test, replace the msgbox, %name% with whatever send line you want.

Related

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%]]

AutoHotkey: How can I paste text into this particular textbox?

Using AutoHotkey, I have a very tiny script to write text:
^m::
SendInput Foo
Return
This works in most places like Notepad and chrome. However, it doesn't work for a particular program I am using. The program has a simple textbox which I can type text into. The program is called TextExpander. I am making sure the cursor is located in the textbox. For some reason, I can't get auto-hotkey to type text into it.
Any help? Tips?
You can look at ControlSetText. Use WindowSpy to check the control name. If it has one, you can use that to set the text.
Alternately, make sure you're trying SendInput/SendEvent/SendPlay.
If you have a way to reliably focus the box you're trying to enter text to, you can put your variable onto Clipboard and send paste via Ctrl+V SendInput, ^v.

(AHK) Creating variable hotkeys that gets the key names from a 2 char file name of a script

I'm trying to make something for our employees to use so that they dont have to alter the script itself to define hotkeys. This may only work for hotkeys which can be defined by a single character, but that's fine, as there are so many combinations that can be made with them, and they can be very easy to remember. The script would look only at 2 character AHK files (or 6 if you must include the extension) in the working directory. And the variables it would search for could be defined with RegEx so for the first hotkey, it would look like ^. and then second would look like .(?=.) Once a match is found, it would simply launch that matched file. Has something like this been done before? It seems so simple but I can't seem to find anything on it.
Edit: Elliot brought this to my attention: http://autohotkey.com/board/topic/60630-easy-editmanage-hotkeyshotstrings-plugin-ahk-l/
It's a neat script manager, and very useful, but it's not what I'm looking for.
I dont not want an additional interface. I want to be able to change the hotkeys by using the filename.
Based on the answer of Forvin. Added the execution of the corresponding ahk script.
#Persistent
SetTimer, FindNewHotkeys, 2000
FindNewHotkeys:
Loop, %A_ScriptDir%\*
{
RegExMatch(A_LoopFileName, "^(.)(.).ahk$", hk)
If (hk)
{
Hotkey, ~%hk1% & ~%hk2%, HotkeyLabel
}
}
Return
HotkeyLabel:
RegExMatch(A_ThisHotkey, "~(.) & ~(.)", hk)
run, %hk1%%hk2%.ahk
Return
#Persistent
SetTimer, FindNewHotkeys, 2000
FindNewHotkeys:
Loop, %A_ScriptDir%\*
{
RegExMatch(A_LoopFileName, "^(.)(.).ahk$", hk)
If (hk)
Hotkey, ~%hk1% & ~%hk2%, HotkeyLabel
}
Return
HotkeyLabel:
MsgBox, A hotkey has been pressed!
Return

AHK Sending a TXT file as typed out text

am using AHK to publish boilerplate e-mail body texts in our company. Our employees use various webmail, and other mail so managing templates is impossible. So comes AHK. We have created scripts to publish this with the details of the boilerplate within the body of the script, but it is difficult to delegate revision management when the script itself needs to be edited each time as the boilerplate text changes. Is there a way to send the contents of a Text file, i.e. "bp..pins.txt" as a keyboard input verses placing all the boilerplate text within the script?
Btw: We use Dropbox to sync scripts across users computers.
One way to do that is to simply use the command FileRead
FileSelectFile, path
::doit:: ; hotstring type "doit" to activate
FileRead, FileContent, %path%
Sendinput %FileContent%
return
Hope it helps
Do realize that SendInput has limitations. If you send enough text, it will buffer in the keyboard buffer and show up on the screen much slower than expected. It won't take more than a paragraph for the SendInput to end up being slower than a manual cut and paste.
I recommend reading the file into the clipboard and pasting it instead:
FileSelectFile, path
::doit:: ; hotstring type "doit" to activate
FileRead, FileContent, %path%
Clipboard := FileContent
SendInput ^v
return

autohotkey long text and in a virtual machine

So I'm trying to learn autohotkey scripts and the documentation is lacking at best. First, can authotkey read commands and perform actions and such inside a virtual machine? I have a windows host and a linux virtual machine running eclipse. I'd like to get a hostring (or a keyboard macro, either is fine) to put in some long (10+ lines) of text. Can that actually work in a VM or do I have to run autohotkey inside the VM for it to work?
As for implementing this, I have 2 problems. First, how do I display multiple lines of text from a keyboard macro? I know about the Send command, but I haven't figured out how that works. I have this:
:*:insert::
(
Text to
insert
goes here
and more here
)
And this works fine except in notepad++, it inserts consecutively more tabs, so it will look like
Text to
insert
goes here
and more goes here
And so in my many line macro, by the end it's several pages scrolled off the screen.
As for keyboard macro, changing the above to
#c::
Send{Raw} (
stuf
to send
)
Return
This gives syntax errors and I have no idea what the correct way of doing that would be. Should I just stick with using hotstrings?
You could try to modify the clipboard and use control + v to paste it into the proper place.
Try:
#c::
{
clipboard := "yourtext`nMultiline`nYet another line"
send, {control down}v{control up}
return
}
The first 'insert' hotstring is correct,
however, you would get the same result that you describe,
if you performed manually, the keypresses that the hotstring is sending.
To get the output you want,
you need to change these two settings:
Settings, Preferences...,
Auto-Completion,
untick: Enable auto-completion on each input
Settings, Preferences...,
MISC.,
untick: Auto-indent
the '#c' hotstring is amended below:
#c::
Send {Raw}
(
stuf
to send
)
Return