Last active
February 2, 2018 04:42
-
-
Save nbogie/fbbfc0f53c374182374b792ab023cbd1 to your computer and use it in GitHub Desktop.
Applescript (for Midipipe) to control playback of youtube, vimeo, netflix, etc (in chrome), via MIDI device. Allows control of playback position, volume, playback speed, as well as play/pause, from midi messages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#returns true if the message represents a positive change of | |
# the given controller number, as will happen when depressing buttons a midi keyb. | |
# We ignore messages with value bytes 0, as those are sent on button releases. | |
on isMidiButtonPressed(msg, controllerNumber) | |
return (isControlChange(msg, controllerNumber) and (item 3 of msg > 0)) | |
end isMidiButtonPressed | |
on isControlChange(msg, controllerNumber) | |
return ((item 1 of msg = 191) and (item 2 of msg = controllerNumber)) | |
end isControlChange | |
#our main handler called by midipipe when it gets a midi event | |
on runme(message) | |
tell application "Google Chrome" | |
#it's a control change... | |
if (item 1 of message = 191) then | |
repeat with t in tabs of windows | |
tell t | |
if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" or URL starts with "https://vimeo.com/" or URL starts with "https://www.netflix.com/" then | |
#play button toggles play/pause | |
if (my isMidiButtonPressed(message, 117)) then | |
execute javascript " | |
var v = document.querySelector('video'); | |
if (v.paused) { | |
v.play(); | |
} | |
else { | |
v.pause(); | |
} | |
" | |
end if | |
#stop button pauses video | |
if (my isMidiButtonPressed(message, 116)) then | |
execute javascript "document.querySelector('video').pause()" | |
end if | |
#toggle fullscreen. Borked: Once full screen is toggled off, fullscreen is disabled | |
if (my isMidiButtonPressed(message, 56)) then | |
execute javascript "document.querySelector('.ytp-fullscreen-button').click()" | |
end if | |
#click to next video | |
if (my isMidiButtonPressed(message, 57)) then | |
execute javascript "document.querySelector('.ytp-next-button').click()" | |
end if | |
#change playback speed, based on a continuous controller's value | |
if (my isControlChange(message, 17)) then | |
set speed to (6 * (item 3 of message) / 127) | |
execute javascript " | |
var v = document.querySelector('video'); | |
v.playbackRate = " & speed & "; | |
" | |
end if | |
#change playback position, based on a continuous controller's value | |
if (my isControlChange(message, 24)) then | |
set timescaling to ((item 3 of message) / 127) | |
execute javascript " | |
var v = document.querySelector('video'); | |
v.currentTime = v.duration * " & timescaling & "; | |
" | |
end if | |
#change volume, based on a continuous controller's value | |
if (my isControlChange(message, 41)) then | |
set volumescaling to ((item 3 of message) / 127) | |
execute javascript " | |
var v = document.querySelector('video'); | |
v.volume = " & volumescaling & "; | |
" | |
end if | |
#move playback position back a little, if rewind pressed | |
if (my isMidiButtonPressed(message, 114)) then | |
execute javascript " | |
var v = document.querySelector('video'); | |
v.currentTime = v.currentTime - 10; | |
" | |
end if | |
#move playback position forward a little, if ffwd pressed | |
if (my isMidiButtonPressed(message, 115)) then | |
execute javascript " | |
var v = document.querySelector('video'); | |
v.currentTime = v.currentTime + 30; | |
" | |
end if | |
exit repeat | |
end if | |
end tell | |
end repeat | |
end if | |
end tell | |
end runme |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment