QLab Script - reporter with levels

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


# QLab reporter with Levels
# © 2009 Jeremy Lee

global tempContents, file_name, NewContents


tell application "QLab"
    set tempContents to {}
    set progress_factor to 10
 
    set file_name to (choose file name with prompt "Enter a name for the exported QLab file:" default name "QLab Export")
    if file_name is false then error number -128 -- cancel
    if file_name as string does not end with ".txt" then set file_name to ((file_name as string) & ".txt")
 
    my tell_everybody("Getting started...", 1)
 
 
    (* This stuff is for the matrix reporting *)
 
    set numOfColumns to 24 (* How many matrix outs do you want to look at? *)
 
    set matrixTitles to {"Master" & tab & "Input 1" & tab & "Input 2"}
 
    set surTitle to "Output "
 
    repeat with curNum from 1 to numOfColumns
        copy (tab & surTitle & " " & curNum) as string to end of matrixTitles
    end repeat
 
    set surTitle to "Matrix 1/"
 
    repeat with curNum from 1 to numOfColumns
        copy (tab & surTitle & " " & curNum) as string to end of matrixTitles
    end repeat
 
    set surTitle to "Matrix 2/"
 
    repeat with curNum from 1 to numOfColumns
        copy (tab & surTitle & " " & curNum) as string to end of matrixTitles
    end repeat
 
    (* End matrix reporting titles *)
 
    tell me to add_this_to_tempContents("Index" & tab & "CueID" & tab & "Q Number" & tab & "Q Type" & tab & "Q Name" & tab & "Q Target" & tab & "File Target" & tab & "Pre Wait" & tab & "Duration" & tab & "Post Wait" & tab & "Continue" & tab & "Cue Start" & tab & "Cue End" & tab & "Loop Start" & tab & "Loop End" & tab & "Num Loops" & tab & "Inf Loop" & tab & "Internal Fade" & tab & "Fade Mode" & tab & "Stop Fade" & tab & "MIDIPatch" & tab & "MIDICmd" & tab & "MIDIChan" & tab & "MIDIByte1" & tab & "MIDIByte2" & tab & "MIDIStart" & tab & "MIDIEnd" & tab & "MIDIFade" & tab & "Broken?" & tab & matrixTitles & return)
 
 
 
    tell workspace 1
        set numCues to count of cues
        repeat with curCueNum from 1 to numCues
 
 
 
            set fullCue to (a reference to cue curCueNum)
 
            set cueIndex to curCueNum
 
            set cueID to uniqueID of fullCue
            set currentNum to q number of fullCue
            set currentType to q type of fullCue
 
            tell me to set pWait to transform_wait_to_time(pre wait of fullCue)
            tell me to set curDur to transform_seconds_to_time(duration of fullCue)
            tell me to set pstWait to transform_wait_to_time(post wait of fullCue)
 
            set curCont to continue mode of fullCue
            if curCont as string is "do_not_continue" then set curCont to ""
            if curCont as string is "auto_continue" then set curCont to "Cont."
            if curCont as string is "auto_follow" then set curCont to "Follow"
 
            set cueBroken to broken of fullCue
 
            if currentType as string is "Group" then
                set currentMode to mode of fullCue
                if currentMode as string is "cue_list" then set currentType to "Cue List"
                if currentMode as string is "fire_first_enter_group" then set currentType to "Group - Enter"
                if currentMode as string is "fire_first_go_to_next_cue" then set currentType to "Group - Go To Next"
                if currentMode as string is "fire_all" then set currentType to "Group - Fire All"
                if currentMode as string is "fire_random" then set currentType to "Group - Random"
 
                set curDur to ""
 
                set CuesInGroup to count of cues in fullCue -- Do something with this later to indicate individual cues in groups
                set currentType to currentType & "; Cues in group: " & CuesInGroup
 
            end if
 
            if currentType as string is "Audio" then
 
                tell me to set cueStart to transform_seconds_to_time(start time of fullCue)
 
                (* Can't quite remember why this is in here
 
                if start time of fullCue > 0 then
                    set cueStart to start time of fullCue
                else
                    set cueStart to "0"
                end if
*)
 
 
                tell me to set cueEnd to transform_seconds_to_time(end time of fullCue)
 
                if loop start time of fullCue > 0 then
                    tell me to set loopStart to transform_seconds_to_time(loop start time of fullCue)
                else
                    set loopStart to "Top"
                end if
 
                tell me to set loopEnd to transform_seconds_to_time(loop end time of fullCue)
                if loopEnd = cueEnd then set loopEnd to "End"
 
                set loopNum to loop count of fullCue
                set loopInf to infinite loop of fullCue
 
                (* This is here because QLab 2.07 does not properly report the infiniteloop value - is this fixed? *)
                if loopInf as string is not "true" then set loopInf to "False"
 
                set intFade to integrated fade of fullCue
 
            else
                set cueStart to ""
                set cueEnd to ""
                set loopStart to ""
                set loopEnd to ""
                set loopNum to ""
                set loopInf to ""
                set intFade to ""
            end if
 
            if currentType as string is "Fade" then
                (*
                set fadeMode to mode of fullCue
*)
                set stopFade to stop target when done of fullCue
                set fadeMode to ""
 
            else
                set fadeMode to ""
                set stopFade to ""
 
            end if
 
            if currentType as string is "MIDI" then
                set MIDIPatch to patch of fullCue
                set MIDICmd to command of fullCue
                set MIDIChan to channel of fullCue
                set MIDIByte1 to byte one of fullCue
                set MIDIByte2 to byte two of fullCue
                set MIDIFade to fade of fullCue
                if MIDIFade as string is "enabled" then
                    set MIDIStart to start value of fullCue
                    set MIDIEnd to end value of fullCue
                    set currentType to "MIDI Fade"
                else
                    set MIDIStart to ""
                    set MIDIEnd to ""
                    set curDur to ""
                end if
            else
                set MIDIPatch to ""
                set MIDICmd to ""
                set MIDIChan to ""
                set MIDIByte1 to ""
                set MIDIByte2 to ""
                set MIDIStart to ""
                set MIDIEnd to ""
                set MIDIFade to ""
 
            end if
 
            set currentName to q name of fullCue
 
            if exists (q name of cue target of fullCue) then
                set cueTarget to q name of cue target of fullCue
            else
                set cueTarget to ""
            end if
 
            try
                set fileTarget to (file target of fullCue as string)
            on error
                set fileTarget to ""
            end try
 
            set levelMAtrix to {}
 
            if (currentType as string is "Audio") or (currentType as string is "Fade") then
 
 
                set masterLevel to fullCue getLevel row 0 column 0
                set inputOne to fullCue getLevel row 1 column 0
                set inputTwo to fullCue getLevel row 2 column 0
 
                set IOMaster to {masterLevel & tab & inputOne & tab & inputTwo & tab}
 
                set outputRow to {}
                set inputOneRow to {}
                set inputTwoRow to {}
                set levelMAtrix to {}
 
 
                repeat with colNum from 1 to numOfColumns
                    set cueLevel to fullCue getLevel row 0 column colNum
                    if cueLevel = -120 then set cueLevel to "x"
                    copy (cueLevel & tab) as string to end of outputRow
                end repeat
 
                repeat with colNum from 1 to numOfColumns
                    set cueLevel to fullCue getLevel row 1 column colNum
                    if cueLevel = -120 then set cueLevel to "x"
                    copy (cueLevel & tab) as string to end of inputOneRow
                end repeat
 
                repeat with colNum from 1 to numOfColumns
                    set cueLevel to fullCue getLevel row 2 column colNum
                    if cueLevel = -120 then set cueLevel to "x"
                    copy (cueLevel & tab) as string to end of inputTwoRow
                end repeat
 
                set levelMAtrix to {IOMaster & outputRow & inputOneRow & inputTwoRow}
 
            end if
 
 
 
            tell me to add_this_to_tempContents(cueIndex & tab & cueID & tab & currentNum & tab & currentType & tab & currentName & tab & cueTarget & tab & fileTarget & tab & pWait & tab & curDur & tab & pstWait & tab & curCont & tab & cueStart & tab & cueEnd & tab & loopStart & tab & loopEnd & tab & loopNum & tab & loopInf & tab & intFade & tab & fadeMode & tab & stopFade & tab & MIDIPatch & tab & MIDICmd & tab & MIDIChan & tab & MIDIByte1 & tab & MIDIByte2 & tab & MIDIStart & tab & MIDIEnd & tab & MIDIFade & tab & cueBroken & tab & levelMAtrix & return)
 
 
            if (progress_factor  0) and (curCueNum mod progress_factor) = 0 then my tell_everybody((curCueNum as string) & " of " & (numCues as string) & " cues scanned so far...", 1)
 
        end repeat
    end tell
 
 
