Check to see if file exists before changing file name
I record a radio program during the night for 3 hours with the recording being broken into one hour segments. The files are Midnight At The Oasis.mp3, Midnight At The Oasis 2.mp3, etc. I have an AppleScript that changes the names of the files by adding yesterdays date and the hour number to the file name, i.e. Midnight At The Oasis - (yesterdays date) - Hour 1.mp3.
My script worked as long as all 3 hours or recordings existed so I decided to add an if statement to see if the individual hour exists before attempting to change the name. Now I get error messages saying that the file can't be found. Here is my script with the "if exists":
set Yesterday to (current date) - (24 * 60 * 60)
set myYesterday to (date string of Yesterday)
set y to year of date myYesterday
set m to month of date myYesterday
set d to day of date myYesterday
set newMyYesterday to m & " " & d & ", " & y as text
set theFile to "Users/Tony/Recordings/Midnight At The Oasis/Midnight At The Oasis.mp3"
if exists file theFile then
tell application "Finder"
set newHourOne to "Midnight At The Oasis - " & newMyYesterday & " - Hour 1.mp3"
set theFile to (POSIX file "/Users/Tony/Recordings/Midnight At The Oasis/Midnight At The Oasis.mp3") as alias
tell application "Finder"
set the name of file theFile to newHourOne
end tell
end tell
end if
set theFile to "Users/Tony/Recordings/Midnight At The Oasis/Midnight At The Oasis 2.mp3"
if exists file theFile then
tell application "Finder"
set newHourTwo to "Midnight At The Oasis - " & newMyYesterday & " - Hour 2.mp3"
set theFile to (POSIX file "/Users/Tony/Recordings/Midnight At The Oasis/Midnight At The Oasis 2.mp3") as alias
tell application "Finder"
set the name of file theFile to newHourTwo
end tell
end tell
end if
set theFile to "Users/Tony/Recordings/Midnight At The Oasis/Midnight At The Oasis 3.mp3"
if exists file theFile then
tell application "Finder"
set newHourThree to "Midnight At The Oasis - " & newMyYesterday & " - Hour 3.mp3"
set theFile to (POSIX file "/Users/Tony/Recordings/Midnight At The Oasis/Midnight At The Oasis 3.mp3") as alias
tell application "Finder"
set the name of file theFile to newHourThree
end tell
end tell
end if
Can someone please tell me where the error is?
MacBook Pro, OS X Mavericks (10.9)