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)

Posted on Nov 1, 2015 5:38 AM

Reply
5 replies

Nov 1, 2015 2:22 PM in response to CheapSeats

In addition to any POSIX vs Mac path issues, AppleScript doesn't know if files exist - the Finder or System Events do. You can also try to set an alias in a try statement and catch the error if it doesn't exist. You are in desperate need of a handler, but for your posted script, you just need to move the file check into the Finder tell statements, for example


tell application "Finder"
  set theFile to POSIX file "Users/Tony/Recordings/Midnight At The Oasis/Midnight At The Oasis.mp3"
  if exists theFile then
      --
  end if
end tell

Nov 2, 2015 11:47 AM in response to CheapSeats

(* 
  *)
set fullName to ((path to desktop as Unicode text) & "TestFormat.rtf")
if FileExists(fullName) is true then
    -- Ok, you do something
    display dialog "File already exists ;-)" giving up after 7
else
   -- Ok, no file found.
end if
(* 
  Author: Philip Regan
  http://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence

 *)

on FileExists(theFile) -- (String) as Boolean
    tell application "System Events"
        if exists file theFile then
            return true
        else
            return false
        end if
    end tell
end FileExists

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Check to see if file exists before changing file name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.