QLab Script - make it go faster

Contents

Table of Contents

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

 
# Make it go faster 
 
--
 
set theExplanation to "This script will trawl through all the cues in the last Group Cue currently selected in QLab " & ¬
    "and scale their Pre & Post Waits by the ratio you enter."
 
--
 
(* This script is not designed to be run from within QLab!
 
v1.0: 26/09/09 Rich Walsh
v1.1: 27/09/09 General tidy up; added countdown timer, time taken
v1.2: 12/10/09 Snow Leopard can't "get running", so rewrote a sequence; also minor fix to subroutine
v1.3: 16/10/09 Now "tested" in Snow Leopard; expanded makeNiceT to allow for hours; general tidying
v1.4: 11/01/10 Improved efficiency; corrected minor typos; 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 >>> *)
 
-- ###FIXME### QLab and/or the "script runner" is, generally, increasingly unresponsive to scripts the more cues there are in a workspace
 
-- Declarations
 
global dialogTitle
set dialogTitle to "Make it go faster"
 
-- Preamble
 
display dialog theExplanation & return & return & "It may take a little while to run; 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 Group Cue in the selection, or give up
 
    set badSelection to true
    repeat with i from countSelection to 1 by -1
        if q type of item i of theSelection is "Group" then
            set badSelection to false
            set theGroupCue to item i of theSelection
            exit repeat
        end if
    end repeat
    if badSelection is true then
        display dialog "QLab's selection does not contain a Group Cue." with title dialogTitle with icon 0 buttons {"OK"} default button "OK" giving up after 5
        return
    end if
 
    -- Prompt for the ratio
 
    set rateQuestion to ""
    repeat until rateQuestion is not ""
        set rateQuestion to text returned of (display dialog "Enter the ratio by which you wish to adjust the times (eg: 1.1 will make them 10% longer):" with title ¬
            dialogTitle default answer "" buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel")
        try
            set theRateChange to rateQuestion as number
            if theRateChange is less than or equal to 0 then
                set rateQuestion to ""
            end if
        on error
            set rateQuestion to ""
        end try
    end repeat
 
    display dialog "One moment caller..." with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 1
 
    set startTime to time of (current date)
 
    set currentName to q name of theGroupCue
    if currentName is "" then
        set nameSuffix to ("| Times adjusted x" & theRateChange as string) & " |"
    else if last character of currentName is "|" then
        set nameSuffix to (" Times adjusted x" & theRateChange as string) & " |"
    else
        set nameSuffix to (" | Times adjusted x" & theRateChange as string) & " |"
    end if
    set q name of theGroupCue to currentName & nameSuffix
 
    -- Scale the times
 
    set allCues to cues of theGroupCue
    set allCuesRef to a reference to allCues
    set allPres to pre wait of every cue of theGroupCue
    set allPresRef to a reference to allPres
    set allPosts to post wait of every cue of theGroupCue
    set allPostsRef to a reference to allPosts
    set countCues to count allCuesRef
 
    repeat with i from 1 to countCues
        set eachCue to item i of allCuesRef
        set pre wait of eachCue to (item i of allPresRef) * theRateChange
        set post wait of eachCue to (item i of allPostsRef) * theRateChange -- Watch out for this with auto-follow cues,
        (* as the post wait is still addressable even though it is hidden *)
        if i mod 50 is 0 and (countCues - i) > 25 then -- Countdown timer (and opportunity to escape)
            set timeTaken to ((time of (current date)) - startTime) as integer
            set timeString to my makeMMSS(timeTaken)
            if frontmost then
                display dialog (("Time elapsed: " & timeString & " - " & i as string) & " of " & countCues as string) & " cues done..." with title ¬
                    dialogTitle with icon 1 buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel" giving up after 1
            end if
        end if
    end repeat
 
    set timeTaken to ((time of (current date)) - startTime) as integer
    set timeString to my makeNiceT(timeTaken)
    activate
    display dialog "Done. 
 
 (That took " & timeString & ".)" with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 60
 
end tell
 
-- Subroutines
 
on makeMMSS(howLong)
    set howManyMinutes to howLong div 60
    set minuteString to (howManyMinutes as string)
    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
 
on makeNiceT(howLong)
    if howLong is 0 then
        return "less than a second"
    end if
    set howManyHours to howLong div 3600
    if howManyHours is 0 then
        set hourString to ""
    else if howManyHours is 1 then
        set hourString to "1 hour"
    else
        set hourString to (howManyHours as string) & " hours"
    end if
    set howManyMinutes to (howLong mod 3600) div 60
    if howManyMinutes is 0 then
        set minuteString to ""
    else if howManyMinutes is 1 then
        set minuteString to "1 minute"
    else
        set minuteString to (howManyMinutes as string) & " minutes"
    end if
    set howManySeconds to howLong mod 60 as integer
    if howManySeconds is 0 then
        set secondString to ""
    else if howManySeconds is 1 then
        set secondString to "1 second"
    else
        set secondString to (howManySeconds as string) & " seconds"
    end if
    if hourString is not "" then
        if minuteString is not "" and secondString is not "" then
            set theAmpersand to ", "
        else if minuteString is not "" or secondString is not "" then
            set theAmpersand to " and "
        else
            set theAmpersand to ""
        end if
    else
        set theAmpersand to ""
    end if
    if minuteString is not "" and secondString is not "" then
        set theOtherAmpersand to " and "
    else
        set theOtherAmpersand to ""
    end if
    return hourString & theAmpersand & minuteString & theOtherAmpersand & secondString
end makeNiceT
 
(* END: Make it go faster *)