Autohotkey write text read from a text file - autohotkey

I want my script to read a textfile containing an intiger and to write this number by keyboard with out me actually touching it, but im kinda having trouble understanding the autohotkey since there is no software for it. Having some C# knowledge this is what i have:
FileRead, OutputVar, answertext.txt
sleep, 3000
;MyString = %OutputVar%
MyString = 16807
Loop, Parse, MyString
{
if (%A_LoopField% = -)
{
Send, {SC00C}
}
if (%A_LoopField% = 0)
{
Send, {SC00B}
}
if (%A_LoopField% = 1)
{
Send, {SC002}
}
if (%A_LoopField% = 2)
{
Send, {SC003}
}
if (%A_LoopField% = 3)
{
Send, {SC004}
}
if (%A_LoopField% = 4)
{
Send, {SC005}
}
if (%A_LoopField% = 5)
{
Send, {SC006}
}
if (%A_LoopField% = 6)
{
Send, {SC007}
}
if (%A_LoopField% = 7)
{
Send, {SC008}
}
if (%A_LoopField% = 8)
{
Send, {SC009}
}
if (%A_LoopField% = 9)
{
Send, {SC00A}
}
}
exit
Now this code starts but it always punches in 0 and nothing else, and if MyString = -1234, there is an error.

I will provide here some notes on improving your AutoHotkey script, and an amended version of it.
Your 'if' lines should be like this for most strings, with double quotes, and no percent signs:
if (A_LoopField = "a")
With numbers the double quotes are optional:
if (A_LoopField = "1")
if (A_LoopField = 1)
However fundamentally, your script need only be a few lines long:
FileRead, OutputVar, answertext.txt
sleep, 3000
;MyString = %OutputVar%
MyString = 16807
Send {Raw}%MyString%
You may prefer to include a key delay, to slow down the sending of text:
MyString = 16807
SetKeyDelay, 500
Send {Raw}%MyString%
Other methods for sending the text might be:
Clipboard := MyString
SendInput ^v
Or to insert text into Edit controls such as in Notepad for example without using the clipboard:
Control, EditPaste, %MyString%, Edit1, ahk_class Notepad
Or to set the entire contents of Edit controls, again without using the clipboard:
ControlSetText, Edit1, %vText%, ahk_class Notepad

Related

Why doesn't timer treat global variable correctly?

This continuously sends t:
myVar := true
SetTimer, myLoop, 0
myLoop(){
if (true) {
Send, {t down}
}
}
#Persistent
But this does nothing:
myVar := true
SetTimer, myLoop, 0
myLoop(){
if (myVar) {
Send, {t down}
}
}
#Persistent
Why? myVar and true both evaluate to true so I don't understand why it's different.
myVar := true should be:
global myVar := true
Because a global must explicitly be declared as such.

Combine if statement and for loop such that loop only gets executed once per every execution of if statement in swift

I'm trying to understand swift and therefore try to come up with simple command line games: in this game a player has to guess a secret word within 6 attempts by typing something in the command line, but every time he gets it wrong, a statement prints the number of his wrong attempts:
let response = readLine()
if response != "secret word" {
for n in 1...6 {
print(n)
}
}
else {
print("you are right!")
}
Now I know that my code will print all lines once the condition is not true, but I'm looking for a way to only print one item out of the four loop for every if statement consecutively.
I think a while loop works pretty well. Maybe something like this:
print("Welcome to the input game!\n\n\n\n")
var remainingTries = 5
let dictionary = ["apple", "grape", "pear", "banana"]
let secretWord = dictionary.randomElement()
print("Please guess a fruit")
while remainingTries > 0 {
remainingTries -= 1
let response = readLine()
if response == secretWord {
print("You got it!")
remainingTries = 0
} else if remainingTries <= 0 {
print("Too bad, you lose!")
remainingTries = 0
} else {
print("Incorrect. Tries remaining: \(remainingTries)")
}
}

Autohotkey iterate through loop, but only return value if key matches input

