AppleScript Finder Exists Exact Match

I’m running an AppleScript through VBA for Excel 2013’s in-built function MacScript.

The exact script that I am passing is the following:

tell application "Finder" exists file "FILE PATH:123.pdf" end tell 

This works as you would expect if the file is present: it returns true if the file 123.pdf exists at that file path. However, it also returns true if the file 0123.pdf is at that location, even if 123.pdf does not exist.

I need to adjust my script to essentially look for exact matches for file name… rather than treating leading 0s in a file name as insignificant.

In all other cases, it has returned true/false as expected.

Note: I thought it might be trying to do the equivalent of checking whether the file name ‘contains’ 123.pdf… but then I tried creating a false file named 1123.pdf. In this case the script returned false as I expected.

Thanks in advance for any advice you might be able to give!

Try using this Handler:


on checkFileExists(FileToCheckString)
	try
		set thePath to quoted form of (POSIX path of FileToCheckString)
		do shell script "test -f " & thePath
		return true
	on error
		return false
	end try
end checkFileExists

Hmm, I was hoping to avoid VBA code since this is an AppleScript forum. However, Excel doesn’t seem to like this script. I did have to modify it to allow to pass newline characters into the AppleScript.

This is what I tried to run:

“on checkFileExists(” & FileOrFolderstr & “)” & Chr(13) & _
“try” & Chr(13) & _
"set thePath to quoted form of (POSIX path of " & FileOrFolderstr & “)” & Chr(13) & _
“do shell script " & Chr(34) & “test -f” & Chr(34) & " & thePath” & Chr(13) & _
“return true” & Chr(13) & _
“on error” & Chr(13) & _
“return false” & Chr(13) & _
“end try” & Chr(13) & _
“end checkFileExists”

And I receive a run-time error ‘5’ Invalid procedure call or argument.

Note, I did try to run it through the Script Editor (not VBA), exactly as you wrote it. Of course the variable isn’t defined, so I replaced “FileToCheckString” with the full path on both instances it occurs in your example. This produced no result. When I removed the on CheckFileExists and end checkFileExists lines, the Script Editor ran and correctly returned true/false.

However, I try the same trick in Excel, then I still receive the runtime error. You’re expertise may not be in Excel, so you may not be able to help me with that, but you can probably at least enlighten me on what the on/end for CheckFileExists is supposed to accomplish and why it may not work in ScriptEditor.

Thank you.

TMA’s code’s a handler (ie. a subroutine). It doesn’t do anything unless called from another line in the script.


on checkFileExists(FileToCheckString)
	try
		set thePath to quoted form of (POSIX path of FileToCheckString)
		do shell script "test -f " & thePath
		return true
	on error
		return false
	end try
end checkFileExists

checkFileExists("FILE PATH:123.pdf")

A simpler solution would be to use System Events instead of the Finder in your original script. That does take leading zeros into account:

tell application "System Events"
	exists file "FILE PATH:123.pdf"
end tell

Excellent! That worked. It’s also nice to know that AppleScripts has function support. I just learned about AppleScripts’ existence while trying to get this method of file verification to work (I’m a Windows guy at heart).

However, in my quest to understand these scripts, why does Finder not distinguish with the 0s? It’s nice that System Events is an alternative option, but frankly it seems like something that both should do by default.

Thanks again for the assistance!

It’s a straight-out bug.