Reputation: 81
I have simple question that i can`t find answer by searching internet.
How can I make my lines into one (single) line?
for example this is a working code
right::
Send, ^s
Reload
Send, hello world
and this is not
right::Send, ^s::Reload::Send, hello world
Upvotes: 4
Views: 3840
Reputation: 60
As a beginner myself, I would probably use a subroutine instead of a function (like in Jim U's answer), not because it's better but because I found it easier to understand. Not used to functions in ahk yet.
If you place the following code in the bottom of the script (or anywhere it won't bother you, or the script)...
banana:
Send, ^s
Send, hello world
return
...the following line will suffice:
right::Gosub, banana
Upvotes: -1
Reputation: 130
In Autohotkey you can not use Line breaks into a single Codeline.
But if you want to make lines into one (single) line.
you Can try this,
1 - Put it into a string.
2 - Convert it + Save it to a file.
3 - and then run the Script from a External file.
note - i did make the Script so how you did want it, save it + ?Reload=run example1.ahk + Send text (It does not work exact so how you did like it, but it is almost. it is for me not clear why you want to reload the script?)
Try this code.
example1.ahk
; [+ = Shift] [! = Alt] [^ = Ctrl] [# = Win]
#SingleInstance force
sleep 100
return
right:: ConvertAndRun("Sendinput, ^s | run example1.ahk | runwait example1.ahk | Send, hello world")
left:: ConvertAndRun("Sendinput, ^s | run example1.ahk | runwait example1.ahk | Send, it works")
~esc::exitapp
ConvertAndRun(y)
{
FileDelete, Runscript.ahk
;1 - Convert the String in single codeline's with return - Character | is the breakline.
StringReplace,y,y, |, `n, all
sleep 150
;2 - Save the String to a file
x:="#notrayicon `n "
z:="`n "
FileAppend, %x%%y%%z%, Runscript.ahk
sleep 150
;3 - Now it can Run all the commands from that string.
run Runscript.ahk
sleep 150
exitapp
}
;
Note - This Script Does Save the String to a file (on the HardDisk c:) that is not so fast, but If you use a Ramdisk, then you can save and run the file's from there, it will increase the speed - a Ramdisk is a virtual Disk (stored on the RAM MEMORY z:) and that is +-100x faster then a Hard disk. - You can find this Free Tool from Here (Imdisk)
Upvotes: 1
Reputation: 3366
Autohotkey doesn't have a statement terminator like the ;
in c and javascript.
However if your goal is compact and readable code, refactoring using functions can help you achieve this.
Your code expressed in one line:
right::alpha("^s", "hello world")
alpha(text1, text2) {
send % text1
tooltip "reload" will interrupt your script. Any code following "reload" may not execute
send % text2
}
Upvotes: 4
Reputation: 12438
From what I know, I don't think it is possible since Line breaks play the role of statement separators, terminating the previous command/expression.
from the doc: https://autohotkey.com/docs/Language.htm
Line breaks are meaningful: Line breaks generally act as a statement separator, terminating the previous command or expression. (A statement is simply the smallest standalone element of the language that expresses some action to be carried out.) The exception to this is line continuation
Upvotes: 0