Send Shift-N to a Google Chrome Tab (YouTube)

Maybe it's simple, shift N moves to next song in google chrome youtube playlists, but I want to do when not in window, bettertouchtool send to application shift N doesn't work, maybe it can be done easier through KM?

Hey @Lon_Ron,

There's no way I know of to send a keystroke to a specific video in a specific tab of an application in the background.

If you knew enough JavaScript it might be possible to do the job with an Execute a JavaScript in Google Chrome action, but I wouldn't bet on it.


yeah I think it would be complex, I'm dumb though, beardedspice is a mac app that does that, can send media keys to various players, forgot about that, and then I route that through btt and it works, so it's good. thanks for the reply.

what the heck, that was a nice challenge.

Youtube Next.kmmacros (2.2 KB)

I have a particular interest in getting inside the workings of web video players. I hope this works for you. It will prompt a user confirmation to allow KM to control Chrome.

the macro just contains this appleScript:

# Youtube Next Track (or next video), which is manually triggered by typing Shift-N
#	-daf 3/1/21

tell application "Google Chrome"
	# find the tab with youtube.
	# the title of the tab is just the title of the video or track, ie not useful.
	# so find a tab whose URL contains "youtube"
	# other urls could trick this script if they contain the string.

	repeat with aWindow in the windows
		set youTab to (every tab in aWindow whose URL contains "youtube")
		if youTab is not {} then
			my YouTubeNext(youTab)
		end if
	end repeat
end tell

# "ytp-next-button" is the real secret sauce. and the most likely thing to break
# but it seems to be a unique identifier for the "next" button. 
# tell it that it's received a "click()"
on YouTubeNext(theTab)
	set jscriptCommand to "document.getElementsByClassName('ytp-next-button')[0].click()"
	tell application "Google Chrome"
		execute theTab javascript jscriptCommand
	end tell
end YouTubeNext

thanks avialan, the people on KM forum are the smartest nicest applescripters :slightly_smiling_face:

for other controls:

the key is in that 'secret sauce' i mentioned above - a unique identifier for the various buttons. One could create a different function for each, cloned off the 'YouTubeNext' function above with just the button code changed. Here are some other button codes I see, names seem pretty self-explanatory.
ytp-play-button (i think this doubles as pause)

1 Like

cool, thanks for those.