Home‎ > ‎

Applescript: Extract Clips From iMovie via FCP export

Instructions

Needed for a coworker's project. Basically, copy and paste the code section into AppleScript Editor. From iMovie. use the Share::Export Final Cut XML menu option and save the resulting XML file some place where you can find it. Run the project from AppleScript Editor, select the resulting XML file, destination folder, base name, and preset. QuickTime Player will convert each clip to a standalone movie in the destination folder. I'm sure there are better ways to do some or even most of this, but it seems to work in limited testing on Snow Leopard (crosses fingers that the new hacks work on Lion...), so enjoy as-is, or modify and redistribute to your heart's content.

Code

set XMLFile to (choose file with prompt "Select a Final Cut XML project:" of type {"xml"})
set destPath to (choose folder with prompt "Select destination folder:") as text

tell application "System Events"
    tell XML element "xmeml" of contents of XML file (XMLFile as text)
        set seq to first XML element of (XML elements whose name is "sequence")
        set baseNm to value of first XML element of contents of seq whose name is "name"
        set med to first XML element of contents of seq whose name is "media"
        set vid to first XML element of contents of med whose name is "video"
        set trk to first XML element of contents of vid whose name is "track"
        set clips to XML elements of contents of trk whose name is "clipitem"
       
        set diaReply to display dialog "Please enter a base name" default answer baseNm
        if button returned of diaReply is "Cancel" then return
        set baseNm to text returned of diaReply
        set setPreset to choose from list {"Computer", "iPod", "iPhone", "480p"} with title "Select preset" with prompt "Select preset" default items {"480p"} without multiple selections allowed and empty selection allowed
       
        set cnt to 0
       
        set diaReply to display dialog "Please enter the first clip to extract (there are " & length of clips & " clips total in the file)" default answer 1
        set startClip to text returned of diaReply
        set diaReply to display dialog "Please enter the last clip to extract (there are " & length of clips & " clips total in the file)" default answer length of clips
        set endClip to text returned of diaReply
       
        repeat with clip in clips
            set cnt to cnt + 1
            if (cnt ≥ startClip) and (cnt ≤ endClip) then
                set fl to (first XML element of contents of clip whose name is "file")
                set flnm to (value of first XML attribute of contents of fl whose name is "id") as string
                set frames to (value of first XML element of contents of clip whose name is "duration")
                set op to (value of first XML element of contents of clip whose name is "in") as integer
                set ed to (value of first XML element of contents of clip whose name is "out") as integer
                set rt to (first XML element of contents of clip whose name is "rate")
                tell application "QuickTime Player"
                    set doc to open flnm
                    set dur to duration of doc
                    set nm to name of doc -- Hack to get around odd issue in Lion pt1
                    set win to (first window whose name is name of doc) -- Hack to get around odd issue in Lion pt2
                    set op_sec to op * dur / frames
                    set ed_sec to ed * dur / frames
                    trim doc from op_sec to ed_sec
                    set doc to document of win -- Hack to get around odd issue in Lion pt3
                    export doc in destPath & baseNm & " - clip " & cnt & ".mov" using settings preset setPreset
                    close doc saving no
                end tell
            end if
        end repeat
    end tell
end tell
Comments