Convert seconds to specific format in ahk?
- milkygirl90
- Posts: 565
- Joined: 10 Nov 2020, 21:22
Convert seconds to specific format in ahk?
I want to be able to easily copy the current playback position (1:01:45 in this example) to clipboard in YouTube.
I intend to right click > copy URL with current time, which displays as https://youtu.be/bW3BssTdfyOc?t=3705
How do I copy the 3705 just after the =, and how do I convert from 3705 to this format 1:01:45 ?
I intend to right click > copy URL with current time, which displays as https://youtu.be/bW3BssTdfyOc?t=3705
How do I copy the 3705 just after the =, and how do I convert from 3705 to this format 1:01:45 ?
- Attachments
-
- image.png (5.94 KiB) Viewed 1034 times
Re: Convert seconds to specific format in ahk?
Hallo,
try:
try:
Code: Select all
Url = https://youtu.be/bW3BssTdfyOc?t=3705
RegExMatch(Url, "\d+$", Seconds)
Time = 20210101000000
Time += Seconds, Seconds
FormatTime, Time, %Time%, H:mm:ss
MsgBox,% Time
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Convert seconds to specific format in ahk?
same idea what boiler shared, here is a function I found https://www.autohotkey.com/boards/viewtopic.php?t=80485
Code: Select all
MsgBox, % FormatSeconds(3705)
FormatSeconds(NumberOfSeconds) ; Convert the specified number of seconds to hh:mm:ss format. ;; Mentioned above
{
time = 19990101 ; *Midnight* of an arbitrary date.
time += %NumberOfSeconds%, seconds
FormatTime, mmss, %time%, mm:ss
return NumberOfSeconds//3600 ":" mmss
}
ExitApp
Re: Convert seconds to specific format in ahk?
This makes use of Rohwedder's script and adds the functionality that operates on copied youtube links that meet that format and puts the result back in the clipboard ready for pasting. Anything else copied remains in the clipboard unchanged.
Code: Select all
#Persistent
OnClipboardChange("GetYouTubeTime")
return
GetYouTubeTime() {
if RegExMatch(Clipboard, "https://youtu.*?\K\d+$", Seconds) {
Time = 20210101000000
Time += Seconds, Seconds
FormatTime, Time, %Time%, H:mm:ss
OnClipboardChange("GetYouTubeTime", 0)
Clipboard := Time
OnClipboardChange("GetYouTubeTime", 1)
}
}
- milkygirl90
- Posts: 565
- Joined: 10 Nov 2020, 21:22
Re: Convert seconds to specific format in ahk?
Thanks all for the responses!
@boiler , may I confirm I just need to add the hotkey trigger for it to work? I tried it and it didn't output anything:
@boiler , may I confirm I just need to add the hotkey trigger for it to work? I tried it and it didn't output anything:
Code: Select all
#Persistent
^v::OnClipboardChange("GetYouTubeTime")
return
GetYouTubeTime() {
if RegExMatch(Clipboard, "https://youtu.*?\K\d+$", Seconds) {
Time = 20210101000000
Time += Seconds, Seconds
FormatTime, Time, %Time%, H:mm:ss
OnClipboardChange("GetYouTubeTime", 0)
Clipboard := Time
OnClipboardChange("GetYouTubeTime", 1)
}
}
return
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Convert seconds to specific format in ahk?
Just run the script, copy a youtube link that has the time at the end (3705) then hit paste, you will see it converted itmilkygirl90 wrote: ↑29 Jun 2021, 00:35Thanks all for the responses!
@boiler , may I confirm I just need to add the hotkey trigger for it to work? I tried it and it didn't output anything:
Code: Select all
#Persistent ^v::OnClipboardChange("GetYouTubeTime") return GetYouTubeTime() { if RegExMatch(Clipboard, "https://youtu.*?\K\d+$", Seconds) { Time = 20210101000000 Time += Seconds, Seconds FormatTime, Time, %Time%, H:mm:ss OnClipboardChange("GetYouTubeTime", 0) Clipboard := Time OnClipboardChange("GetYouTubeTime", 1) } } return
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Convert seconds to specific format in ahk?
hiboiler wrote: ↑28 Jun 2021, 23:59This makes use of Rohwedder's script and adds the functionality that operates on copied youtube links that meet that format and puts the result back in the clipboard ready for pasting. Anything else copied remains in the clipboard unchanged.Code: Select all
#Persistent OnClipboardChange("GetYouTubeTime") return GetYouTubeTime() { if RegExMatch(Clipboard, "https://youtu.*?\K\d+$", Seconds) { Time = 20210101000000 Time += Seconds, Seconds FormatTime, Time, %Time%, H:mm:ss OnClipboardChange("GetYouTubeTime", 0) Clipboard := Time OnClipboardChange("GetYouTubeTime", 1) } }
what is the function of these lines
Code: Select all
OnClipboardChange("GetYouTubeTime", 0)
OnClipboardChange("GetYouTubeTime", 1)
thanks
- milkygirl90
- Posts: 565
- Joined: 10 Nov 2020, 21:22
Re: Convert seconds to specific format in ahk?
I pasted the exact code inside, and now the pasted output is unchanged (i.e. the full YouTube link and not the formatted timestamp) when it is nested inside a #IfWinActive ahk_group VideoSites, but works when it's outside of #IF. any idea why?
Re: Convert seconds to specific format in ahk?
No, do not trigger any part of it with a hotkey (unless you want a hotkey to toggle this capability on and off, but not to actually invoke it, and if you want to do that, then it needs to be changed to implement a toggle). The OnClipboardChange(…) line (the first one, not the ones inside the function) needs to be at the top of your script in the auto-execute section, before any hotkeys or anything. By executing that line once, it sets up the automatic trigger to execute the function whenever the clipboard changes (and the function itself will then only change the clipboard when it is supposed to). Put the function almost anywhere, like at the very bottom of your script, not part of any hotkey or anything.milkygirl90 wrote: ↑ @boiler , may I confirm I just need to add the hotkey trigger for it to work?
You should not remove them because it then needlessly causes the function to trigger itself (luckily not in an infinite loop in this case). The one with a 0 turns off the automatic triggering of the function when the clipboard changes, and the other one turns it back on. They are there so that the function isn’t triggering itself when it changes the clipboard. Yes, it works without them in this case, causing it to call itself again, then finds there is nothing to change the second time around. If you make other changes, you could find it continually calling itself, probably without you realizing it, and using a bunch of CPU.AHKStudent wrote: ↑ what is the function of these lines
I looked in the documentation but didn't understand how this functions here, it did work without those too so I would like to know what they doCode: Select all
OnClipboardChange("GetYouTubeTime", 0) OnClipboardChange("GetYouTubeTime", 1)
It has nothing to do with being inside of an #If block because those only affect hotkeys (and it doesn’t make sense to put it under a hotkey since that’s not how it is triggered). It is because when you put it there, the OnClipboardChange() line never gets executed, which it needs to happen only once, and it won’t get executed at all if it’s not at the top of the script in the auto-execute section. The function itself doesn’t get affected by #If directives either. Put it out of the way of everything, like at the very bottom of your script.milkygirl90 wrote: ↑ I pasted the exact code inside, and now the pasted output is unchanged (i.e. the full YouTube link and not the formatted timestamp) when it is nested inside a #IfWinActive ahk_group VideoSites, but works when it's outside of #IF. any idea why?
- milkygirl90
- Posts: 565
- Joined: 10 Nov 2020, 21:22
Re: Convert seconds to specific format in ahk?
You were right - I missed out the auto-execute portion should be put at the very top. Thank you so much!
- emrekarahan0001
- Posts: 25
- Joined: 18 Jan 2021, 14:19
Re: Convert seconds to specific format in ahk?
Minute And Second To Convert Miliseconds
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
Gui, +ToolWindow
Gui, Color, 0d0d0d
Gui, Font, s10
Gui, Add, Text, x5 y10 c00fcc2, input Minutes And Seconds and press "Convert"
Gui, Add, Edit, x5 y40 w50 h20 vMinute,
Gui, Add, Edit, x60 y40 w50 h20 vSecond,
Gui, Add, Button, x+5 y40 w70 h20, Convert
Gui, Show, xCenter yCenter w300 h100, Minutes And Seconds To Convert Miliseconds
Return
ButtonConvert:
GuiControlGet, Minute
GuiControlGet, Second
vMin := Minute
vSec := Second
vTotal := vMin*60000+vSec*1000
Gui, Add, Text, x5 y70 c00dffc, MM:SS = %vTotal% (MilliSeconds)
Return
GuiEscape:
GuiClose:
ExitApp