QLab Script - search cues for text

Contents

Table of Contents

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

    Note: QLab 3 includes built-in search functionality. The following script is intended for use with QLab 2.

    
    (* Search cues for text *)
    
    --
    
    set theExplanation to "This script will do a simple search for text in the names of cues (not cue lists) in the current QLab workspace, jumping to them " & ¬
        "one at a time until you have found what you are looking for (which only works if \"Playback position is always the selected cue\" is ON!).
    
    If looking at just the names wasn't successful, you'll then have the option of searching the file targets (although, in order to protect against invalid file targets, " & ¬
        "this part of the script will ignore cues that QLab considers \"broken\" for any reason). The search only looks at the names of the files, not the full path.
    
    More sophisticated searches (eg: searching in notes) can be done with the companion script \"All purpose enquiry\"."
    
    --
    
    (* This script is not designed to be run from within QLab!
    
    v0.9: 04/10/09 Rich Walsh (proof of concept)
    v0.9.1: 05/10/09 Got the file target search working fast enough to be usable
    v0.9.2: 05/10/09 Found way of dealing with missing file targets
    v0.9.3 12/10/09 Snow Leopard can't "get running", so rewrote a sequence
    v0.9.4: 16/10/09 Now "tested" in Snow Leopard
    v0.9.5: 27/10/09 Found a better way of preventing infinite loops; added delay required for Snow Leopard
    v1.0: 11/01/10 Improved efficiency; 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
    v1.1: 31/01/10 Better jumping mechanism
    
    <<< Last tested with: QLab 2.2.6; Mac OS 10.5.8 & 10.6.2 >>> *)
    
    -- Declarations
    
    global dialogTitle
    set dialogTitle to "Search cues for text"
    
    -- Preamble
    
    set searchText to ""
    repeat until searchText is not ""
        set searchText to text returned of (display dialog theExplanation & return & return & "Enter the text to search for:" with title dialogTitle with icon 1 ¬
            default answer "" buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel")
    end repeat
    
    -- 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"
    
        -- Test for a workspace
    
        try
            get selected of front workspace
        on error
            display dialog "There is no workspace open in QLab." with title dialogTitle with icon 0 buttons {"OK"} default button "OK" giving up after 5
            return
        end try
    
        -- Find cues that contain the search text in their name (which may take a few moments)
    
        display dialog "One moment caller..." with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 1
    
        tell front workspace
    
            set allCueLists to uniqueID of every cue list -- This will be used later
            set allCueListsRef to a reference to allCueLists
            set countCueLists to count allCueListsRef
            set foundCues to uniqueID of every cue whose q name contains searchText
            set foundCuesRef to a reference to foundCues
            set countFoundCues to count foundCuesRef
            set foundCueLists to uniqueID of every cue list whose q name contains searchText
            set foundCueListsRef to a reference to foundCueLists
            set countFoundCueLists to count foundCueListsRef
            set skipTheNames to false
    
            if countFoundCues is 0 then
                activate
                display dialog "The text \"" & searchText & "\" wasn't found in just the names.
    
    Do you want to continue with a search of the file targets (which may take some time)?" with title dialogTitle with icon 1 ¬
                    buttons {"Cancel", "OK"} default button "Cancel" cancel button "Cancel"
                set skipTheNames to true
            else if countFoundCues is equal to countFoundCueLists then
                display dialog "The text \"" & searchText & "\" only exists in the names of cue lists.
     
    Do you want to continue with a search of the file targets (which may take some time)?" with title dialogTitle with icon 1 ¬
                    buttons {"Cancel", "OK"} default button "Cancel" cancel button "Cancel"
                set skipTheNames to true
            end if
    
            -- Return the results
    
            if skipTheNames is false then
    
                repeat with eachID in foundCuesRef
                    if eachID is not in allCueListsRef then -- Don't add cue lists to search results
                        set eachCue to (first cue whose uniqueID is eachID)
                        set theContainer to (the first cue whose (q type is "Group" and cues contains eachCue))
                        repeat until mode of theContainer is cue_list
                            set theContainer to (the first cue whose (q type is "Group" and cues contains theContainer))
                        end repeat
                        set current cue list to theContainer
                        set playback position of the current cue list to eachCue
                        activate
                        display dialog "Here's one..." with title dialogTitle with icon 1 ¬
                            buttons {"That's the badger", "Next"} default button "Next" cancel button "That's the badger"
                    end if
                end repeat
    
                -- Prompt to continue with checking file targets
    
                activate
                display dialog "That's all the results from just the names.
    
    Do you want to continue with a search of the file targets (which may take some time)?" with title dialogTitle with icon 1 ¬
                    buttons {"Cancel", "OK"} default button "Cancel" cancel button "Cancel"
    
            end if
    
            -- Find cues that contain the search text in their file target
    
            set foundCues to {}
    
            set allAV to uniqueID of every cue whose broken is false and (q type is "Audio" or q type is "Video")
            set allAVRef to a reference to allAV
            set allAVTargets to file target of every cue whose broken is false and (q type is "Audio" or q type is "Video")
            set allAVTargetsRef to a reference to allAVTargets
    
            set currentTIDs to AppleScript's text item delimiters
            set AppleScript's text item delimiters to ":" -- Used to extract just the filename from the file target
            repeat with i from 1 to count allAVRef
                if (last text item of ((item i of allAVTargetsRef) as string)) contains searchText then
                    copy item i of allAVRef to end of foundCues
                end if
            end repeat
            set AppleScript's text item delimiters to currentTIDs
    
            set foundCuesRef to a reference to foundCues
            set countFoundCues to count foundCues
    
            if countFoundCues is 0 then
                activate
                display dialog "The text \"" & searchText & "\" wasn't found in the file targets." with title dialogTitle with icon 0 ¬
                    buttons {"OK"} default button "OK"
                return
            end if
    
            -- Return the results
    
            repeat with eachID in foundCuesRef
                set eachCue to (first cue whose uniqueID is eachID)
                set theContainer to (the first cue whose (q type is "Group" and cues contains eachCue))
                repeat until mode of theContainer is cue_list
                    set theContainer to (the first cue whose (q type is "Group" and cues contains theContainer))
                end repeat
                set current cue list to theContainer
                set playback position of the current cue list to eachCue
                activate
                display dialog "Here's one (in the file target)..." with title dialogTitle with icon 1 ¬
                    buttons {"That's the badger", "Next"} default button "Next" cancel button "That's the badger"
            end repeat
    
            display dialog "That's your lot." with title dialogTitle with icon 1 buttons {"Small onion"} default button "Small onion"
    
        end tell
    
    end tell
    
    (* END: Search cues for text *)