Convert seconds to specific format in ahk?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Convert seconds to specific format in ahk?

Post by milkygirl90 » 28 Jun 2021, 23:22

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 ?

image.png
image.png (5.94 KiB) Viewed 1035 times
Attachments
image.png
image.png (5.94 KiB) Viewed 1035 times

Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Convert seconds to specific format in ahk?

Post by Rohwedder » 28 Jun 2021, 23:50

Hallo,
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

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Convert seconds to specific format in ahk?

Post by AHKStudent » 28 Jun 2021, 23:58

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

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Convert seconds to specific format in ahk?

Post by boiler » 28 Jun 2021, 23:59

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)
	}
}

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Convert seconds to specific format in ahk?

Post by milkygirl90 » 29 Jun 2021, 00:35

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:

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

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Convert seconds to specific format in ahk?

Post by AHKStudent » 29 Jun 2021, 01:34

milkygirl90 wrote:
29 Jun 2021, 00:35
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:

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
Just run the script, copy a youtube link that has the time at the end (3705) then hit paste, you will see it converted it

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Convert seconds to specific format in ahk?

Post by AHKStudent » 29 Jun 2021, 01:39

boiler wrote:
28 Jun 2021, 23:59
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)
	}
}
hi

what is the function of these lines

Code: Select all

OnClipboardChange("GetYouTubeTime", 0)
		
		OnClipboardChange("GetYouTubeTime", 1)
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 do

thanks

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Convert seconds to specific format in ahk?

Post by milkygirl90 » 29 Jun 2021, 02:13

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?

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Convert seconds to specific format in ahk?

Post by boiler » 29 Jun 2021, 03:05

milkygirl90 wrote: @boiler , may I confirm I just need to add the hotkey trigger for it to work?
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.

AHKStudent wrote: what is the function of these lines

Code: Select all

OnClipboardChange("GetYouTubeTime", 0)
		
		OnClipboardChange("GetYouTubeTime", 1)
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 do
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.

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?
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.

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Convert seconds to specific format in ahk?

Post by milkygirl90 » 29 Jun 2021, 03:19

You were right - I missed out the auto-execute portion should be put at the very top. Thank you so much!

User avatar
emrekarahan0001
Posts: 25
Joined: 18 Jan 2021, 14:19

Re: Convert seconds to specific format in ahk?

Post by emrekarahan0001 » 09 Jul 2021, 10:08

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

Post Reply

Return to “Ask for Help (v1)”