Convert file over a network using a watch folder (Mac only)

All things .jsx

Moderator: Paul Tuersley

Post Reply
Posts: 54
Joined: December 12th, 2007, 9:56 am
Location: London

Convert a file over a network using a watch folder and After Effects (Mac only)

This is a script that combines Applescript and AEscript to convert a Quicktime clip from one codec to another through a watch folder. This was needed by a client because clips coming from a variety of sources (Macs, PCs, Avid suites) were to be encoded into MPEG-4s using special software, but this encoder could not read the native formats coming from the other machines. So one of the computers on the network was used as a dedicated machine, employing this script and AE, to convert any file into the Apple Uncompressed 8 bit codec. The files are rendered directly to the PC with the MPEG-4 encoder installed.

For this script to function it must be placed in the HD/Library/Scripts/Folder Action Scripts folder, and then activated by control clicking on a chosen watch folder and selecting "Attach a Folder Action...".

It works pretty well, but I am getting some timeout errors (the applescript keeps on running even after it's finished, it seems, and the error pops up about 15 minutes later). If someone knows Applescript really well (it's pretty new to me) and knows how to fix that, please post.

(Paste the following into the Applescript Script Editor and save as a .scpt)

Code: Select all

on adding folder items to this_folder after receiving added_items
      -- wait for all added items to copy into folder before continuing
      repeat with i from 1 to number of items in added_items
         set this_item to item i of added_items
         tell application "Finder"
            -- while the file codes are temporary, the file is still copying
            repeat while creator type of this_item is "MACS" and file type of this_item is "brok"
               delay 1
            end repeat
         end tell
      end repeat
      -- activate AE (this will be ignored if it's already open)
      tell application "Adobe After Effects 6.5"
         with timeout of 300 seconds
            set renderDestination to "//ATEME$/inputs/704x576p/"
            -- begin After Effects script: close any open project (if it isn't rendering) and open a new one
            set line1 to "if (app.project.renderQueue.rendering != true){" & return
            set line2 to "app.project.close(CloseOptions.DO_NOT_SAVE_CHANGES);" & return
            set line3 to "app.newProject();}" & return
            set closeAnyExistingProjectsThenOpenNewProject to line1 & line2 & line3
            DoScript closeAnyExistingProjectsThenOpenNewProject with override
            -- end After Effects script
            -- begin After Effects script: stop the render (if already rendering, so that any new files can be imported)
            DoScript "app.project.renderQueue.stopRendering();" with override
            -- end After Effects script
            -- begin After Effects script: reset the last render if it was interrupted (it will be set to start over)
            set line1 to "var theQueue = app.project.renderQueue;" & return
            set line2 to "var lastQueueNumber = theQueue.numItems;" & return
            set line3 to "if (lastQueueNumber > 0){" & return
            set line4 to "var stoppedComp = theQueue.item(lastQueueNumber).comp;" & return
            set line5 to "var resetComp = theQueue.items.add(stoppedComp);" & return
            set line6 to "var newFilePath2 = \"" & renderDestination & "\" + + \".mov\";" & return
            set line7 to "if (File(newFilePath2).exists == true) {File(newFilePath2).remove()};" & return
            set line8 to "theQueue.item(lastQueueNumber+1).applyTemplate(\"Best Settings\");" & return
            set line9 to "theQueue.item(lastQueueNumber+1).outputModule(1).applyTemplate(\"Uncompressed 8 bit\");" & return
            set line10 to "theQueue.item(lastQueueNumber+1).outputModule(1).file = new File(newFilePath2);" & return
            set line11 to "}" & return
            set resetStoppedRender to line1 & line2 & line3 & line4 & line5 & line6 & line7 & line8 & line9 & line10 & line11
            DoScript resetStoppedRender
            -- end After Effects script
            -- import clips copied into the watch folder into AE
            open added_items
            -- begin After Effects script      
            set line1 to "var proj = app.project;" & return
            set line2 to "var theItems = proj.selection;" & return
            set line3 to "for (var k=0; k<theItems.length; k++)" & return
            set line4 to "{" & return
            set line5 to "var activeItem = theItems[k];" & return
            set line6 to "if (activeItem.width==720){activeItem.pixelAspect=1.42}else{activeItem.pixelAspect=1};" & return
            set line7 to "var fieldType = activeItem.mainSource.fieldSeparationType;" & return
            set line8 to "var wasInterlaced = false;" & return
            set line9 to "if (fieldType != FieldSeparationType.OFF) {fieldType = FieldSeparationType.OFF;wasInterlaced = true;}" & return
            set line10 to "var compName =,\".\"));" & return
            set line11 to "var activeComp = proj.items.addComp(compName, 720, 576, 1.42, activeItem.duration-0.04, activeItem.frameRate);" & return
            set line12 to "var NewLayer = activeComp.layers.add(activeItem);" & return
            set line13 to "if (wasInterlaced == true)" & return
            set line14 to "{" & return
            set line15 to "NewLayer(\"Effects\").addProperty(\"Reduce Interlace Flicker\");" & return
            set line16 to "NewLayer(\"Effects\")(\"Reduce Interlace Flicker\").softness.setValue(1);" & return
            set line17 to "}" & return
            set line18 to "var theQueue = app.project.renderQueue;" & return
            set line19 to "var newFilePath = \"" & renderDestination & "\" + compName + \".mov\";" & return
            set line20 to "if (File(newFilePath).exists == true) {File(newFilePath).remove()};" & return
            set line21 to "theQueue.items.add(activeComp);" & return
            set line22 to "var lastQueueNumber = theQueue.numItems;" & return
            set line23 to "theQueue.item(lastQueueNumber).applyTemplate(\"Best Settings\");" & return
            set line24 to "theQueue.item(lastQueueNumber).outputModule(1).applyTemplate(\"Uncompressed 8 bit\");" & return
            set line25 to "theQueue.item(lastQueueNumber).outputModule(1).file = new File(newFilePath);" & return
            set line26 to "}" & return
            set line27 to "proj.renderQueue.render();" & return
            set ChangeFormatOfAddedItems to line1 & line2 & line3 & line4 & line5 & line6 & line7 & line8 & line9 & line10 & line11 & line12 & line13 & line14 & line15 & line16 & line17 & line18 & line19 & line20 & line21 & line22 & line23 & line24 & line25 & line26 & line27
            DoScript ChangeFormatOfAddedItems
            -- end After Effects script
         end timeout
      end tell
   on error the error_message number the error_number
      display dialog "Error: " & the error_number & ". " & the error_message
   end try
end adding folder items to
Post Reply