-- ###BETA: UNTESTED###
-- Only works properly when run as a separate process!
-- NB: the OSC command will NOT WORK if the workspace has a Passcode
-- ###FIXME### The times displayed in the dialog are as shown in the waveform, not in the Action column (ie: confusing if rate ≠ 1)
-- Declarations
global dialogTitle
set dialogTitle to "Choose a slice"
-- Main routine
tell application id "com.figure53.QLab.4" to tell front workspace
try -- This protects against no selection (can't get last item of (selected as list))
set selectedCue to last item of (selected as list)
set sliceMarkers to slice markers of selectedCue -- This will throw an error if the cue doesn't take slices
if sliceMarkers is {} then error -- Abort if no slices
if running of selectedCue is true then
pause selectedCue
set startFlag to true
else
set startFlag to false
end if
set currentTime to (percent action elapsed of selectedCue) * (duration of selectedCue) * (rate of selectedCue)
-- ###FIXME### As of 4.6.6, "action elapsed" reports differently between clicking in waveform and loading to time when rate ≠ 1
-- Testing revealed rounding errors of the order of 1.0E-12 when comparing times so they are truncated to milliseconds for comparison
set roundedCurrent to my roundToNearest(currentTime, 1.0E-3)
set sliceMarkers to slice markers of selectedCue
set sliceTimes to {0}
repeat with eachMarker in sliceMarkers
set end of sliceTimes to time of eachMarker
end repeat
set countSliceTimes to count sliceTimes
set readableTimes to {}
repeat with eachTime in sliceTimes
set end of readableTimes to my makeHHMMSSsss(eachTime)
end repeat
repeat with i from 1 to countSliceTimes
if (my roundToNearest(item i of sliceTimes, 1.0E-3)) - roundedCurrent > 0 then
exit repeat -- We have found the next slice start
end if
end repeat
set loadTimeReadable to my pickFromListCustomDefault(readableTimes, "Choose which slice to jump to:", i - 1)
repeat with i from 1 to countSliceTimes
if item i of readableTimes is loadTimeReadable then
set loadTime to item i of sliceTimes
exit repeat
end if
end repeat
set eachID to uniqueID of selectedCue
tell me to do shell script "echo '/cue_id/" & eachID & "/loadActionAt " & loadTime & "' | nc -u -w 0 localhost 53535"
if startFlag is true then
start selectedCue
end if
end try
end tell
-- Subroutines
(* === INPUT === *)
on pickFromListCustomDefault(theChoice, thePrompt, theDefault) -- [Shared subroutine]
tell application id "com.figure53.QLab.4"
choose from list theChoice with prompt thePrompt with title dialogTitle default items item theDefault of theChoice
if result is not false then
return item 1 of result
else
error number -128
end if
end tell
end pickFromListCustomDefault
(* === TIME === *)
on makeHHMMSSsss(howLong) -- [Shared subroutine]
set howManyHours to howLong div 3600
if howManyHours is 0 then
set hourString to ""
else
set hourString to my padNumber(howManyHours, 2) & ":"
end if
set howManyMinutes to (howLong mod 3600) div 60
set minuteString to my padNumber(howManyMinutes, 2)
set howManySeconds to howLong mod 60 div 1
set secondString to my padNumber(howManySeconds, 2)
set howManyFractionalSeconds to howLong mod 1
set howManyRoundedSeconds to round 1000 * howManyFractionalSeconds rounding as taught in school
set fractionString to my padNumber(howManyRoundedSeconds, 3)
return hourString & minuteString & ":" & secondString & "." & fractionString
end makeHHMMSSsss
(* === NUMBER WRANGLING === *)
on roundToNearest(theNumber, toNearest) -- [Shared subroutine]
return (round (theNumber / toNearest) rounding as taught in school) * toNearest
end roundToNearest
(* === TEXT WRANGLING === *)
on padNumber(theNumber, minimumDigits) -- [Shared subroutine]
set paddedNumber to theNumber as text
repeat while (count paddedNumber) < minimumDigits
set paddedNumber to "0" & paddedNumber
end repeat
return paddedNumber
end padNumber