quicktime batching with folders and subfolders

dear macscripters

I’m trying to batch convert a huge number of quicktime files to resize them. I’ve found a script here that works perfectly for the job BUT it will only convert selected files. We’ve already got everything organised into folders and subfolders and would like to keep them organised like that so that it doesn’t interfere with the final cut pro file locations.

Is there anyway of modifying this script so that it will select a folder and any subfolders and any quicktime in them and then export the resized copy into another folder (keeping the folde/subfolder names would be a bonus!)

I’ve had a bash at it but am admitting defeat as i’m quite an apple script noob and can’t quite see how the naming structure works.

the script is below and if anyone can help we would really appreciate it

thanks
Jane

property chooseFileFolder : ""
property settingsFolder : ""

on run
	checkAlias(chooseFileFolder)
	choose file with prompt "Select the file(s) to export:" default location result with multiple selections allowed without invisibles
	set chosenFiles to result
	
	set chooseFileFolder to parentFolder(first item of chosenFiles)
	open chosenFiles
end run

on open theseItems
	choose folder with prompt "Choose destination for exported file(s):"
	set exportFolder to result as Unicode text
	
	checkAlias(settingsFolder)
	choose file with prompt "Choose the QuickTime export settings file:" default location result
	set exportSettingsFile to result
	set settingsFolder to parentFolder(exportSettingsFile)
	
	try
		tell application "QuickTime Player"
			activate
			close every window
		end tell
	end try
	
	repeat with thisItem in theseItems
		-- Use the original name for the exported file
		set {name:thisName, folder:isFolder} to (info for thisItem without size)
		
		-- Skip any folders that were dropped
		if not (isFolder) then
			-- Remove extension from original name, if any
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {"."}
			try
				set thisName to text 1 thru text item -2 of thisName
			end try
			set AppleScript's text item delimiters to ASTID
			
			tell application "QuickTime Player"
				try
					open thisItem
					
					if (can export front movie as QuickTime movie) then
						
						with timeout of 86400 seconds -- 24 hours
							export front movie to (exportFolder & thisName) as QuickTime movie using settings exportSettingsFile
						end timeout
					else
						display dialog "QuickTime Player can't export "" & thisName & "" as a QuickTime movie." buttons {"Skip File"} default button 1 with icon caution
					end if
				on error errMsg number errNum
					if errNum is -2019 then set errMsg to "The export was canceled in QuickTime Player."
					
					display dialog "Error " & errNum & return & return & errMsg buttons {"Cancel Script", "Skip File"} default button 2
					if (button returned of result) is "Cancel Script" then error number -128
				end try
				
				try
					close front movie saving no
				end try
			end tell
		end if
	end repeat
	
	quit application "QuickTime Player"
	tell me to display dialog "Export script finished!" buttons {"View Export Folder", "OK"} default button 2 with icon note
	
	if (button returned of result) is "View Export Folder" then tell application "Finder" to open exportFolder
end open

on checkAlias(someItem)
	try
		return someItem as alias
	on error
		return path to home folder
	end try
end checkAlias

on parentFolder(someItem)
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	try
		set someFolder to (text 1 thru text item -2 of (someItem as Unicode text)) & ":"
	on error
		set someFolder to ""
	end try
	set AppleScript's text item delimiters to ASTID
	return someFolder
end parentFolder

AppleScript tags added by ACB

I think my strategy would be to have the user choose a folder containing the hierarchy, duplicate it, and work with the original or the copy:

tell application "Finder"
	set F to (choose folder without invisibles) as alias
	set N to name of F
	set C to container of F as alias
	set CF to (duplicate F to C) as alias -- CF will be the name of the copy (N & " copy")
end tell

At that point work on the files in place:

-- given a folder reference called "target"; F or CF above (in the same tell block)
tell application "Finder"
	try
		set tFiles to files of entire contents of target as alias list
	on error -- fails if only one.
		set tFiles to files of entire contents of target as alias as list
	end try
end tell

Now operate on the list of aliases to every file, first testing them for the type you want.

You get type by running this against one of your files:

set P to type identifier of (info for (choose file without invisibles))

This might work. Since you said all of the movies you wanted to convert were “quicktime” movies I assumed they all have a “.mov” file extension. If that is true then the following script might work. In my limited testing it does what you ask, so you can give it a try and let me know if you have problems (of course let me know if it works too).

