Thanks for sharing, Rob. I’m sure those will be very useful.
FWIW, here is an AppleScript that checks both files and folders using just one handler.
It is based on a script written by @ccstone and @ShaneStanley.
###AppleScript doesItemExist()
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
doesItemExist("~/Documents") --> true
doesItemExist("~/BadFolder") --> false
doesItemExist("~/Documents/A Real File.txt") --> true
doesItemExist("~/Documents/A File that does not exist.txt") --> false
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on doesItemExist(pPosixPath) -- @Path @File @Finder @ASObjC
(* VER: 1.0 2017-03-02
---------------------------------------------------------------------------------
PURPOSE: Determine if the Path Actually Exists
PARAMETERS:
• pPOSIXPath | text | POSIX Path to check
RETURNS: boolean │ true IF the file/folder does exist; else false
AUTHOR: JMichaelTX
BASED ON: Script by Chris Stone & Shane Stanley
REF:
1. Does a NSURL Have a Valid Target
https://lists.apple.com/archives/applescript-users/2017/Mar/msg00010.html
—————————————————————————————————————————————————————————————————————————————————
*)
## Requires: use framework "Foundation"
local myNSURL, doesItExistBool
--- Expand tilde (~) in Path (if it exists) ---
set pPosixPath to (current application's NSString's stringWithString:pPosixPath)'s stringByExpandingTildeInPath
--- GET NSURL & DETERMINE IF IT ACTUALLY EXISTS ---
set myNSURL to current application's |NSURL|'s fileURLWithPath:pPosixPath
set doesItExistBool to (myNSURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean
return doesItExistBool
end doesItemExist
--~~~~~~~~~~~~~~~ END OF handler doesItemExist ~~~~~~~~~~~~~~~~~~~~~~~~~
A good way to get some intuition for those two functions is probably just to look at the commented test results (in line and at the end of the code).
(Each function is shown applied to two files and two folders, and the results for each are listed)
One way to back that up with some complementary intuition may be to observe that both are wrappers for .fileExistsAtPath. The only difference is that one adds the additional isDirectory test.