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

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.

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

{AHK} Defining controls (/Assigning tasks) for those GUI buttons that were creted after script launch

Lets say with some action during running script, you create gui with several buttons via Loop.
Loop, 5
{
Gui, Add, Button,, Number %A_Index%)
}
Gui, Show
How do u then assign actions upon pressing one of buttons?
It seems you cant do it after script launch, and tricks like
ButtonNumber%A_Index%: ;even if i was doing it inside loop.
do something here bla bla
return
do not work.
To do things even worse, i wanted to created these buttons (here for test) from contents of a file, say, each lines text gets utilized to name a button.
you can find similar mini-projects in AHK help files. Buts lets stick with this simple analog.
May be:
Storing and Responding to User Input, third option Variable or g-label is the anwser. Yet it asks for static/global var, but i have troubles declaring these. And g-labels i am not familiar with.
Other option i had in mind is- creating pre-defined buttons (a lot), rename them to my values (from file), and discard rest. hopefully i will be able to use predefined controls.
P.S
AHK help file is a real mess, as a beginner i find it pretty had to fish out complete and meaningful information, instead you have to search and take a bite here and there.
One way is to use a parsing loop and one g-label for all the buttons, then use A_guiControl to get the variable name of the button that called the sub-routine
Example:
; fileread, file_content, Path-to-file
file_content =
(
line with text one
line with text more
line with text other
line with text something
line with text two
)
Loop, parse, File_content, `n, `r
{
Gui, Add, Button, vMyButton%A_index% gButtons, %A_LoopField%
}
Gui, Show
return
Buttons:
msgbox % A_GuiControl
return
GuiClose:
ExitApp
Hope it helps
I have found one possible anwser to my problem. Basicly it involves g-label functionality that blackholyman (lel) suggested.
Using same g-label for all my buttons combined with A_GuiControl comparison inside button control.
Since i have stored buton names in a file, in one line with other data, that is relevant for this button, i can compare each line, by parsing, with button name (A_GuiControl), thats makes me able to retrieve relevant data inside assigned g-label.
May be some one will find it useful. Ill add code later.

How can I declare a variable within a hotstring in 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.

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

Autohotkey to select text under cursor?

Is it possible to select text under cursor using Autohotkey specifically, using a mouse and a key combination. e.g. I specifically want to just do a Ctrl-click on any word in IE/FF/Foxit Reader and a webpage with first Google search result opens up.
Thank you.
Yes it is possible...
The easiest way is to set Ctrl+LButton to double click (which selects the current word under the cursor) copy the word to the clipboard and then use google with the parameters "q=" for the search term and "btnI=I'm+Feeling+Lucky" for using the "I'm Feeling Lucky" function.
It would look like this:
^LButton::
Send, {LButton 2}^c
Run, http://www.google.com/search?&q=%clipboard%&btnI=I'm+Feeling+Lucky
return
This would work in most cases without issues, the issue comes when selecting words like:
53°C
Jhon's
test%s
and others, because double clicking them only selects the first part of the text before the symbols.
So as long as you are double clicking normal words this should work fine.