use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set thePath to (path to desktop)
if isFile(thePath) then
log "do something with my file"
end if
on isFile(thePath) -- accepts thePath as text or as alias
local tid, flag
set tid to text item delimiters
set text item delimiters to ":"
if class of thePath is alias then set thePath to thePath as text
if last text item of thePath is "" then -- its a folder
set flag to false
else
set flag to true
end if
set text item delimiters to tid
return flag
end isFile
and an even simpler one below
-- works with HFS paths, change the ":" to "/" for posix paths
on isFile(thePath)
if class of thePath is alias then set thePath to thePath as text
if text -1 of thePath is ":" then -- its a folder
return false
else
return true
end if
end isFile
It will check if the object at the target path is actually a directory or not. hasDirectoryPath will only check the path string without checking the actual item at this path.
set thePath to choose folder
log isClass(thePath)
set thePath to choose file --> if we choose a file it will return document file
log isClass(thePath)
set thePath to (path to desktop as text) & "somefile.txt"
log isClass(thePath)
set thePath to (path to desktop)
log isClass(thePath)
set thePath to POSIX path of (path to desktop) & "somefile.txt"
log isClass(thePath)
set thePath to POSIX path of (path to desktop)
log isClass(thePath)
on isClass(thePath)
tell application "Finder"
if thePath's class is text and (last character of thePath) is ":" then
return folder
else if thePath's class is text and (last character of thePath) is "/" then
return folder
end if
if thePath's class is alias then
set theProperties to thePath's properties
return theProperties's class
end if
end tell
return file
end isClass