Find timecode numbers in clipboard and wrap Square Brackets around it (for Reclipped and Roam Research)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKfan2021
Posts: 3
Joined: 08 May 2021, 06:19

Find timecode numbers in clipboard and wrap Square Brackets around it (for Reclipped and Roam Research)

08 May 2021, 06:41

Hi, I am new to coding but as a daily video subtitle editor I usually have to deal with timecoded contents like this. I have been searching around AHK and the web for similar solutions but so far nothing pops up. Some help would be greatly appreciated! Thank you guys in advance!

Code: Select all

0:00 Intro
1:40 Exterior 
2:40 Entry
3:15 Office 
4:50 Powder Room
1:25:10 Living Room
Where number : number : number is the timecode (hh:mm:ss)

I am trying to automatically turn into this:

Code: Select all

[[0:00]] Intro
[[1:40]] Exterior 
[[2:40]] Entry
[[3:15]] Office 
[[4:50]] Powder Room
[[1:25:10]] Living Room
Basically it parses the clipboard, finds the strings with timecoded numbers in it, and wraps the whole number part in square brackets thing, leaving the texts intact and return the result

So far I only found some codes to wrap the whole line by line, not to just wrap the needed part

Code: Select all

InputBox, VarThemDauCuoiCaigi, Them Dau Cuoi cai gi
VarThemDauThemDuoi=
Loop, parse, clipboard, `n, `r
{
If (!VarThemDauThemDuoi) ; If nothing is in x
   VarThemDauThemDuoi := VarThemDauCuoiCaigi A_LoopField VarThemDauCuoiCaigi
else ; If there is something in x, add to it
    VarThemDauThemDuoi .= "`r`n" VarThemDauCuoiCaigi A_LoopField VarThemDauCuoiCaigi
}
clipboard:=VarThemDauThemDuoi

sleep 100
TrayTip, Output here, %clipboard%, 3, 1
return
I think I will have to use RegExReplace or something similar to this, but this is what I've got so far which doesn't seem to work

Code: Select all

Variable = 20:00 Intro dsf 2221:40 Exterior 222

RegExMatch(variable, "\d+`:\d+", var)
RegExReplace(var,"\d+%var%\d+", , )
MsgBox % var

return

And even more advanced, how do I do it with longer times where it runs into hh:mm:ss (3 sets of numbers like the last line "[[1:25:10]] Living Room", instead of mm:ss)

Really appreciate your help!
User avatar
mikeyww
Posts: 27082
Joined: 09 Sep 2014, 18:38

Re: Find timecode numbers in clipboard and wrap Square Brackets around it (for Reclipped and Roam Research)

08 May 2021, 07:54

Code: Select all

str =
(
0:00 Intro
1:40 Exterior 
2:40 Entry
3:15 Office 
4:50 Powder Room
1:25:10 Living Room
)
SendInput % "{Text}" new := RegExReplace(str, "m`a)^(\S+)", "[[$1]]")
AHKfan2021
Posts: 3
Joined: 08 May 2021, 06:19

Re: Find timecode numbers in clipboard and wrap Square Brackets around it (for Reclipped and Roam Research)

08 May 2021, 10:52

Jesus, to a noob like me you are like a wizard! Thank you so much, didn't know that it would all be included in just a line of code like that! Elegant!

I will try to digest and dissect it gradually to learn from you the maestro!

Many thanks again!
AHKfan2021
Posts: 3
Joined: 08 May 2021, 06:19

Re: Find timecode numbers in clipboard and wrap Square Brackets around it (for Reclipped and Roam Research)

31 May 2021, 04:43

Just a small update to his amazing script: heres the code to do it if the Timecode is at the END (not the beginning) of the line

Code: Select all

str =
(
Intro 0:00
Exterior 1:40
Entry 2:40
Office 3:15
Powder Room 4:50
Living Room 1:25:10
)
clipboard:=RegExReplace(clipboard, "m`a)(\S+)$", "[[$1]]")
;m multiline
; ^abc beginning
; abc$ end
It happens sometimes, for certain notes taken by some Youtube channel. Now we can do both ways!

Just post this in case someone will need it, thank you mikey for providing me this wonderful opportunity to learn!
User avatar
mikeyww
Posts: 27082
Joined: 09 Sep 2014, 18:38

Re: Find timecode numbers in clipboard and wrap Square Brackets around it (for Reclipped and Roam Research)

31 May 2021, 06:32

Good! It looks like you figured out a few things here. \S means "not white space"; + means "at least one of those, through the last consecutive one".

Without a ClipWait, you might need a short Sleep (e.g., 80 ms) at the end of the routine before the clipboard is updated and available-- if there is more to the script after this.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 270 guests