Increment variables help please

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Increment variables help please

Post by jonjon » 19 Mar 2023, 04:52

[Mod edit: Moved topic to AHK v1 help. The main help sections are now for AHK v2.]

Hi,
I learned about variables yesterday and created this script. I'm using it to write text in Notepad, where each time, a new song title is erased and replaced by the next one after 1 second. I'm going to take a screen capture (Mp4) of this with ShareX and then import into a video editor (cropping out the edges) to create video captions.

Code: Select all

SetKeyDelay, 40			 
SetNumLockState , on
#SingleInstance Force
^q::suspend
^w::exitapp
          
^1::
a=40   ;key delay for typing text
b=0    ;key delay for erasing text

x=1000     ;duration text stays on screen before being erased
y=50        ; how many characters for BackSpace
z=1          ; starting increment
num=z++    ;increment by 1

1  = A Glorious Life
2  = A Jolly Warm-up for Flute
3  = A Medley of Three Christmas Carols
4  = A Pause for Thought


SetKeyDelay, %a%
send, Piece %num% : %1%
sleep, %x%
SetKeyDelay, %b%	
send, {BackSpace %y%}
SetKeyDelay, %a%


send, Piece %num% : %2%
sleep, %x%
SetKeyDelay, %b%
send, {BackSpace %y%}
SetKeyDelay, %a%

send, Piece  %num% : %3%
sleep, %x%
SetKeyDelay, %b%
send, {BackSpace %y%}
SetKeyDelay, %a%

send, Piece 4 : %4%
sleep, %x%
SetKeyDelay, %b%
send, {BackSpace %y%}
SetKeyDelay, %a%
return


It all worked well, except I'd like to make the number after the word "Piece" increment by 1 each time automatically.
Like this:
Piece 1 : A Glorious Life
Piece 2 : A Jolly Warm-up for Flute
Piece 3 : A Medley of Three Christmas Carols
Piece 4 : A Pause for Thought.

Obviously there's something wrong with my incremental variables.

Code: Select all

z=1    ; starting increment
num=z++  ;increment by 1
I've read
https://www.autohotkey.com/docs/v1/Variables.htm and Googled it extensively, and tried using

Code: Select all

:=
but no luck..

How can I achieve this please? Am I on the right lines? My final script will have 42 musical pieces, not 4 as in the example. Thank you for any assistance.

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Increment variables help please

Post by off » 19 Mar 2023, 05:09

Hello,
Perhaps

Code: Select all

z:=0
; each new line, add
z+=1 ; this equal to z:=z+1
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Increment variables help please

Post by off » 19 Mar 2023, 05:25

Here is the concept:

Code: Select all

^1::
typeDelay=40
eraseDelay=0
duration=1000
line=0
erase=50
totalLine=4

Line_1=A Glorious Life
Line_2=A Jolly Warm-up for Flute
Line_3=A Medley of Three Christmas Carols
Line_4=A Pause for Thought

Loop, %totalLine%
{
line+=1
SetKeyDelay, %typeDelay%
A_Line=% Line_%line%
Send, Piece %line%: %A_Line%
Sleep, %duration%
SetKeyDelay, %eraseDelay%
Send, {bs %erase%}
}
Return
Quick edit on A_Line var
Last edited by off on 19 Mar 2023, 06:49, edited 1 time in total.
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 19 Mar 2023, 06:39

That's fantastic, @orff. Thank you so much. I will play with this.

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 20 Mar 2023, 04:07

Hi @orff, the script you gave with the extra line (A_line) works perfectly and is exactly what I was looking for.... thanks a million. I also learned a lot from your script so I am most grateful for your assistance.
Just one thing - the moderators moved this thread to the "AHK Version 1" help section of the forum, but I presume it also works in Version 2? I'm not sure which version of AHK I am running...

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 20 Mar 2023, 04:09

Sorry, your name is @off, not @orff. Apologies.

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Increment variables help please

Post by off » 20 Mar 2023, 04:15

Moderator moved your thead to V1 because you using V1 syntax in your previous script, V1 and V2 syntax is somewhat similar, but you cant run V1 script with V2. You can install both version and run your V1/V2 scripts, as far per script you only use one version's syntax.

And,
Youre welcome, feel free to ask more :D
No problem, you can tag user with [ mention][ /mention] tho

Note: if you're not sure what ahk version you using, ipen autohotkey document with .chm ext. There will be your ahk version displayed afaik. But i guess you using V1 since my script work
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Increment variables help please

Post by off » 20 Mar 2023, 05:43

@jonjon
I improved the script, now the lyric can be just pasted into Song.txt in same folder as the script, and run it

Code: Select all

^1::
typeDelay=40
eraseDelay=0
duration=1000

Loop, Read, Song.txt
{
SetKeyDelay, %typeDelay%
A_Line=% Line_%line%
Send, Piece %A_Index%: %A_LoopReadLine%
Sleep, %duration%
SetKeyDelay, %eraseDelay%
Send, ^{a}{bs}
}
Return
Inside the Song.txt

Code: Select all

A Glorious Life
A Jolly Warm-up for Flute
A Medley of Three Christmas Carols
A Pause for Thought
For testing purposes, try this Song.txt >
Song.txt
Testing Song.txt
(755 Bytes) Downloaded 24 times
*note: you can change Song.txt to whatever you wanted, dont forget to change Loop, Read, Song.txt too
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 20 Mar 2023, 13:52

Ah, yes, thank you. I should have looked at https://www.autohotkey.com/v2/ before I posted that question!!
Woww, thank you for the amended script. I can now just use the external text file of song titles. That is BRILLIANT. I can't thank you enough, @off. Excellent.

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 20 Mar 2023, 14:13

