Batch rename and Export movies

Hi,
I’ve done a lot of learning on this so far but it’s time to ask for help!

I’m working on a script that will First check the filenames of movie files in a specified folder (‘Input’ on the desktop), rename them if necessary based on their creation date, then export them as QuickTime movies. The QT export works fine but the checking and renaming doesn’t seem to be.

Acknowledgement here for the export script that this is based on
http://ldopa.net/2008/05/23/batch-export-for-quicktime-pro/

Script and result below:

Thanks for any help!

with timeout of 86400 seconds
	
	display dialog "Before beginning batch processing, make sure QuickTime Player is set to the desired export settings, and all videos to be processed are in a folder named ˜Input' on the desktop." with icon note
	
	tell application "Finder"
		set the startup_disk to the name of the startup disk
	end tell
	
	set user to do shell script "whoami"
	set input_folder_name to "Input"
	set input_folder to startup_disk & ":Users:" & user & ":Desktop:" & input_folder_name & ":"
	set user_desktop to startup_disk & ":Users:" & user & ":Desktop:"
	set output_folder to startup_disk & ":Users:" & user & ":Desktop:Output:"
	set file_extension to "_export.mov"
	
	try
		tell application "Finder"
			make new folder at user_desktop with properties {name:"Output"}
		end tell
	end try
	
	
	try
		set the_folder_list to list folder input_folder without invisibles
		repeat with x from 1 to count of the_folder_list
			
			set the_file to item x of the_folder_list
			set the_file_name to the name of the_file
			tell application "Finder"
				if the_file_name begins with "IMG" then
					set the item_info to the info for the_file
					set the_date to the creation date of the item_info
					set the_day to day of the_date
					set DD to the_day
					if the_day < 10 then set DD to "0" & the_day
					set the_month to (month of the_date) * 1
					set MM to the_month
					if the_month < 10 then set MM to "0" & the_month
					set yyyy to year of the_date
					set the_time to (time of the_date)
					set the_hour to the_time div hours
					set HH to the_hour
					if the_hour < 10 then set HH to "0" & the_hour
					set the_minutes to the_time mod hours div minutes
					set MN to the_minutes
					if the_minutes < 10 then set MN to "0" & the_minutes
					set the_seconds to the_time mod hours mod minutes
					set SS to the_seconds
					if the_seconds < 10 then set SS to "0" & the_seconds
					set new_name to "CANON" & yyyy & "-" & MM & "-" & DD & " " & HH & ";" & MN & ";" & SS & ".mov"
					set the name of the_file to new_name
				end if
			end tell
		end repeat
	end try
	
	
	try
		set the_folder_list to list folder input_folder without invisibles
		repeat with x from 1 to count of the_folder_list
			set the_file to input_folder & item x of the_folder_list
			set output_file to output_folder & item x of the_folder_list & file_extension
			tell application "QuickTime Player 7"
				activate
				open the_file
				export front document to output_file as QuickTime movie using most recent settings with replacing
				close front document
			end tell
		end repeat
	on error
		display dialog "This script requires a folder named ˜" & input_folder_name & "˜ located on the desktop." with icon stop
	end try
	
	say "Finished"
	
end timeout

and result:

tell application "AppleScript Editor"
	display dialog "Before beginning batch processing, make sure QuickTime Player is set to the desired export settings, and all videos to be processed are in a folder named ˜Input' on the desktop." with icon note
		--> {button returned:"OK"}
end tell
tell application "Finder"
	get name of startup disk
		--> "Macintosh HD"
end tell
tell current application
	do shell script "whoami"
		--> "cameronh"
end tell
tell application "Finder"
	make new folder at "Macintosh HD:Users:cameronh:Desktop:" with properties {name:"Output"}
		--> error "The operation can't be completed because there is already an item with that name." number -48
end tell
tell current application
	list folder "Macintosh HD:Users:cameronh:Desktop:Input:" without invisibles
		--> {"clip-2011-12-01 11;15;28.mov", "IMG_0217.MOV"}
	list folder "Macintosh HD:Users:cameronh:Desktop:Input:" without invisibles
		--> {"clip-2011-12-01 11;15;28.mov", "IMG_0217.MOV"}
