AppleScript code for determining if file exists

Delurking for a quick code snippet. I needed to have AppleScript check to see if a file exists before doing something. Initially I was trying to do this with a bare if *filepath* exists, until ninety seconds of searching revealed something obvious. You need to tell the Finder to do it.

For example…


tell application "Finder"
if exists file "Macintosh HD:Users:chrisb:Documents:test file.txt" then
display dialog "it exists"
else
display dialog "it does not exist"
end if
end tell

Leave a Reply

Your email address will not be published. Required fields are marked *