QLab Script - Make a fade group

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

# Create Fade Group: This script will create a group and place a fade in
# and fade out around the cue that you have selected.
# This script is not designed to be run from within QLab.

set userDuration to 5
tell application "QLab"
        set mySel to selected of workspace 1
        set originalCue to last item of mySel
        if q type of originalCue is "Audio" then
                tell workspace 1
                        --Get original levels
                        set originalCueLevel to originalCue getLevel row 0 column 0
                        originalCue setLevel row 0 column 0 db -120

                        --Create Fade In
                        make type "Fade"
                        set newCue to last item of (selected as list)
                        set cue target of newCue to originalCue
                        set duration of newCue to userDuration
                        newCue setLevel row 0 column 0 db originalCueLevel
                        set continue mode of newCue to auto_continue
                        set q name of newCue to "Fade in: " & q name of originalCue

                        -- Create Fade Group Cue
                        make type "Group"
                        set theGroupCue to last item of (selected as list)
                        set q name of theGroupCue to "Fade: " & q name of originalCue

                        -- Move the Fade In Cue inside the Group Cue
                        set theAudioCueID to uniqueID of newCue
                        set theAudioCueIsIn to the first cue whose (q type is "Group" and cues contains newCue)
                        set theGroupCueID to uniqueID of theGroupCue
                        move (the first cue whose uniqueID is theAudioCueID) of theAudioCueIsIn to end of (the first cue whose uniqueID is theGroupCueID)

                        --Move the Audio Cue inside the Group Cue
                        set theAudioCueID to uniqueID of originalCue
                        set theAudioCueIsIn to the first cue whose (q type is "Group" and cues contains originalCue)
                        set theGroupCueID to uniqueID of theGroupCue
                        move (the first cue whose uniqueID is theAudioCueID) of theAudioCueIsIn to end of (the first cue whose uniqueID is theGroupCueID)

                        --Create Fade Out
                        make type "Fade"
                        set newCue to last item of (selected as list)
                        set cue target of newCue to originalCue
                        set duration of newCue to userDuration
                        newCue setLevel row 0 column 0 db -120
                        set stop target when done of newCue to true
                        set q name of newCue to "Fade out: " & q name of originalCue

                        --Move the Fade Out Cue inside the Group Cue
                        set theAudioCueID to uniqueID of newCue
                        set theAudioCueIsIn to the first cue whose (q type is "Group" and cues contains newCue)
                        set theGroupCueID to uniqueID of theGroupCue
                        move (the first cue whose uniqueID is theAudioCueID) of theAudioCueIsIn to end of (the first cue whose uniqueID is theGroupCueID)

                end tell
        end if
end tell