Suppose I have the following
^!r::
InputBox, input, Enter the string
if (input = "trip")
{
TripFunction()
}
else if (input = "leave")
{
LeaveFunction()
}
else
{
Msgbox, That word isnt defined.
}
Return
But, anticipating having to add a lot of different cases to test for, I figure the best idea is to put this into an array, and iterate through the array, looking for the matching key, returning the value (the function), and no longer iterating through the dictionary. So now, I have something like this:
^!r::
InputBox, input, Enter the string
dict = { "trip": TripFunction(), "leave": LeaveFunction() }
for k, v in dict
{
...
see if k = "trip", if so, return TripFunction(), if not, go to next
item in array
...
}
Return
The trouble I'm having is once it successfully matches akey in the dictionary, it will return all of the associated values. What should I put in the brackets to do what I intend?
You're using the wrong equal sign operator (use := for non-literal assignments instead of =)
Also, in the line dict = { "trip": TripFunction(), "leave": LeaveFunction() }, tripfunction aind leavefunction are executed, which you probably do not want.
Try:
^!r::
InputBox, input, Enter the string
dict := { "trip": "TripFunction", "leave": "LeaveFunction" }
for k, v in dict
{
if(k==input) {
%v%() ; documentation: https://autohotkey.com/docs/Functions.htm#DynCall
break
}
}
Return
TripFunction() {
msgbox trip
}
LeaveFunction() {
msgbox leave
}

Read lines from big text file in Swift until new line is empty: the Swift way

I have the following text file structure (the text file is pretty big, around 100,000 lines):
A|a1|111|111|111
B|111|111|111|111
A|a2|222|222|222
B|222|222|222|222
B|222|222|222|222
A|a3|333|333|333
B|333|333|333|333
...
I need to extract a piece of text related to a given key. For example, if my key is A|a2, I need to save the following as a string:
A|a2|222|222|222
B|222|222|222|222
B|222|222|222|222
For my C++ and Objective C projects, I used the C++ getline function as follows:
std::ifstream ifs(dataPathStr.c_str());
NSString* searchKey = #"A|a2";
std::string search_string ([searchKey cStringUsingEncoding:NSUTF8StringEncoding]);
// read and discard lines from the stream till we get to a line starting with the search_string
std::string line;
while( getline( ifs, line ) && line.find(search_string) != 0 );
// check if we have found such a line, if not report an error
if( line.find(search_string) != 0 )
{
data = DATA_DEFAULT ;
}
else{
// we need to form a string that would include the whole set of data based on the selection
dataStr = line + '\n' ; // result initially contains the first line
// now keep reading line by line till we get an empty line or eof
while(getline( ifs, line ) && !line.empty() )
{
dataStr += line + '\n'; // append this line to the result
}
data = [NSString stringWithUTF8String:navDataStr.c_str()];
}
As I am doing a project in Swift, I am trying to get rid of getline and replace it with something "Cocoaish". But I cannot find a good Swift solution to address the above problem. If you have an idea, I would really appreciate it. Thanks!
Using the StreamReader class from Read a file/URL line-by-line in Swift, you could do that it Swift like this:
let searchKey = "A|a2"
let bundle = NSBundle.mainBundle()
let pathNav = bundle.pathForResource("data_apt", ofType: "txt")
if let aStreamReader = StreamReader(path: pathNav!) {
var dataStr = ""
while let line = aStreamReader.nextLine() {
if line.rangeOfString(searchKey, options: nil, range: nil, locale: nil) != nil {
dataStr = line + "\n"
break
}
}
if dataStr == "" {
dataStr = "DATA_DEFAULT"
} else {
while let line = aStreamReader.nextLine() {
if countElements(line) == 0 {
break
}
dataStr += line + "\n"
}
}
aStreamReader.close()
println(dataStr)
} else {
println("cannot open file")
}

How to store input from keyboard into variable?

I am currently using Random function in Autohotkey to generate random and save into variable rand but if user presses R.
my question is below this code
R::
Random, rand, 1, 3
Msgbox, %rand%
if (rand = "1")
{
;SAM()
}
else if (rand = "2")
{
;AAJ()
}
else if (rand = "3")
{
;HEAD()
}
else
{
;Msgbox, else
}
I also want to add code where if user presses 1 it will, may if I can add OR expression in if statement such as
if (rand = "1" || keyboardinput = "1" )
{
;SAM()
}
Why not use the same approach u used with your code for generating random numbers.
1::
if( rand == 1)
{
tooltip, hello
}
return
You are also missing a return at the end of the first part of your code, unless you want the script to start executing things it shouldn't.