end tell
tell application "QuickTime Player 7"
	activate
	open "Macintosh HD:Users:cameronh:Desktop:Input:clip-2011-12-01 11;15;28.mov"
		--> document "clip-2011-12-01 11;15;28.mov"
	export document 1 to "Macintosh HD:Users:cameronh:Desktop:Output:clip-2011-12-01 11;15;28.mov_export.mov" as QuickTime movie using most recent settings with replacing
	close document 1
	activate
	open "Macintosh HD:Users:cameronh:Desktop:Input:IMG_0217.MOV"
		--> document "IMG_0217.MOV"
	export document 1 to "Macintosh HD:Users:cameronh:Desktop:Output:IMG_0217.MOV_export.mov" as QuickTime movie using most recent settings with replacing
	close document 1
end tell
tell current application
	say "Finished"
end tell

The file staring with IMG doesn’t get renamed, but the files export correctly apart from that.

Thanks for any help.

Oops. Somehow I posted the wrong script–it’s missing the file rename section. I will fix it once back at the computer in the next few hours.

{EDIT] Fixed now

First, you can simplify your life greatly by using things like these:

set user_desktop to path to desktop --will always give the correct path based on the current startup disk and logged in user

and then create the folders you could need like this:

if not exists folder input_folder then
tell application "Finder" to make new folder at user_desktop with properties {name:"Input"}
end

No need to put it in a try block if you are checking if it exists first, you can tell Finder directly in one line. The commands needs a finder path, aka colon-delimited path btw.

But what exactly is your question? Is there something that is not working properly? getting an error somewhere?

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

And here is a list of the paths you can ask the OS to give you the correct path to:

path to application support
path to applications folder
path to desktop
path to desktop pictures folder
path to documents folder
path to downloads folder
path to favorites folder
path to Folder Action scripts
path to fonts
path to help
path to home folder
path to internet plugins
path to keychain folder
path to library folder
path to modem scripts
path to movies folder
path to music folder
path to pictures folder
path to preferences
path to printer descriptions
path to public folder
path to scripting additions
path to scripts folder
path to shared documents
path to shared libraries
path to sites folder
path to startup disk
path to startup items
path to system folder
path to system preferences
path to temporary items
path to trash
path to users folder
path to utilities folder
path to workflows folder
path to voices
path to apple menu
path to control panels
path to control strip modules
path to extensions
path to launcher items folder
path to printer drivers
path to printmonitor
path to shutdown folder
path to speakable items
path to stationery

from system domain
from local domain
from network domain
from user domain
from Classic domain

use the last 5 “from…” to ask a specific folder that exists in different domains.

Thanks leonsimard for the tips so far - I will incorporate them.

Hopefully my question is now clearer. Any file that starts with “IMG” I want to rename to a format almost identical to the file naming convention used by iMovie 11 (so the 2 files in the result posted above would have the same format except one would start with ‘CANON’ instead of ‘clip-’). That part of the script doesn’t seem to be doing anything - it just goes on to export the movies without changing the relevant file name/s.

Just for extra information after this script is complete I have another script that will convert the file creation dates of the output files based on the file name (so that’s why renaming is important in this script). Following that I will import into iPhoto & sync on to my iPad where I will edit them in iMovie. I know more steps of this process can be put into one Applescript but for now if I can just get the rename and export working in one script I’m happy to do the rest manually.

Hmm, ok. Well, you could store the contents of the list command in a variable and test each one. Something like this:

tell application "Finder"
	set user_desktop to path to desktop
	set folder_contents to list folder user_desktop without invisibles
	repeat with currentItem in folder_contents
		if currentItem begins with "IMG" then
			--do some operations on the file
		end
	end
end tell

You can also search and replace within a string using this method:

on searchAndReplaceString_stringToSearchAndReplace_theSearchString_theReplaceString(stringToSearchAndReplace, theSearchString, theReplaceString)
	set returnString to stringToSearchAndReplace
	set oldASTID to AppleScript's text item delimiters
	
	set AppleScript's text item delimiters to theSearchString
	set tempString to text items of stringToSearchAndReplace
	
	set AppleScript's text item delimiters to theReplaceString
	set returnString to tempString as string
	
	set AppleScript's text item delimiters to oldASTID
	
	return returnString
end searchAndReplaceString_stringToSearchAndReplace_theSearchString_theReplaceString

which you would use like this in your code:

set replacedString to my searchAndReplaceString_stringToSearchAndReplace_theSearchString_theReplaceString(fileName, "IMG", "CANON")

Does this helps?