QLab Script - stopwatch fade maker

Contents

Table of Contents

This is an "external script" - see QLab Scripts and Macros

 
(* Stopwatch fade maker *)
 
--
 
set theExplanation to "This script will play the last Audio Cue currently selected in QLab and allow you to capture a list of times relative to the start of the cue. " & ¬
    "It will then make a series of Fade Cues (with your default duration), each with a Pre Wait to match one of those times. " & ¬
    "The list will also be copied to the Clipboard.
 
Currently it is only accurate to ±1s; in the future it may actually be useful..."
 
--
 
(* This script is not designed to be run from within QLab!
 
v0.9: 30/09/09 Rich Walsh (proof of concept)
v0.9.1: 04/10/09 Found more efficient ways of handling lists
v0.9.2: 12/10/09 Snow Leopard can't "get running", so rewrote a sequence
v0.9.3: 16/10/09 Now "tested" in Snow Leopard
v0.9.4: 27/10/09 Removed unused variable
v1.0: 11/01/10 Corrected minor typos; switched to using playback position to navigate; added tell front workspace for elegance;
        wrapped text for better wiki experience; implemented dialogTitle for cross-script pillaging; new explanation format
 
<<< Last tested with: QLab 2.2.6; Mac OS 10.5.8 & 10.6.2 >>> *)
 
-- ###ADD### When (if) possible, pull more accurate times from QLab itself
 
-- Declarations
 
global dialogTitle
set dialogTitle to "Stopwatch fade maker"
 
-- Preamble
 
display dialog theExplanation & return & return & "Are you sure you wish to proceed?" with title dialogTitle with icon 1 ¬
    buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel"
 
-- Check QLab is running
 
tell application "System Events"
    set qLabIsRunning to count (every process whose name is "QLab")
end tell
if qLabIsRunning is 0 then
    display dialog "QLab is not running." with title dialogTitle with icon 0 buttons {"OK"} default button "OK" giving up after 5
    return
end if
 
tell application "QLab"
 
    activate
 
    -- Test for a workspace and a selection
 
    set noSelection to false
    try
        set theSelection to (selected of front workspace as list)
        set countSelection to count theSelection
        if countSelection is 0 then
            set noSelection to true
        end if
    on error
        set noSelection to true
    end try
    if noSelection is true then
        display dialog "There is no selection in QLab." with title dialogTitle with icon 0 buttons {"OK"} default button "OK" giving up after 5
        return
    end if
 
    -- Find the last Audio Cue in the selection and check it has a valid target, or give up
 
    set badSelection to true
    set hasTarget to ""
    repeat with i from countSelection to 1 by -1
        if q type of item i of theSelection is "Audio" then
            set badSelection to false
            set theAudioCue to item i of theSelection
            try
                set targetTest to (file target of theAudioCue) as string
                if targetTest is "missing value" then
                    set hasTarget to "The last selected Audio Cue has lost its target."
                end if
            on error
                set hasTarget to "The last selected Audio Cue does not have a valid target."
            end try
            exit repeat
        end if
    end repeat
    if badSelection is true then
        display dialog "QLab's selection does not contain an Audio Cue." with title dialogTitle with icon 0 buttons {"OK"} default button "OK" giving up after 5
        return
    end if
    if hasTarget is not "" then
        display dialog hasTarget with title dialogTitle with icon 0 buttons {"OK"} default button "OK" giving up after 5
        return
    end if
 
    -- Start the file & get the times
 
    set timesList to {}
    set timeString to ""
    set returnString to ""
 
    tell front workspace
 
        set playback position of current cue list to theAudioCue
        stop
        go
        moveSelectionUp -- Move to end of previous cue sequence (that contains the Audio Cue) so that fades are inserted after it (not after the next cue);
        (* only works properly if "Playback position is always the selected cue" is ON! *)
 
        set startTime to time of (current date)
 
        repeat
            set buttonPush to button returned of (display dialog "Timing..." & return & return & timeString with title dialogTitle with icon 1 ¬
                buttons {"Done", "Mark time"} default button "Mark time")
            if buttonPush is "Mark time" then
                set lapTime to ((time of (current date)) - startTime) -- This can only pull times to the nearest second!
                copy lapTime to end of timesList
                set timeString to timeString & returnString & my makeMMSS(lapTime)
            else
                exit repeat
            end if
            set returnString to return
        end repeat
 
        stop
 
        -- Make the fades
 
        repeat with eachTime in timesList
            make type "Fade"
            set newCue to last item of (selected as list)
            set cue target of newCue to theAudioCue
            set pre wait of newCue to eachTime
            set q name of newCue to "Fade: " & q name of theAudioCue
        end repeat
 
    end tell
 
    if (count timesList) is 0 then
        display dialog "No times captured." with title dialogTitle with icon 0 buttons {"OK"} default button "OK"
    else
        display dialog "Your times were (they're also now on the Clipboard):" & return & return & timeString with title dialogTitle with icon 1 ¬
            buttons {"OK"} default button "OK"
        set the clipboard to timeString
    end if
 
end tell
 
-- Subroutines
 
on makeMMSS(howLong)
    set howManyMinutes to howLong div 60
    if howManyMinutes > 9 then
        set minuteString to (howManyMinutes as string)
    else
        set minuteString to "0" & (howManyMinutes as string)
    end if
    set howManySeconds to howLong mod 60 as integer
    if howManySeconds > 9 then
        set secondString to (howManySeconds as string)
    else
        set secondString to "0" & (howManySeconds as string)
    end if
    return minuteString & ":" & secondString
end makeMMSS
 
(* END: Stopwatch fade maker *)