Chris and Shane, thanks for sharing this great little script.
I have made a small mod to refactor into a handler, and add support for a tilde path:
-------------------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# Mod: @JMichaelTX (refactor) 2017-03-02
# dCre: 2017/03/02 11:31
# dMod: 2017/03/02 11:33
# Appl: AppleScriptObjC
# Task: Determine whether a NSURL has a valid target.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Test, @NSURL, @Valid, @Target
-------------------------------------------------------------------------------------------
use AppleScript
version
"2.4" -- Yosemite (10.10) or later -- I'm not sure about os versions...
use framework
"Foundation"
use scripting additions
-------------------------------------------------------------------------------------------
set folderPathExisting to
POSIX path of (path to
downloads folder)
set folderPathNotExisting to
POSIX path of (path to
downloads folder) &
"Nuts!"
## @JMichaelTX: My Test Cases
set goodPathBool to my
doesItemExist(folderPathExisting)
set badPathBool to my
doesItemExist(folderPathNotExisting)
set invalidPathBool to my
doesItemExist("not a valid path")
set pathWithTidleBool to my
doesItemExist("~/Downloads")
-------------------------------------------------------------------------------------------
--
## Refactor into Handler by @JMichaelTX
on doesItemExist(pPOSIXPath)
local myNSURL,
doesItExistBool
## @JMichaelTX: Add support for tidle (~) Path
set pPOSIXPath to (current application's
NSString's stringWithString:pPOSIXPath)'s
stringByExpandingTildeInPath
set myNSURL to
current application's
|NSURL|'s fileURLWithPath:pPOSIXPath
set doesItExistBool to (myNSURL's
checkResourceIsReachableAndReturnError:(missing value)) as
boolean
return doesItExistBool
end doesItemExist