Last active
February 23, 2017 08:07
-
-
Save nbogie/60b619e492d3c5cc7cf638fc4b74cd0b to your computer and use it in GitHub Desktop.
applescript (for midipipe) for MIDI control of VLC. Very basic due to VLC's limited applescript dictionary.
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
#VLC is bad at being controlled by applescript. | |
# Consider instead using Transcribe! for studying videos - it offers lots of midi control. | |
#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 | |
on VLCJumpTowardsProgress(fraction) | |
tell application "VLC" | |
set ct to current time | |
set dur to (duration of current item) | |
set targetTime to dur * fraction | |
my VLCJumpTowardsTime(targetTime) | |
end tell | |
end VLCJumpTowardsProgress | |
on VLCJumpTowardsTime(targetTime) | |
tell application "VLC" | |
set ct to current time | |
set dur to (duration of current item) | |
end tell | |
set delta to targetTime - ct | |
VLCJumpTowardsDelta(delta) | |
end VLCJumpTowardsTime | |
on VLCJumpTowardsDelta(delta) | |
set jumpStep to ComputeJumpStep(delta) | |
if jumpStep is not equal to 0 then | |
if jumpStep < 0 then | |
VLCBackwards(-jumpStep) | |
else | |
VLCForwards(jumpStep) | |
end if | |
end if | |
end VLCJumpTowardsDelta | |
on VLCTogglePlay() | |
tell application "VLC" | |
play | |
end tell | |
end VLCTogglePlay | |
on VLCStop() | |
tell application "VLC" | |
if playing then | |
play | |
end if | |
end tell | |
end VLCStop | |
on VLCBackwards(stepAmt) | |
tell application "VLC" | |
step backward stepAmt | |
end tell | |
end VLCBackwards | |
on VLCForwards(stepType) | |
tell application "VLC" | |
step forward stepType | |
end tell | |
end VLCForwards | |
on VLCFullscreen() | |
tell application "VLC" | |
fullscreen | |
end tell | |
end VLCFullscreen | |
on ComputeJumpStep(delta) | |
if delta > 180 then | |
return 4 | |
end if | |
if delta > 20 then | |
return 2 | |
end if | |
if delta > 5 then | |
return 1 | |
end if | |
if delta < -180 then | |
return -4 | |
end if | |
if delta < -20 then | |
return -2 | |
end if | |
if delta < -5 then | |
return -1 | |
end if | |
return 0 | |
end ComputeJumpStep | |
#our main handler called by midipipe when it gets a midi event | |
on runme(message) | |
#it's a control change... | |
if (item 1 of message = 191) then | |
#play button toggles play/pause | |
if (my isMidiButtonPressed(message, 117)) then | |
my VLCTogglePlay() | |
end if | |
#stop button pauses video | |
if (my isMidiButtonPressed(message, 116)) then | |
my VLCStop() | |
end if | |
#toggle fullscreen. Borked: Once full screen is toggled off, fullscreen is disabled | |
if (my isMidiButtonPressed(message, 113)) then | |
my VLCFullscreen() | |
end if | |
#click to next video | |
if (my isMidiButtonPressed(message, 57)) then | |
my VLCNextVideo() | |
end if | |
if (my isMidiButtonPressed(message, 51)) then | |
tell application "System Events" | |
tell application process "VLC" | |
set frontmost to true | |
keystroke "-" using command down | |
end tell | |
end tell | |
end if | |
if (my isMidiButtonPressed(message, 52)) then | |
tell application "System Events" | |
tell application process "VLC" | |
set frontmost to true | |
keystroke "=" using command down | |
end tell | |
end tell | |
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) | |
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) | |
my VLCJumpTowardsProgress(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) | |
#my VLCVolumeUp() | |
end if | |
#move playback position back a LOT, if prev section pressed | |
if (my isMidiButtonPressed(message, 110)) then | |
my VLCBackwards(4) | |
end if | |
#move playback position back a little, if rewind pressed | |
if (my isMidiButtonPressed(message, 114)) then | |
my VLCBackwards(2) | |
end if | |
#move playback position forward a LOT, if fwd section pressed | |
if (my isMidiButtonPressed(message, 111)) then | |
my VLCForwards(4) | |
end if | |
#move playback position forward a little, if ffwd pressed | |
if (my isMidiButtonPressed(message, 115)) then | |
my VLCForwards(2) | |
end if | |
end if | |
end runme | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
added very basic control of playback speed (via keypresses - VLC applescript dictionary doesn't expose that control, sadly)