User Submitted CoyoteScript

MIDI Mouse Drag

Author: Sander

Script Version: 2

Minimum CoyoteMIDI Build: 553

Drags your mouse across a predefined path based on the MIDI value of the translation input. Ideal for controlling UI faders or sliders directly from MIDI.

This script adds the following translation parameters:

Start: The start pixel coordinates of the path to drag across. Coordinates relative to your mouse can be specified by prefixing them with a “~”.

End: The end pixel coordinates of the path to drag across. Coordinates relative to your mouse can be specified by prefixing them with a “~”.

Mouse Button: The button to press down and drag with. The options are: Left, Right, and Middle.

Release Delay: The time in seconds since the last input MIDI before the mouse button is released again.

Return Mouse: Whether when the mouse button is released, the mouse should be returned to the position it was at before the drag action began.
				
					// Validate if the input parameters are present
if (mapkeys $parameters) !contains pos1 || (mapkeys $parameters) !contains pos2 || !($parameters pos1) || !($parameters pos2)
    print "A start and end position must be defined first!"
    return

$start = (split ($parameters pos1) ",")
$end = (split ($parameters pos2) ",")

// Validate if the input parameters are formatted properly
if (count $start) != 2 || (count $end) != 2
    print "The start and end position must each contain an x and a y coordiante separated by a comma!"
    return

$id = ($trigger translationid)
$startTime = (time)
$previousStartTime = (%CC_mouse_drag_start_time $id)

// if this is the start of a new drag sequence, store where the mouse was before we start dragging
$beginDrag = $startTime - $previousStartTime > ($parameters releaseDelay)
if $beginDrag
    %CC_mouse_drag_return_position $id = (getmouse)
%CC_mouse_drag_start_time $id = $startTime

// if the start or end positions are relative, add the mouse position from before the drag sequence to the relative positions
$pos = (%CC_mouse_drag_return_position $id)
if (startswith "~" ($start 1))
    $start 1 = ($pos 1) + (after "~" ($start 1))
if (startswith "~" ($start 2))
    $start 2 = ($pos 2) + (after "~" ($start 2))
if (startswith "~" ($end 1))
    $end 1 = ($pos 1) + (after "~" ($end 1))
if (startswith "~" ($end 2))
    $end 2 = ($pos 2) + (after "~" ($end 2))

// Check whether the provided coordinates are valid numbers
if !(isnumber ($start 1)) || !(isnumber ($start 2)) || !(isnumber ($end 1)) || !(isnumber ($end 2))
    print "The start and end coordinates must be valid numbers!"
    return

// Get the progress of the MIDI value
$max = 127
if ($trigger midievent) == pitchbend
    $max = 16383
$progress = ($trigger rawvalue) / $max
indicator true (R: $progress * 511,G: 511 * (1 - $progress), B: 0) 1

// find the offset to the start position that the mouse should be at based on the end position and the MIDI progress
$offsetX = (round (($end 1) - ($start 1)) * $progress)
$offsetY = (round (($end 2) - ($start 2)) * $progress)

// Move the mouse';
movemouse (($start 1) + $offsetX),(($start 2) + $offsetY)

// if this was the start of a drag sequence, also press the mouse down
if $beginDrag
    click ($parameters button) default down

wait ($parameters releaseDelay)

// If this is the end of a drag sequence (based on timeout) return the mouse back to its original position
if $startTime == (%CC_mouse_drag_start_time $id)
    click ($parameters button) default up
    if ($parameters returnMouse)
        movemouse (%CC_mouse_drag_return_position $id)

// make sure the necessary variables are set as a map
event scriptsloaded
    %CC_mouse_drag_start_time new map
    %CC_mouse_drag_return_position new map

// add the UI parameters
event uirefreshed
    translationparameter pos1 (textbox "Start" "The starting position of the path to drag along." default 0.5)
    translationparameter pos2 (textbox "End" "The end position of the path to drag along." default 0.5)
    translationparameter button (combobox "Mouse Button" "The button of the mouse to hold down while dragging." "Left,Right,Middle" Left 0.5)
    translationparameter releaseDelay (numberbox "Release Delay" "The delay before the mouse drag is released and the mouse is returned to the original position." 0.3 default true 0 default 0.1)
    translationparameter returnMouse (checkbox "Return Mouse" "Whether the mouse should be returned to its original position after the drag ends." true)
				
			

22 downloads