end tell
 
 
tell me to make_report()
 
set dMes to return & return & "Would you like to open the text file report with Excel now? Times and Durations will not show up correctly unless you manually tell Excel to import all columns as 'text' rather than 'general' data."
set myEnding to (display dialog "Done!" & dMes buttons {"Done", "Yes"} default button 2 with icon 1 giving up after 30)
if button returned of myEnding is "Yes" or gave up of myEnding is true then
    tell application "Microsoft Excel"
        activate
        try
            open (file_name as alias)
        on error errMs
            display dialog errMs buttons {"Cancel"}
        end try
    end tell
end if -- button is "Done"
 
 
to make_report()
    set NewContents to (tempContents as string)
 
    try
        copy (a reference to (open for access file_name with write permission)) to fileRefr
        write NewContents to fileRefr
        close access fileRefr
    on error errx number errNum from badObj
        try
            close access fileRefr
        end try
        if (errNum is equal to -48) then
            --display dialog "File exists"
            do shell script "rm " & quoted form of POSIX path of file_name
            my make_report()
        else
            display dialog "There has been an error creating the file:" & return & return & (badObj as string) & errx & return & "error number: " & errNum buttons {"Cancel"}
        end if
    end try
end make_report
 
 
to add_this_to_tempContents(x)
    copy x as string to end of tempContents
