this post was submitted on 01 Jul 2024
1 points (55.6% liked)

Auto Hot Key

89 readers
1 users here now

Rules

  • Be polite/respectful to each other

  • You're expected to know if you're using v1 or v2

  • When asking for script help, make sure to include your code

  • When posting code, it needs to be formatted

  • Stay on the topic of AHK and/or programming

  • Replies should attempt to teach/help the user

    • No replies that are only self-promotional in nature
    • Linking to your own content is OK if it's relevant to the post and supplements your explanation
  • Please do not delete your post after receiving help from the community

    • This prevents others from being able to search for and utilize the answers others have provided
  • Do not respond to questions with AI-generated responses

  • Do not post ChatGPT code as Script Help

    • It's better to make a Script Request post

Latest AHK versions

AHK v2.0

AHK v1.1

v2.0.4

v1.1.37.01

Jul 3, 2023

Jul 8, 2023

Download

Download

v2 Changelog

v1 Changelog

Make sure you keep that AHK up to date!


Offical AHK Documentation


New to AutoHotkey?

Check out the AHK beginners tutorial. It covers most of the basic concepts of AutoHotkey.


Additional Help Sources


Live Chat

If you prefer live chat with other humans:

founded 1 year ago
MODERATORS
 

The goal

Bookmark a YouTube preroll ad with as few mouseclicks and keystrokes as possible.

But why?

I like to have the option of rewatching or referencing an interesting or funny ad at a later date. Most ads are unlisted videos, which makes them nigh impossible to look up.

My previous workflow (12 steps):

  1. Right-click on the video player, select "Copy debug info"
  2. Alt+Tab to a text editor
  3. Ctrl+V the debug info into text editor
  4. Ctrl+F for "addocid"
  5. Ctrl+C the advertisement video id
  6. Alt+Tab back to browser
  7. Ctrl+N to open new browser window
  8. Type "youtu.be/"
  9. Ctrl+V the video id
  10. Wait a fraction of a second for the URL to redirect from youtu.be/[video_id] to https://www.youtube.com/watch?v=[video_id]&feature=youtu.be to https://www.youtube.com/watch?v=[video_id]
  11. Ctrl+D to bookmark the video
  12. Ctrl+W to close the browser window

My new workflow (4 steps):

  1. Right-click on the video player, select "Copy debug info"
  2. Press F9 to run the script
  3. Check that the bookmark is saved to the correct folder
  4. Ctrl+W to close the browser tab

The AHK script:

#Requires AutoHotkey v2.0

F9:: ; Press F9, then a thing happens
{
	ActiveHwnd := WinExist("A")			; Save active window ID to variable
	Haystack := A_Clipboard				; Save clipboard to variable
	NeedleRegEx := 's).*"addocid": "(.*?)",.*'	; Regex witchcraft
	
	; Regex breakdown:
		; Example line: "addocid": "kU5Y-LRSteA",
		; We need to use "s)" otherwise it will treat each line in the clipboard as a separate string.
		; We need to flank the line we're searching for with ".*", otherwise it will search each line
		; We need to use the non-greedy "?" option when capturing the video ID "(.*?)" otherwise it will capture the rest of the file
	
	VidID := RegExReplace(Haystack, NeedleRegEx, "$1")		; Do the actual regex replacement
	URL := "https://www.youtube.com/watch?v=" . VidID		; Concatenate the video ID with the rest of a YouTube URL
	
	Run URL			; Open the video in the new tab
	Sleep 1000		; Wait for the tab to open
	WinActivate ActiveHwnd	; Reactivate the window
	Sleep 1000		; Wait again?
	Send "^d"		; Open bookmark dialogue and create bookmark
}

Potential improvements

  1. Automate clicking on "Copy debug info": I figured out how to use AHK to open the right-click menu in the YouTube player, but I can't figure out a reliable way to locate and click on "Copy debug info". It seems like it is in a different place depending on whether it is the 1st or 2nd preroll ad.
  2. Save bookmark in specific folder, rather than just the default bookmark dialogue box. No idea how to do this elegantly.

This is my first AHK project, so I've probably done several things incorrectly or inefficiently. Any feedback would be welcome :)

I'm so happy to have found an AHK community on Lemmy, however small!

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 2 points 4 months ago (1 children)

Wow! This is pretty intense. Thanks for sharing and welcome to the community

[โ€“] threelonmusketeers 1 points 4 months ago

Thanks! I'm still very new to AHK, so I probably did things wrong without knowing it. It's been an interesting learning experience so far though, and I'd love to get some feedback and learn more. Thanks for the warm welcome!