(* this script will convert the movies found in a folder to another folder, and will place the converted movies in the same folder heirarchy as the originals. Note: you need a quicktime "export settings" file for this script. *)

-- get the source and destination folders for the converted movies
set sourceFolder to choose folder with prompt "Choose the source folder with the movies:"
set destinationFolder to choose folder with prompt "Choose the destination folder where you want the exported movies saved:"
set exportSettingsFile to choose file with prompt "Choose the QuickTime export settings file:" without invisibles

-- get a list of all the ".mov" quicktime files inside the source folder
tell application "Finder"
	try
		set tFiles to files of entire contents of sourceFolder whose name extension is "mov"
	on error -- fails if only one.
		set tFiles to files of entire contents of sourceFolder whose name extension is "mov" as list
	end try
end tell

-- convert the tFiles list to a list of the file paths in string format
set filestrings to {}
repeat with afile in tFiles
	set end of filestrings to afile as string
end repeat

-- create a list of lists where the first item in each list is the path to the original file and the second item is the path where the converted movie file should be saved
set old_new_paths to {}
set sourcestring to sourceFolder as string
set destinationstring to destinationFolder as string
set {TIDs, text item delimiters} to {text item delimiters, sourcestring}
repeat with i from 1 to (count of filestrings)
	set newpath to destinationstring & text item 2 of (item i of filestrings)
	set end of old_new_paths to {(item i of filestrings), newpath}
end repeat


-- create the folder hiearchy inside the destination folder
set text item delimiters to ":"
repeat with i from 1 to (count of old_new_paths)
	set newpath_file to item 2 of (item i of old_new_paths)
	set newpath to (text items 1 thru -2 of newpath_file) as string
	--set newpath to newpath as string
	tell application "Finder"
		if not (exists folder newpath) then
			do shell script "mkdir -p " & quoted form of POSIX path of newpath
		end if
	end tell
end repeat
set text item delimiters to TIDs

-- open the movies in quicktime and convert them into their new path
tell application "QuickTime Player"
	activate
	repeat with aMovie in old_new_paths
		try
			open (item 1 of aMovie)
			if (can export front document as QuickTime movie) then
				with timeout of 86400 seconds -- 24 hours
					export front document to (item 2 of aMovie) as QuickTime movie using settings exportSettingsFile
				end timeout
			else
				display dialog "QuickTime Player can't export this movie as a QuickTime movie." buttons {"Skip File"} default button 1 with icon caution
			end if
		on error errMsg number errNum
			if errNum is -2019 then set errMsg to "The export was canceled in QuickTime Player."
			display dialog "Error " & errNum & return & return & errMsg buttons {"Cancel Script", "Skip File"} default button 2
			if (button returned of result) is "Cancel Script" then error number -128
		end try
		try
			close front document saving no
		end try
	end repeat
end tell

-- finish up the script
quit application "QuickTime Player"
set frontApp to displayed name of (info for (path to frontmost application))
tell application frontApp to display dialog "Export script finished!" buttons {"View Export Folder", "OK"} default button 2 with icon note
if (button returned of result) is "View Export Folder" then
	tell application "Finder"
		activate
		open destinationFolder
	end tell
end if

regulus, thank you! that one works a treat… that’s hours of tedious work just gone!

My editor is a reluctant mac convert and is yet to fully appreciate the POWER of mac scripting. This will also serve the secondary purpose of silencing his anti-mac bitching and moaning… and help his journey from the PC-darkside to jedi mac master (maybe).

adam, thank you too although I lost it after duplicating the file structure. I think I’m too much of a newbie to this stuff to actually construct anything. Once this massive project is over i’m going to do a crash course in apple scripting. If I keep doing animation then there is a heap of very repetitive tasks that I would prefer my computer to think about than develop RSI doing them myself. I’ll save the RSI for moving puppets about.

thanks again

Jane

You’re welcome Jane. It’s always a bonus when I can help convert those lost souls who have yet to realize the power of the mac.

Applescript is awesome. I’ve only just learned it myself this year, with the help of the great people on MacScripters. I’d suggest if you want to learn applescript then do some of the simple tutorials on this website. There’s lots of them in the “unscripted” section, one series in particular that I liked is the one entitled “AppleScript Tutorial for Beginners”. Before you know it you’ll be creating your own scripts and having lots of time for playing with puppets!

Good luck!