end add_this_to_tempContents
 
to tell_everybody(theNews, sec)
    tell application "QLab"
        if frontmost then
            display dialog theNews buttons {"*"} giving up after sec with icon 1
        end if
    end tell
end tell_everybody
 
to transform_seconds_to_time(x)
 
    if x > 0 then
        set curHour to (x div 3600)
        set curHour to pad_number(curHour) as string
 
        if x > 3600 then set x to (x - (curHour * 3600))
 
        set curMin to (x div 60)
        set curMin to pad_number(curMin) as string
 
 
        set curSec to x mod 60
        set curSec to ((curSec * 100) div 1) / 100 (* Set seconds to 1/100 places *)
        set curSec to pad_number(curSec) as string
        if length of curSec = 4 then set curSec to curSec & "0" (* need this in case the seconds is a clean 1/10 of a second *)
    else
        set curHour to "00"
        set curMin to "00"
        set curSec to "00.00"
    end if
 
 
 
    set fullTime to (curHour & ":" & curMin & ":" & curSec)
 
    return fullTime
 
end transform_seconds_to_time
 
to transform_wait_to_time(x)
 
    if x > 0 then
        set curHour to (x div 3600)
        set curHour to pad_number(curHour) as string
 
        if x > 3600 then set x to (x - (curHour * 3600))
 
        set curMin to (x div 60)
        set curMin to pad_number(curMin) as string
 
 
        set curSec to (x - ((curHour as number) * 3600) - ((curMin as number) * 60)) div 1
        set curSec to pad_number(curSec) as string
 
        set curFraction to ((x - ((curHour as number) * 3600) - ((curMin as number) * 60) - curSec as number) * 10) as string
        set curFraction to (get text 1 of curFraction) & (get text 3 through end of curFraction)
 
    else
        set curHour to "00"
        set curMin to "00"
        set curSec to "00"
        set curFraction to "00"
    end if
 
 
 
    set fullTime to (curHour & ":" & curMin & ":" & curSec & "." & curFraction)
    return fullTime
 
end transform_wait_to_time
 
 
 
to pad_number(x)
    if x < 10 and x  1 then
        set x to "0" & x
    else if (x < 1) and (x / 1 = x div 1) then (* Check for decimal point less than one*)
        set x to "00" & x
    else if (x < 1) and (x / 1  x div 1) then
        set x to "0" & x
    end if
 
    if x = "000" then set x to "00"
 
    return x
end pad_number