Message for @off - this YouTube link gives a very rough idea of what the script achieves. - subtitles for a video. Look at the bottom-left corner... It's working very well so far! It's only a test though - more details in the YouTube description...
Thanks again....

https://youtu.be/IFRiSoIKaWs

I haven't had a chance to play with your latest script but I will do soon. I am very grateful.

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Increment variables help please

Post by off » 20 Mar 2023, 21:55

Great video! :bravo:
I hope you can optimize the script for a good uses

Have a great day!
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 21 Mar 2023, 04:42

Thanks @off!
I found a new use case!! Fancy video lyric subtitles! I had to do a bit of frame-by frame video editing to get the subtitles centered but I think they look OK....

https://youtu.be/zcPBDmnDIAQ

I actually adapted the first AHK version you sent me. But the second version (reading the text file) will also be useful for a later project... Thanks again!

Code: Select all

^1::
typeDelay=30     ;Key delay for text to appear
eraseDelay=10    ;Key delay for text to be erased (0  = instant)
duration=4000    ;Duration for the text to show before being erased
erase=60            ;Number of characters to delete
totalLine=19       ;Number of lines of text you need
line=0                ;Start line number (leave at 0)

Line_1 =       Yesterday, 
Line_2 =       All my troubles seemed so far away...
Line_3 =       Now it looks as though they're here to stay,
Line_4 =       Oh, I believe in yesterday.
Line_5 =       Suddenly, 
Line_6 =       I'm not half the man I used to be...
Line_7 =       There's a shadow hanging over me,
Line_8 =       Oh, yesterday came suddenly.
Line_9 =       Why she had to go, I don't know, she wouldn't say...
Line_10=       I said something wrong, now I long for yesterday...
Line_11=       Yesterday, love was such an easy game to play.
Line_12=       Now I need a place to hide away,
Line_13=       Oh, I believe in yesterday.
Line_14=       Why she had to go, I don't know, she wouldn't say.
Line_15=       I said something wrong, now I long for yesterday...
Line_16=       Yesterday, love was such an easy game to play.
Line_17=       Now I need a place to hide away,
Line_18=       Oh, I believe in yesterday.
Line_19=       Mm mm mm mm mm mm mm

Loop, %totalLine%
{
line+=1
SetKeyDelay, %typeDelay%
A_Line=% Line_%line%
Send, %A_Line%
Sleep, %duration%
SetKeyDelay, %eraseDelay%
Send, {bs %erase%}
}
Return

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Increment variables help please

Post by off » 21 Mar 2023, 04:53

The second script is recommended,
As easy as copy pasting text you need to the .txt, no need to typing Line_X= in the script.

And importantly, easy replacing text!
But, hey! Atleast you can understand how to use both script!
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 21 Mar 2023, 05:32

I agree @off. Second script is easier, but the lines of text didn't seem to be erased slowly, they just disappeared. Maybe I got some setting wrong. I will try again..
Writing the text was easy anyway in Notepad++. I used the "column editing" feature (ALT+select text). It allows you to copy a block of text without typing Line_X= each time.
I was pleased with the result. Thanks again. Have a great day!

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Increment variables help please

Post by off » 21 Mar 2023, 05:52

Sorry, i was using Ctrl+A and BackSpace instead of slowly erasing it, here:

Code: Select all

^1::
typeDelay=40
eraseDelay=0
duration=1000
erase=50 ; backspace amount

Loop, Read, Song.txt
{
SetKeyDelay, %typeDelay%
A_Line=% Line_%line%
Send, Piece %A_Index%: %A_LoopReadLine%
Sleep, %duration%
SetKeyDelay, %eraseDelay%
Send, {bs %erase%}
;Send, ^{a}{bs} ; instant backspacing
}
Return
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 21 Mar 2023, 06:46

Ah, OK - yes indeed, I should have noticed that! Thanks again @off, I will definitely be using this version from now on.
One question - I don't suppose there's an AHK solution for automatically centering the text when using Notepad or another text editor? That would be awesome...
Cheers!

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Increment variables help please

Post by off » 21 Mar 2023, 07:39

Microsoft Word : Ctrl+E (Middle Alignment)
Notepad : Since this is a plain text editor, there is no Middle Text Alignment, same as Notepad++ AFAIK.

To automatically use Ctrl+E, use this script.

Code: Select all

^1::
Send, ^{e} ; auto Middle Alignment in Microsoft Word
; etc..
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 21 Mar 2023, 09:26

Now that is a cool way of doing it. Thanks @off. I don't have Word ... but I do have LibreOffice Writer.... Just tried it. Close, but not quite what I would like - the text comes out from the middle. Not bad - in fact it's quite a nice text effect but I don't think it would be right for video subtitles. See this demo video..
https://youtu.be/jMy07LIVcn0

Now I'm looking for help with another AHK script!! I'd like to create a script that increases the number incrementally each time you click.
So, say in Notepad, click once, 1 appears,
click again - 2 appears,
click again - 3 appears... and so on.
Is this possible?
I'll have a go myself but I'm not an expert... yet... LOL...

jonjon
Posts: 53
Joined: 29 Jun 2020, 03:39

Re: Increment variables help please

Post by jonjon » 21 Mar 2023, 09:59

Something like this seems to work. I'd like to be able to click in a field in my video editor to increase the number by one each time. Problem is, I have to find a way of switching the script off when finished..

Code: Select all

SetKeyDelay,20
x:=0

lbutton::
click
Send, {bs 2}
x+=1
Send, %x%
Return

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Increment variables help please

Post by off » 21 Mar 2023, 18:32

Code: Select all

Esc::ExitApp
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

Post Reply

Return to “Ask for Help (v1)”